Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

Android rotation animation

I have a problem and I just can't manage to find a solution that works. So, here is my problem. I have to make an ImageView to rotate itself, to 90 degree on orientation change. I did that, and the image is actually rotating pretty awesome, but when the animation finishes, the image reset itself to previous position.

Here is the code I used to rotate the image:

   Matrix matrix = mImageView.getImageMatrix();
                    RectF dst = new RectF();
                    matrix.mapRect(dst, new RectF(mImageView.getDrawable().getBounds()));
                    mAnimation = new RotateAnimation(0.0f, -90.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    mAnimation.setDuration(5000);
                    mImageView.startAnimation(mAnimation);
                    mImageView.setImageMatrix(matrix);
                    mCurrentOrientation = 1;

Upvotes: 1

Views: 2812

Answers (2)

Marko Niciforovic
Marko Niciforovic

Reputation: 3601

you can use this to animation persist after it has been done:

mAnimation.setFillEnabled(true);
mAnimation.setFillAfter(true);

Upvotes: 1

Michal Palczewski
Michal Palczewski

Reputation: 918

One thing you can do is set the imageview to an already rotated version of the image and then rotate it from -90 to 0 degrees. That will work with the API you are already using. If you make the two calls, one right after it will work correctly without stutter.

The other option is to use a later API version. The Object Animator animations do not return the view to it's original position. More on this here.

How to rotate a drawable by ObjectAnimator?

The code is simple, but this doesn't work with gingerbread.

Upvotes: 0

Related Questions