Reputation: 133
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?
Upvotes: 0
Views: 2267
Reputation: 692053
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:
(PI / 2PI) * 12 = 6
((PI/2) / 2PI) * 60 = 15
Upvotes: 1