加上文字表盤

This commit is contained in:
Raymond Yang 2023-04-10 14:41:34 +08:00
parent 9b26d5e4ab
commit d684db72ed

View File

@ -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
// 取得目前時間:時、分、秒