I am trying to create a pad-like view in android. I have a circle that follows user gestures, and I use distance to keep a circle that goes beyond the main circle of the pad control. My problem is that I want the circle to continue to follow the gesture, but remain inside the main circle. I use a formula to find a point using angle and radius, but it does cause some funky things.
I translate the canvas, so the center of the main circle is 0, 0. Here is the code:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(this.mainRadius, this.mainRadius);
canvas.drawCircle(0, 0, this.mainRadius, this.debugPaint);
canvas.drawCircle(this.handleX, this.handleY, this.handleRadius, this.handlePaint);
}
private void translateHandle(MotionEvent event) {
int x = (int) (event.getX() - this.mainRadius);
int y = (int) (event.getY() - this.mainRadius);
double distance = distanceFromCenter(x, y);
if (distance <= this.maxDistance) {
this.handleX = x;
this.handleY = y;
} else {
float angle = (float) Math.toDegrees(Math.atan2(y, x));
if (angle < 0)
angle += 360;
this.handleX = (int) ((this.mainRadius - this.handleRadius) * Math.cos(angle));
this.handleY = (int) ((this.mainRadius - this.handleRadius) * Math.sin(angle));
}
}
And here is the funky in the gif image: 
source
share