Skip to content

Instantly share code, notes, and snippets.

@ntakouris
Created December 14, 2014 17:16
Show Gist options
  • Save ntakouris/a94afe57a67b1aae6a2f to your computer and use it in GitHub Desktop.
Save ntakouris/a94afe57a67b1aae6a2f to your computer and use it in GitHub Desktop.
@Override
public void onDraw(Canvas canvas, Rect bounds) {
/* draw your watch face */
// Update the time
mTime.setToNow();
int width = bounds.width();
int height = bounds.height();
// 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 = width / 2f;
float centerY = height / 2f;
float circleCenter = centerY + FLAT_TIRE_OFFSET;
float minsPassed = mTime.minute;
float hoursPassed = mTime.hour;
float secsPassed = mTime.second;
float timePassedForCircle = minsPassed + hoursPassed * 60;
double rads = 0.87222222292 * timePassedForCircle;
canvas.drawArc();
// Compute rotations and lengths for the clock hands.
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;
// Only draw the second hand in interactive mode.
/*if (!mAmbient) {
float secX = (float) Math.sin(secRot) * secLength;
float secY = (float) -Math.cos(secRot) * secLength;
canvas.drawLine(centerX, centerY, centerX + secX, centerY +
secY, mSecondPaint);
}*/
// Draw the minute and hour hands.
float minX = (float) Math.sin(minRot) * minLength;
float minY = (float) -Math.cos(minRot) * minLength;
canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY,
mMinutePaint);
float hrX = (float) Math.sin(hrRot) * hrLength;
float hrY = (float) -Math.cos(hrRot) * hrLength;
canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY,
mHourPaint);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment