Search code examples
androidanimationandroid-animationandroid-viewobjectanimator

View rotates first time, but not second time


I have the following short code that animates a TextView when I click on a button. When I click the button the first time, it works like expected. But when I click it after that, it does not animate anymore. The onClick-Method is called, I checked that. But the animation does not get restarted.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myTv = (TextView) findViewById(R.id.myTv);

        myButton = (Button) findViewById(R.id.myButton);
        myButton.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        PropertyValuesHolder rotation = PropertyValuesHolder.ofFloat("rotationY", 360);
        ObjectAnimator cardAnimator = ObjectAnimator.ofPropertyValuesHolder(myTv, rotation);
        cardAnimator.setDuration(500);
        cardAnimator.start();
    }
}

Solution

  • First time you tell "animate this view's rotationY to 360" degrees - it animates.

    Second time you tell "animate this view's rotationY to 360" degrees - it still animates, but this time it has nothing to do, because that view is already at 360 degrees.

    Explicitly tell, that you want it to be animated from 0 to 360:

    
     PropertyValuesHolder rotation = PropertyValuesHolder.ofFloat("rotationY", 0, 360);