Rotating an image, and displaying angle of rotation

此生再无相见时 提交于 2019-11-26 22:09:06

问题


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.


回答1:


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);
                };

            }



回答2:


Use this code to keep the needle in final position:

if(currentDegree==toDegrees)
        {
            needle.refreshDrawableState();
            RotateAnimation nrpmNeedleDeflection=new rotateAnimation(toDegrees,toDegrees...);
            nrpmNeedleDeflection.setDuration(1); 
            nrpmNeedleDeflection.setFillAfter(true);
            needle.startAnimation(nrpmNeedleDeflection);
         }


来源:https://stackoverflow.com/questions/11287737/rotating-an-image-and-displaying-angle-of-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!