Rotate canvas along its center based on user touch - Android
I want to rotate the canvas circularly on its center axis based on user touch.
Like a old phone dialer .
i want to rotate based on center
but
Now its rotating based on top left corner .so i am able to see only 1/4 for rotation of image.
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);
}
You need to add this to your custom view methods
@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
}
This method will give you the canvas size then use
canvas.rotate(rotation, screenW / 2, screenH / 2);
Pass the point of rotation to rotate api:
canvas.rotate(rotation, 0, centerY);
Instead of rotating the Canvas you can use x=0;
x=x+50;
yourview.setRotation(x);
use this on touch event and x=x-50 for rotating backword
I think it will help
精彩评论