Android: animated viewing in onClick ()

I have an ImageView that I want to animate when a user clicks on it.

So, I ended up with this simple solution:

public void onClick(View v) {
            imageView.animate()
                    .rotationX(360).rotationY(360)
                    .setDuration(1000)
                    .setInterpolator(new LinearInterpolator());
        }

It works just fine, but only FIRST TIME (the first click plays the animation, after which the animation does not work at all).

How can i fix this?

+4
source share
1 answer

You need to reset the rotation before or after each animation. For instance:

imageView.animate()
    .rotationX(360).rotationY(360)
    .setDuration(1000)
    .setInterpolator(new LinearInterpolator())
    .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animator) {
            imageView.setRotationX(0);
            imageView.setRotationY(0);
        }
    });
+2
source

All Articles