title: Move Minute hand in Android clock
body: <p>I am trying to write a simple proof of concept app that allows a user to rotate minute hand of a clock. I am having hard time coming up with the right logic for OnTouchEvent. </p>
  
  <p>So far I have the following code:</p>
  
  <pre><code>    public boolean onTouchEvent(MotionEvent e) {
  
         float x = e.getX();
         float y = e.getY();
  
      switch (e.getAction()) {
      case MotionEvent.ACTION_MOVE:
          //find an approximate angle between them.
  
          float dx = x-cx;
          float dy = y-cy;
          double a=Math.atan2(dy,dx);
  
          this.degree = Math.toDegrees(a);
          this.invalidate();
      }
      return true;
      }
  
  
  protected void onDraw(Canvas canvas) {
      super .onDraw(canvas);
  
      boolean changed = mChanged;
      if (changed) {
          mChanged = false;
      }
  
      int availableWidth = getRight() - getLeft();
      int availableHeight = getBottom() - getTop();
  
      int x = availableWidth / 2;
      int y = availableHeight / 2;
  
      cx = x;
      cy = y;
  
      final Drawable dial = mDial;
  
      int w = dial.getIntrinsicWidth() + 100;
      int h = dial.getIntrinsicHeight() + 100;
  
      boolean scaled = false;
  
      if (availableWidth &lt; w || availableHeight &lt; h) {
          scaled = true;
  
          float scale = Math.min((float) availableWidth / (float) w, (float) availableHeight / (float) h);
  
          canvas.save();
          canvas.scale(scale, scale, x, y);
      }
  
      if (changed)
      {
          dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
      }
  
      dial.draw(canvas);
  
      canvas.save();
      float hour = mHour / 12.0f * 360.0f;
  
      canvas.rotate(hour, x, y);
  
      final Drawable hourHand = mHourHand;
  
      if (changed) {
  
          w = hourHand.getIntrinsicWidth() + 30;
          h = hourHand.getIntrinsicHeight() + 30;
  
          hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y  + (h / 2));
      }
  
      hourHand.draw(canvas);
      canvas.restore();
  
      canvas.save();
      float minute = mMinutes / 60.0f * 360.0f;
  
      if (bearing == 0)
      {
          canvas.rotate(minute, x, y);
      }
      else
      {
          canvas.rotate((float)bearing, x, y);
      }
  
      final Drawable minuteHand = mMinuteHand;
  
      if (changed) {
  
          w = minuteHand.getIntrinsicWidth() + 30;
          h = minuteHand.getIntrinsicHeight() + 30;
  
          minuteHand.setBounds(x - w, y - h, x + w, y  + h);
      }
  
      minuteHand.draw(canvas);
      canvas.restore();
  
      if (scaled) {
          canvas.restore();
      }
  }
  </code></pre>
  
  <p>Then based on that, my OnDraw method rotates the minute hand to the specified "this.degree"(just calls canvas.rotate). I am assuming my math is off here.  I tried to follow the example here: <a href="https://stackoverflow.com/questions/5102942/calculate-angle-for-rotation-in-pie-chart">Calculate angle for rotation in Pie Chart</a>, but that's still not rotating the minute hand correctly. Any help would be appreciated. </p>