Rotate canvas along its center based on user touch - Android
Posted
by
Ganapathy
on Game Development
See other posts from Game Development
or by Ganapathy
Published on 2011-08-13T08:56:38Z
Indexed on
2012/09/06
21:53 UTC
Read the original article
Hit count: 304
I want to rotate the canvas circularly on its center axis based on user touch.
i want to rotate based on center but its rotating based on top left corner .
so i am able to see only 1/4 for rotation of image.
any idea..
Like a old phone dialer .
I have tried like as follows
onDraw(Canvas canvas){
canvas.save();
// do my rotation
canvas.rotate(rotation,0,0);
canvas.drawBitmap( ((BitmapDrawable)d).getBitmap(),0,0,p );
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
updateRotation(x,y);
mPreviousX = x;
mPreviousY = y;
invalidate();
}
private void updateRotation(float x, float y) {
double r = Math.atan2(x - centerX, centerY - y);
rotation = (int) Math.toDegrees(r);
}
© Game Development or respective owner