From d684db72edf0e9c516769fb2a7d0651f74ee3d5f Mon Sep 17 00:00:00 2001 From: Raymond Yang Date: Mon, 10 Apr 2023 14:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=96=87=E5=AD=97=E8=A1=A8?= =?UTF-8?q?=E7=9B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ray650128/iosclockwidget/IOSClockView.kt | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/ray650128/iosclockwidget/IOSClockView.kt b/app/src/main/java/com/ray650128/iosclockwidget/IOSClockView.kt index 0c00bad..0002626 100644 --- a/app/src/main/java/com/ray650128/iosclockwidget/IOSClockView.kt +++ b/app/src/main/java/com/ray650128/iosclockwidget/IOSClockView.kt @@ -59,6 +59,15 @@ class IOSClockView: View { } } + private val clockHourText: Paint by lazy { + Paint().apply { + isAntiAlias = true + style = Paint.Style.FILL + color = Color.BLACK + textSize = width * 0.1f + } + } + private val clockMinuteScale: Paint by lazy { Paint().apply { isAntiAlias = true @@ -103,7 +112,7 @@ class IOSClockView: View { drawClockFace(canvas) - drawScaleHourHand(canvas, 12) + drawTime(canvas) postInvalidateDelayed(30) } @@ -146,10 +155,20 @@ class IOSClockView: View { val stopX = centerX + sin(degrees.toDouble()).toFloat() * (radius * 0.95f) val stopY = centerY - cos(degrees.toDouble()).toFloat() * (radius * 0.95f) canvas?.drawLine(startX, startY, stopX, stopY, clockHourScale) + if (i % 5 == 0) { + val text = "${if (i % 12 == 0) 12 else i % 12}" + val textX = centerX + sin(degrees.toDouble()).toFloat() * (radius * 0.75f) - if (text.length == 1) { + (width * 0.1f) / 3.25f + } else { + (width * 0.1f) / 2f + } + val textY = centerY - cos(degrees.toDouble()).toFloat() * (radius * 0.75f) + (width * 0.1f) / 2.75f + canvas?.drawText(text, textX, textY, clockHourText) + } } } - private fun drawScaleHourHand(canvas: Canvas?, value: Int) { + private fun drawTime(canvas: Canvas?) { val mPaint = Paint() val mPointRange = 100f // 取得目前時間:時、分、秒