Prevent RotateAnimation from resetting when cancelled

1.4k views Asked by At

I have a simple RotateAnimation to animate an ImageView indefinitely until I stop it. I have the animation set up as follows:

    Animation spin = new RotateAnimation(0.0f, 1800.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    spin.setInterpolator(new LinearInterpolator());
    spin.setRepeatCount(Animation.INFINITE);
    spin.setDuration(5000);
    imageView.setAnimation(spin);

When I call imageView.cancelAnimation(), is it possible for me to "freeze" the animation right at whatever frame it ended on (or whatever angle it's at) instead of having it reset to the first frame?

2

There are 2 answers

3
devunwired On BEST ANSWER

You're probably better off wrapping your image contents in a RotateDrawable and setting it to the ImageView rather than using an animation. You can still animate the transition by using a simple Handler to repeatedly call setImageLevel() to adjust the rotation angle.

When the time comes to freeze the animation, just stop posting to the Handler and the image will remain at its last set level value. You can then restart if you like by resuming posts to the Handler.

Another option would be to create a custom ViewGroup and use getChildStaticTransformation() to apply a custom transform to a child ImageView. You would still be handling the animation steps manually, so you can start/stop at any point you like, but Transformation is what the animation system uses to animate views so you might get results closer to that effect.

HTH

0
jiiri On

I have an app that has to hold its rotation position after getting to its determined angle. I searched around for quite sometime and finally found that calling setFillAfter() method. This will stop the rotation from resetting.

    RotateAnimation rotate = new RotateAnimation(startingPoint, value, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    image.setAnimation(rotate);

    rotate.setDuration(ROTATION_INTERVAL);
    rotate.setFillAfter(true);