0

Hi I was going through Sample of analog watch.In onDraw method I came across this logic

 @Override
    public void onDraw(Canvas canvas, Rect bounds) {
        mTime.setToNow();

        // Draw the background.
        if (isInAmbientMode()) {
            canvas.drawColor(Color.BLACK);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }

        // Find the center. Ignore the window insets so that, on round watches with a
        // "chin", the watch face is centered on the entire screen, not just the usable
        // portion.
        float centerX = bounds.width() / 2f;
        float centerY = bounds.height() / 2f;

        float secRot = (mTime.second / 30f) * (float) Math.PI;
        int minutes = mTime.minute;
        float minRot = minutes / 30f * (float) Math.PI;
        float hrRot = ((mTime.hour + (minutes / 60f)) / 6f) * (float) Math.PI;

        float secLength = centerX - 20;
        float minLength = centerX - 40;
        float hrLength = centerX - 80;

        if (!mAmbient) {
            float secX = (float) Math.sin(secRot) * secLength;
            float secY = (float) -Math.cos(secRot) * secLength;
            canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mHandPaint);
        }

        float minX = (float) Math.sin(minRot) * minLength;
        float minY = (float) -Math.cos(minRot) * minLength;
        canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mHandPaint);

        float hrX = (float) Math.sin(hrRot) * hrLength;
        float hrY = (float) -Math.cos(hrRot) * hrLength;
        canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHandPaint);
    }

I am not understanding what is exact meaning of these line

float secRot = (mTime.second / 30f) * (float) Math.PI;
    int minutes = mTime.minute;
    float minRot = minutes / 30f * (float) Math.PI;
    float hrRot = ((mTime.hour + (minutes / 60f)) / 6f) * (float) Math.PI;
Lokesh Tiwari
  • 10,496
  • 3
  • 36
  • 45

1 Answers1

0

It seems like it is trying to calculate the rotation angle of each of the hands. It is moving the seconds and minutes hands independently but moving the hour hand also dependent on the minutes. At "12:30" the hour hand would be between 12 and 1.

meedz
  • 132
  • 13