emmby
emmby

Reputation: 100464

Rotate an Android ImageView AND its background

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?

Upvotes: 2

Views: 5218

Answers (2)

emmby
emmby

Reputation: 100464

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();
}

Upvotes: 4

Rajeev
Rajeev

Reputation: 121

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);
    }
}

Upvotes: 2

Related Questions