0

I want to set time from the analog clock in an Android Application. For this I have overridden the Analogclock.java class and have established the Ontouchevent listener. I followed this method and this formula to calculate the angle between the hour and the minute hand from the obtained Coordinates. Now I'm able to move the hand to a new position. And I can also fetch the new angle between the needles from this. This is the code for the same :

   case MotionEvent.ACTION_MOVE: 
        x = (int)event.getX();
        y = (int)event.getY();
        double dx = x - minuteRect.centerX();    
        double dy = -(y - minuteRect.centerY()); 
        double inRads = Math.atan2(dy,dx);      

        if (inRads < 0) 
            inRads = Math.abs(inRads);
        else
            inRads = 2*Math.PI - inRads;      

        double newDegree =  Math.toDegrees(inRads); 
        if(isMove()){
            setAngle(newDegree);
            invalidate(); 
        } 

I now want to set the time where the needle is being moved. Is there any way I can calculate the time from the Coordinates or from the angle between the two needles?

Community
  • 1
  • 1
JaveDeveloper
  • 137
  • 2
  • 7
  • 18

1 Answers1

1

I don't know the code, but the math is not hard:

Divide the angle of the hour hand by 2PI, multiply by 12 and truncate, and you have the hours. Divide the angle of the minute hand by 2PI, multiply by 60 and truncate, and you have the minutes.

Example : 06:15:

  • The hour angle is a little bit more than PI. (PI / 2PI) * 12 = 6
  • The minute angle is PI/2. ((PI/2) / 2PI) * 60 = 15
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But I'll only have the angle between the two needles. To get the angle of the hour, the formula I saw was - 1/2(60*Hr+Min). But in my case I won't have the updated minutes. I'll only have the angle between the needles from the above code and the coordiantes wrt to the clock - (x,y) – JaveDeveloper Jan 06 '12 at 03:32
  • In your case, how did u tell that PI/2 is the minute angle? and PI as the hour angle? – JaveDeveloper Jan 06 '12 at 03:42
  • If you take the positive y axis as the reference (i.e. 12 o'clock), and if it's 6:15, the angle of the hour needle is pi, adn the angle of the minute needle is PI/2. – JB Nizet Jan 06 '12 at 07:43