Hi,
Been working on code for a compass.
Got stuck at converting the code below to B4A.
This slows down rotation of the compass (Ease To functionality).
Would appreciate if anyone could help converting this or enlighten me on how to use this in #If Java.
This is how I tried to use in #If Java code.
Am calling the above #If Java code like this:
* The variable Rot has the float rotation value.
Been working on code for a compass.
Got stuck at converting the code below to B4A.
This slows down rotation of the compass (Ease To functionality).
Would appreciate if anyone could help converting this or enlighten me on how to use this in #If Java.
Java code:
protected Runnable mCompassViewUpdater = new Runnable() {
@Override
public void run() {
final float MAX_ROTATE_DEGREE = 1.0f;
if (mPointer != null && !mStopDrawing) {
if (mDirection != mTargetDirection) {
/** Calculate the short routine */
float to = mTargetDirection;
if (to - mDirection > 180) {
to -= 360;
} else if (to - mDirection < -180) {
to += 360;
}
/** Limit max speed to MAX_ROTATE_DEGREE */
float distance = to - mDirection;
if (Math.abs(distance) > MAX_ROTATE_DEGREE) {
distance = distance > 0 ? MAX_ROTATE_DEGREE : (-1.0f * MAX_ROTATE_DEGREE);
}
/** Slow down if the distance is short */
mDirection = normalizeDegree(mDirection
+ ((to - mDirection)
* mInterpolator.getInterpolation(Math.abs(distance) > MAX_ROTATE_DEGREE ? 0.4f : 0.3f)));
mPointer.updateDirection(mDirection);
}
}
}
};
private float normalizeDegree(float degrees) {
return (degrees + 720) % 360;
}
This is how I tried to use in #If Java code.
If Java code:
#If JAVA
import android.view.animation.AccelerateInterpolator;
import java.lang.Object;
import java.util.concurrent.Callable;
float mDirection;
//float mTargetDirection;
AccelerateInterpolator mInterpolator;
boolean mStopDrawing;
public float doRotate(float mTargetDirection) {
BA.Log("mTargetDirection=" + mTargetDirection);
final float MAX_ROTATE_DEGREE = 1.0f;
if (!mStopDrawing) {
if (mDirection != mTargetDirection) {
/** Calculate the short routine */
float to = mTargetDirection;
if (to - mDirection > 180) {
to -= 360;
} else if (to - mDirection < -180) {
to += 360;
}
/** Limit max speed to MAX_ROTATE_DEGREE */
float distance = to - mDirection;
if (Math.abs(distance) > MAX_ROTATE_DEGREE) {
distance = distance > 0 ? MAX_ROTATE_DEGREE : (-1.0f * MAX_ROTATE_DEGREE);
}
/** Slow down if the distance is short */
mDirection = normalizeDegree(mDirection
+ ((to - mDirection)
* mInterpolator.getInterpolation(Math.abs(distance) > MAX_ROTATE_DEGREE ? 0.4f : 0.3f)));
//mPointer.updateDirection(mDirection);
}
BA.Log("mDirection=" + mDirection);
return mDirection;
}
return 0.0f;
}
private float normalizeDegree(float degrees) {
return (degrees + 720) % 360;
}
#End If
Am calling the above #If Java code like this:
B4A code:
Try
Dim s As String = NativeMe.RunMethod("doRotate", Array(Rot))
Log("s=" & s)
Catch
Log("LastException=" & LastException)
End Try