Search code examples
androidrotationandroid-imageviewangle

Rotating an image, and displaying angle of rotation


I am making a virtual speedometer in android. I use the RotateAnimation() to rotate the speedometer needle.

Is there any way to get the angular position of the needle during the rotation? That is, if the needle is rotating from 0 to 360 degree, I want to display the needles angle at each point during the period of animation. I use the following code to perform the animation.

RotateAnimation rpmNeedleDeflection = new RotateAnimation(-32, 213, (float) 135, needle.getHeight()/2);
rpmNeedleDeflection.setDuration(1500);
rpmNeedleDeflection.setFillAfter(true);
needle.startAnimation(rpmNeedleDeflection);
needle.refreshDrawableState();

Any help please... Thanks in advance.


Solution

  • you may try to override applyTransformation

        RotateAnimation rpmNeedleDeflection = new RotateAnimation(fromDegrees, toDegrees, ...){
                    protected void applyTransformation(float interpolatedTime,Transformation t) {
                        float currentDegree = fromDegrees+(toDegrees-fromDegrees)*interpolatedTime;
                        super.applyTransformation(interpolatedTime, t);
                    };
    
                }