title: How to keep a circle inside another circle android view control
body: <p>I am trying to create a pad-like view in android. I got a circle that follows user's gestures and I am using distance to keep the circle of going outside the main circle of the pad control.
  My problem is I want the circle to keep following the gesture, but to stay inside of the main circle. I am using the formula for finding a point using angle and radius, but it does some funky stuff.</p>
  
  <p>I am translating the canvas, so that the center of the main circle is at 0, 0.
  Here is the code:</p>
  
  <pre><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 &lt;= this.maxDistance) {
          this.handleX = x;
          this.handleY = y;
      } else {
          float angle = (float) Math.toDegrees(Math.atan2(y, x));
          if (angle &lt; 0)
              angle += 360;
  
          this.handleX = (int) ((this.mainRadius - this.handleRadius) * Math.cos(angle));
          this.handleY = (int) ((this.mainRadius - this.handleRadius) * Math.sin(angle));
      }
  
      //onTranslateHandle(distance);
  }
  </code></pre>
  
  <p>And here is the funky stuff in a gif image: <img src="https://i.stack.imgur.com/Dj7nX.gif" alt="The issue animated"></p>