tags:

views:

1062

answers:

1

I have a layout with an image on it (embedded in an ImageView). I need to rotate the image (let's say) 90 degrees CCW.

I've written code to animate the image rotating...:

public class MainActivity extends Activity
{
    private ImageView mImageView = null;
    private Animation mRotateAnimation = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImageView = (ImageView) findViewById(R.id.my_image);
        mRotateAnimation = AnimationUtils.loadAnimation(this, R.anim.my_rotate_90);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mImageView.startAnimation(mRotateAnimation);
            return true;
        }
        return super.onTouchEvent(event);
    }
}

The image smoothly rotates 90 degrees, but then snaps back to its original state. This is what the Android documentation says will happen after an animation completes. Presumably, on the notification that the animation has ended, I'm supposed to transform the ImageView (or the underlying drawable), and possibly invalidate it to trigger a redraw.

All well and good, except that I can't find a way to do it, and I can't find any examples of anyone else doing it.

I tried using getImageMatix/setImageMatrix on mImageView, with no apparent effect. There are subclasses of Drawable that will rotate an image, but there is no setDrawable() method on ImageView, so I don't see how to use one.

I searched the examples; though a few of them involve animation and rotation (notably LunarLander), none of them are animating an ImageView, and then leaving it in some transformed state.

Surely I'm missing something simple here... aaaargh, how do you rotate an ImageView within a layout?

Thanks.

+1  A: 

ImageView setImageDrawable will configure the drawable in the Image. This should allow you to use the ImageView class, and thus use the Matrix functions on that.

Mayra