Search code examples
javaandroid-studioobjectanimator

Object Animation will only animate object once


Once a button (randomButton) has been animated, it will not be animated again - but why? How can I force the animation every time ObjectAnimator is called on it?

Button randomButton = eliminate();
randomButton.setText("");
objectAnimator = ObjectAnimator.ofFloat(randomButton, "rotation", 180);
objectAnimator.setDuration(500);
objectAnimator.start();

Solution

  • You need a second value in your in order to reanimate the object.

    objectAnimator = ObjectAnimator.ofFloat(randomButton, "rotation", 180);
    

    This is from https://developer.android.com/reference/android/animation/ObjectAnimator, "A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start() is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation)."

    The correct code would be:

        objectAnimator = ObjectAnimator.ofFloat(randomButton, "rotation", 0, 180);