Search code examples
androidanimationframerotateanimation

Prevent RotateAnimation from resetting when cancelled


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?


Solution

  • 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