Pavlo Zin
Pavlo Zin

Reputation: 770

Android: animating view in onClick()

I have ImageView that I want to animate it whenever 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 perfect, but only FIRST TIME (first click plays animation, after that animation does not work at all).

How can I fix that?

Upvotes: 4

Views: 2628

Answers (1)

Alexis King
Alexis King

Reputation: 43842

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

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

Upvotes: 3

Related Questions