androidobjectanimator

Android ObjectAnimator with constant speed


I used ObjectAnimator to rotate my view from 0 to 360 degree. But the speed of rotation is not constant. I need a constant speed as the animation should repeat a few times. Any acceleration in speed disturbs the consistency of the animation. This is my code:

ObjectAnimator animRotate = ObjectAnimator.ofFloat(ivLoader,"rotation", 0,360);
animRotate.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animation) {
            animRotate.start();
        }
});
animRotate.start();

Solution

  • check out Interpolator class, default one for ValueAnimator (ObjectAnimator is extending it) is:

    private static final TimeInterpolator sDefaultInterpolator =
            new AccelerateDecelerateInterpolator();
    

    it will accelerate during "starting phase" and decelate on end. you want linear interpolation so:

    ObjectAnimator animRotate = ...
    animRotate.setInterpolator(new LinearInterpolator());
    animRotate.addListener(... // rest of code
    

    but consider replacing AnimatorListener with

    animRotate.setRepeatMode(ValueAnimator.INFINITE);
    

    there is also setRepeatCount method