I'm trying to get my app to display a circle that the user can rotate to a certain degree by touching and dragging the circle. Say the maximum the circle can be rotated is 60 degrees, either way.
I've extended the View class to create my custom View. I've drawn a circle on my canvas with the drawCircle() method. To rotate it, I know I'd have to do a canvas.rotate(), but I'm not sure what to fix the center as. How do I measure this angle? What do I rotate about?
public class DrawingView extends View {
Paint paint;
Canvas canvas;
int originalX = 100;
int originalY = 50;
int radius = 75;
// ... some code ...
// paint, canvas initialised in constructors
@Override
protected void onDraw(Canvas canvas){
canvas.drawCircle(originalX, originalY, radius, paint);
// some more code ...
}
@Override
public boolean onTouchEvent(MotionEvent event){
int x = (int)event.getX();
int y = (int)event.getY();
case MotionEvent.ACTION_DOWN:
// Detect if touch in my circle
break;
case MotionEvent.ACTION_MOVE:
double tx = x - originalX;
double ty = y - originalY;
double tlength = Math.sqrt(tx*tx + ty*ty);
double angle = Math.acos(radius/tlength);
double degrees = (angle * 180)/Math.PI;
System.out.println("Degrees " + (angle*180)/Math.PI);
break;
}
}
The code I've displayed doesn't seem to give me a correct value. Like, if I touch and drag all the way to the end of the screen, it gives me a value around 70 degrees. I don't think I'm calculating the angle right, but I have no idea how to either.
I followed the selected answer's way of finding the angle. I then used canvas.rotate() before drawing the circle.