确定时钟指针之间的角度在android中的模拟时钟中已知的时间

时间:2012-01-05 09:20:53

标签: java android math clock

我想从Android应用程序中的模拟时钟设置时间。为此,我重写了Analogclock.java类并建立了Ontouchevent监听器。我按照 this method this formula 来计算获得的坐标的小时和分针之间的角度。现在我可以将手移到新位置。我也可以从中取出针之间的新角度。这是相同的代码:

   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(); 
        } 

我现在想设置针移动的时间。 有什么方法可以从坐标或两针之间的角度计算时间

1 个答案:

答案 0 :(得分:1)

我不知道代码,但数学并不难:

将时针的角度除以2PI,乘以12并截断,您就有了小时数。 将分针的角度除以2PI,乘以60并截断,你就有了分钟。

示例:06:15:

  • 小时角度比PI略高一点。 (PI / 2PI) * 12 = 6
  • 分钟角度为PI / 2。 ((PI/2) / 2PI) * 60 = 15