2

I'm rotating an ImageView called photo with this code:

RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(400);
    rotate.setFillEnabled(true);
    rotate.setFillAfter(true);
    rotate.setInterpolator(new LinearInterpolator());
    rotate.setAnimationListener(new Animation.AnimationListener() {
    });
    photo.startAnimation(rotate);

It rotates nicely and persists. Later I might want to change the picture in this ImageView. However, my new image appears rotated because of the persisted animation on the previous one. How can I "undo" this animation before switching images?

gesuwall
  • 587
  • 2
  • 5
  • 15
  • maybe this will help you: http://stackoverflow.com/questions/4120824/android-reversing-an-animation – Daniel Bo Sep 14 '15 at 23:06
  • 1
    Try to do the reverse animation and perhaps set the duration to 0 if you don't want it animated... – Darwind Sep 14 '15 at 23:07
  • stopAnimation() not working? – NecipAllef Sep 15 '15 at 03:32
  • stopAnimation() would not work as the animation finished a long time ago. So far the only viable solution is creating a reverse animation with 0 duration, but I'm not sure if that's the best solution... – gesuwall Sep 15 '15 at 04:34

1 Answers1

2

Apparently the best way to do this is with another animation with zero duration. If I have my original animation in this function:

private void rotatePhoto(int fromDegrees, int toDegrees, int duration) {
    RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(duration);
    rotate.setFillEnabled(true);
    rotate.setFillAfter(true);
    rotate.setInterpolator(new LinearInterpolator());
    rotate.setAnimationListener(new Animation.AnimationListener() {
    });
    photo.startAnimation(rotate);
}

I can rotate my photo 90 degrees with something like this: rotatePhoto(0,90,400). Afterwards, to "undo" the animation, I can just use rotatePhoto(0,0,0)

gesuwall
  • 587
  • 2
  • 5
  • 15