Payam Roozbahani
Payam Roozbahani

Reputation: 1345

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

Upvotes: 0

Views: 1158

Answers (1)

snachmsm
snachmsm

Reputation: 19233

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

Upvotes: 5

Related Questions