2

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.

JiTHiN
  • 6,548
  • 5
  • 43
  • 69

2 Answers2

4

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

            }
Sudar Nimalan
  • 3,962
  • 2
  • 20
  • 28
  • This code is not working. The needle is not deflecting at all. – JiTHiN Jul 02 '12 at 08:37
  • sorry, i missed one line, super.applyTransformation(interpolatedTime, t); – Sudar Nimalan Jul 02 '12 at 10:08
  • Thanks buddy, it work like a charm. But, the needle is coming back to its original position once the animation is completed. I want it to stay there. Is there any way to fix it? – JiTHiN Jul 03 '12 at 05:41
  • I think yes, first set the target rotation to the needle, needle.setRotation(213) before needle.startAnimation(rpmNeedleDeflection); and try – Sudar Nimalan Jul 03 '12 at 06:00
  • I'm getting an error while using setRotation(), "The method setRotation(int) is undefined for the type ImageView". Should I import something? – JiTHiN Jul 03 '12 at 06:22
  • http://developer.android.com/reference/android/view/View.html#setRotation(float) add from API level 11, i am wondering which API level you use? – Sudar Nimalan Jul 03 '12 at 06:27
  • then you may use http://developer.android.com/reference/android/graphics/drawable/RotateDrawable.html – Sudar Nimalan Jul 03 '12 at 06:35
  • I've found another way solve my problem. Thanks for your help. – JiTHiN Jul 03 '12 at 06:42
  • @rIHaN JiTHiN, could u plz share the code, even I have to rotate the needle according to my speed value. see my question here http://stackoverflow.com/questions/11753730/rotating-speedometer-needle-gauge-around-its-center-as-the-speed-changes – Randroid Aug 01 '12 at 09:32
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);
         }