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

IndieDev

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

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
* The variable Rot has the float rotation value.
 
Solution

IndieDev

Active Member
Licensed User
No, no.
The #If Java code is being called (in a kind of loop), but I am getting this error.

Error:
LastException=(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.animation.AccelerateInterpolator.getInterpolation(float)' on a null object reference

The Rotation float value is received in the doRotate function.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Upvote 0
Solution

IndieDev

Active Member
Licensed User
You haven't created an AccelerateInterpolator instance
Java:
float factor = 1;
AccelerateInterpolator mInterpolator = new AccelerateInterpolator(factor);

I get this error now.
Error:
src\b4a\compass\main.java:1171: error: variable mInterpolator is already defined in class main
    AccelerateInterpolator mInterpolator = new AccelerateInterpolator(factor);
                           ^

Please allow me a liitle time to put together a quick sample program and upload.
 
Upvote 0

IndieDev

Active Member
Licensed User
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:
If Java code:
AccelerateInterpolator mInterpolator;

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

Thank you.
 
Upvote 0
Top