I am drawing a complicated shape using canvas.drawPath. The result of these drawPath methods is something like a map which is bigger than the screen. I would like the user to do the following: move the map using one finger. Or rotate it around the center of the screen using 2 fingers. Basically it should behave exactly like Google Maps only without scaling. So far I was able to get the desired movement and rotation:
private void handleTouch(MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
{
rotate=true;
move=false;
a=Math.toDegrees(-Math.atan2(event.getX(0)-event.getX(1),event.getY(0)-event.getY(1)));
if(a>360)a=a-360;
if(a<-360)a=a+360;
da=a-angle;
if(da>360)da=da-360;
if(da<-360)da=da+360;
}
break;
case MotionEvent.ACTION_POINTER_UP:
{
rotate=false;
move=false;
x = (int)event.getX();
y = (int)event.getY();
dx = x - translate.x;
dy = y - translate.y;
}
break;
case MotionEvent.ACTION_DOWN:
{
move=true;
x = (int)event.getX();
y = (int)event.getY();
dx = x - translate.x;
dy = y - translate.y;
}
break;
case MotionEvent.ACTION_MOVE:
{
if(rotate && event.getPointerCount()==2)
{
angle=Math.toDegrees((-Math.atan2(event.getX(0)-event.getX(1),event.getY(0)-event.getY(1))))-da;
if(angle>360)angle=angle-360;
if(angle<-360)angle=angle+360;
vmap.invalidate();
}
else if(move==true)
{
translate.set((int)event.getX() - dx,(int)event.getY() - dy);
//trans.setTranslate(translate.x,translate.y);
vmap.invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
{
//your stuff
}
break;
}
The problem is correctly applying them together. I can easily make the rotate around its own center if I first translate and then rotate around the center coordinate of the map. However if I try to rotate around the center of the screen things start to get complicated. Lets say I moved the canvas 20 pixels left and then rotated it 30 degrees, then moved 50 pixels down and then rotated it another 50 degrees. If I try to add the two angles together during the second rotation, I will now be rotating around a different coordinate, meaning that as soon as I do the new rotation the map is going to suddenly jump.
I looked into using matrices, but so far that didn't help.