animation - Android RotateAnimation slows down after repeating few times -


i trying make metronome needle moving upside down pendulum. have tried rotationanimation of android seems slow down after few runs. have tried both linearinterpolator , custom interpolator(metronomeinterpolator).

the following code taken android how rotate needle when speed changes?, ihan jithin.

rotateanimation needledeflection = new rotateanimation(             this.previousangle, this.currentangle, this.pivotx, this.pivoty) {         protected void applytransformation(float interpolatedtime,                 transformation t) {             currentdegrees = previousangle + (currentangle - previousangle)                     * interpolatedtime;             currentvalue = (((currentdegrees - minangle) * (maxvalue - minvalue)) / (maxangle - minangle))                     + minvalue;             if (ndl != null)                 ndl.ondeflect(currentdegrees, currentvalue);             super.applytransformation(interpolatedtime, t);         }      };      needledeflection.setanimationlistener(new animationlistener() {         @override         public void onanimationstart(animation arg0) {         }          @override         public void onanimationrepeat(animation arg0) {         }          @override         public void onanimationend(animation arg0) {             previousangle = currentangle;         }     });     needledeflection.setduration(this.deflecttime);     needledeflection.setinterpolator(new metronomeinterpolator());     needledeflection.setfillbefore(true);     needledeflection.setfillafter(false);     needledeflection.setstartoffset(0);     needledeflection.setrepeatmode(animation.restart);     needledeflection.setrepeatcount(animation.infinite);     this.gaugeneedle.startanimation(needledeflection);     this.gaugeneedle.refreshdrawablestate(); 

the animation restart infinite times.

public class metronomeinterpolator implements interpolator { private double p=0.5;  @override public float getinterpolation(float t) {     // a: amplitude, p: period/2     //https://stackoverflow.com/questions/1073606/is-there-a-one-line-function-that-generates-a-triangle-wave     double a=1;     return (float)((a/p) * (p - math.abs(t % (2*p) - p))); } } 

the metronomeinterpolator forming triangular wave. formula taken is there one-line function generates triangle wave?, lightsider.

the whole animation works except timing. animation start , ends correctly first time. however, animation seems slower should after repeating few times. i.e. there tick tock sound playing @ background verified accurate rotation animation lag behind after few repeats.

one of reason codes running before repeating. may suggest way animation using solution can used in android make accurate pendulum? thank much.

i solved adding codes in animation listener. logged start time when animation starts. when repeating, calculate expected duration animation should use. compare time used animation. shorten animation duration if animation used more time expected. reset defined duration if animation uses less time expected.

    needledeflection.setanimationlistener(new animationlistener() {         @override         public void onanimationstart(animation arg0) {             long starttime=animationutils.currentanimationtimemillis();             gaugeview.this.animaterepeatedtimes=0;             gaugeview.this.animatestarttime = starttime;         }          @override         public void onanimationrepeat(animation arg0) {             //duration of animation * number of runs of animation + (long)starttime             long expectedendtime = gaugeview.this.deflecttime* ++gaugeview.this.animaterepeatedtimes + gaugeview.this.animatestarttime;             long repeattime=animationutils.currentanimationtimemillis();             long timeexcessused = repeattime - expectedendtime;             if(timeexcessused>0){                 //reduce animation duration                 arg0.setduration(gaugeview.this.deflecttime-timeexcessused);             }else{                 //reset animation duration normal                 arg0.setduration(gaugeview.this.deflecttime);             }         }          @override         public void onanimationend(animation arg0) {             previousangle = currentangle;         }     }); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -