2

Using this mechanism I'm able to successfully rotate the image in an ImageView. Works great.

However, if the ImageView has a background image, that drawable is not also rotated. How can I also rotate the background image of an ImageView?

Community
  • 1
  • 1
emmby
  • 99,783
  • 65
  • 191
  • 249

2 Answers2

4

Overwrite draw() instead of onDraw() to rotate the background.

@Override
public void draw(Canvas canvas) {
    canvas.save();
    canvas.rotate(45,xRotation,yRotation);
    super.draw(canvas);
    canvas.restore();
}
emmby
  • 99,783
  • 65
  • 191
  • 249
2
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);
    }
}
Onur Topal
  • 3,042
  • 1
  • 24
  • 41
Rajeev
  • 111
  • 1
  • 3