Infinite ObjectAnimator with interpolator... How to accelerate only the initial start?

NullPointerException picture NullPointerException · Jan 27, 2015 · Viewed 12k times · Source

I have a object animator with infinite repeat mode. I want to accelerate it only the first time it starts... not every time it is repeating itself

How can this be achieved?

my code:

universeMovement1 = ObjectAnimator.ofFloat(universeImageView, "x", 0, sw);  
        universeMovement1.setDuration(UNIVERSE_MOVEMENT_TIME);
        universeMovement1.setRepeatCount(ObjectAnimator.INFINITE);
        universeMovement1.setRepeatMode(ObjectAnimator.RESTART);
        universeMovement1.setInterpolator(new AccelerateInterpolator());

Answer

Whitney picture Whitney · Jan 29, 2015

Add a listener to your animation with the method onAnimationRepeat and set the interpolator back to LinearInterpolator, or whatever you want. Hence when it repeats it won't accelerate anymore.

animation.addListener(new AnimatorListenerAdapter(){
        @Override
        public void onAnimationRepeat(Animator animation) {
            animation.setInterpolator(new LinearInterpolator());
        }
    });