Android Question [Solved] Need help converting Java code to B4A

IndieDev

Active Member
Licensed User
Longtime User
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.

[CODE lang="java" title="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;
}
[/CODE]

This is how I tried to use in #If Java code.
[CODE lang="java" title="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
[/CODE]

Am calling the above #If Java code like this:
[CODE lang="b4x" title="B4A code"]
Try
Dim s As String = NativeMe.RunMethod("doRotate", Array(Rot))
Log("s=" & s)
Catch
Log("LastException=" & LastException)
End Try
[/CODE]
* The variable Rot has the float rotation value.
 
Solution
No, no.
The #If Java code is being called (in a kind of loop), but I am getting this error.

[CODE lang="b4x" title="Error"]
LastException=(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.animation.AccelerateInterpolator.getInterpolation(float)' on a null object reference
[/CODE]

The Rotation float value is received in the doRotate function.
 
Last edited:
Upvote 0
Upvote 0
Solution
You haven't created an AccelerateInterpolator instance
Java:
float factor = 1;
AccelerateInterpolator mInterpolator = new AccelerateInterpolator(factor);

I get this error now.
[CODE lang="b4x" title="Error"]
src\b4a\compass\main.java:1171: error: variable mInterpolator is already defined in class main
AccelerateInterpolator mInterpolator = new AccelerateInterpolator(factor);
^
[/CODE]

Please allow me a liitle time to put together a quick sample program and upload.
 
Upvote 0
You probably have blindly added my code and left your original declaration so you now have two declarations. Remove the original one.
Spot on!! ?

I actually had a declaration:
[CODE lang="b4x" title="If Java code"]AccelerateInterpolator mInterpolator;[/CODE]

Now, the #If JAVA code is returning values.
Will update my code for the returned values and revert back.

Thank you.
 
Upvote 0
Back
Top