修正分針、時針長度

This commit is contained in:
Raymond Yang 2023-04-10 13:54:55 +08:00
parent 0fe157ff87
commit d9ac3caf85
3 changed files with 31 additions and 30 deletions

View File

@ -105,7 +105,7 @@ class IOSClockView: View {
drawScaleHourHand(canvas, 12) drawScaleHourHand(canvas, 12)
postInvalidateDelayed(100) postInvalidateDelayed(30)
} }
private fun drawClockFace(canvas: Canvas?) { private fun drawClockFace(canvas: Canvas?) {
@ -157,10 +157,11 @@ class IOSClockView: View {
val hour = calendar[Calendar.HOUR] val hour = calendar[Calendar.HOUR]
val minute = calendar[Calendar.MINUTE] val minute = calendar[Calendar.MINUTE]
val second = calendar[Calendar.SECOND] val second = calendar[Calendar.SECOND]
val millisecond = calendar[Calendar.MILLISECOND]
// 計算時、分、秒的旋轉角度 // 計算時、分、秒的旋轉角度
val angleHour = (hour + minute.toFloat() / 60) * 360 / 12 val angleHour = (hour + minute.toFloat() / 60) * 360 / 12
val angleMinute = (minute + second.toFloat() / 60) * 360 / 60 val angleMinute = (minute + second.toFloat() / 60) * 360 / 60
val angleSecond = second * 360 / 60 val angleSecond = ((second + (millisecond / 1000f)) * 360f / 60f)
//region 繪製時針 //region 繪製時針
canvas?.save() canvas?.save()
@ -168,27 +169,27 @@ class IOSClockView: View {
canvas?.rotate(angleHour, centerX, centerY) canvas?.rotate(angleHour, centerX, centerY)
// 繪製時針1 // 繪製時針1
val rectHour = RectF( val rectHour = RectF(
centerX - 10f / 2, centerX - (width * 0.02f) / 2,
centerY - radius / 2, centerY - radius / 1.75f,
centerX + 10f / 2, centerX + (width * 0.02f) / 2,
centerY - radius / 6 centerY - radius / 6
) )
// 設定時針畫筆屬性 // 設定時針畫筆屬性
mPaint.color = Color.BLACK mPaint.color = Color.BLACK
mPaint.style = Paint.Style.STROKE mPaint.style = Paint.Style.FILL_AND_STROKE
mPaint.strokeWidth = 10f mPaint.strokeWidth = 10f
canvas?.drawRoundRect(rectHour, mPointRange, mPointRange, mPaint) canvas?.drawRoundRect(rectHour, mPointRange, mPointRange, mPaint)
// 繪製時針2 // 繪製時針2
val rectHour2 = RectF( val rectHour2 = RectF(
centerX - 10f / 2, centerX - (width * 0.01f) / 2,
centerY - radius / 2, centerY - radius / 1.75f,
centerX + 10f / 2, centerX + (width * 0.01f) / 2,
centerY centerY
) )
// 設定時針畫筆屬性 // 設定時針畫筆屬性
mPaint.color = Color.BLACK mPaint.color = Color.BLACK
mPaint.style = Paint.Style.FILL mPaint.style = Paint.Style.FILL_AND_STROKE
mPaint.strokeWidth = 5f mPaint.strokeWidth = 5f
canvas?.drawRoundRect(rectHour2, mPointRange, mPointRange, mPaint) canvas?.drawRoundRect(rectHour2, mPointRange, mPointRange, mPaint)
canvas?.restore() canvas?.restore()
@ -200,26 +201,26 @@ class IOSClockView: View {
// 旋轉到分針的角度 // 旋轉到分針的角度
canvas?.rotate(angleMinute, centerX, centerY) canvas?.rotate(angleMinute, centerX, centerY)
val rectMinute = RectF( val rectMinute = RectF(
centerX - 10f / 2, centerX - (width * 0.02f) / 2,
centerY - radius * 3.5f / 5, centerY - radius + 20f,
centerX + 10f / 2, centerX + (width * 0.02f) / 2,
centerY - radius / 6 centerY - radius / 6
) )
// 設定分針畫筆屬性 // 設定分針畫筆屬性
mPaint.color = Color.BLACK mPaint.color = Color.BLACK
mPaint.style = Paint.Style.STROKE mPaint.style = Paint.Style.FILL_AND_STROKE
mPaint.strokeWidth = 10f mPaint.strokeWidth = 10f
canvas?.drawRoundRect(rectMinute, mPointRange, mPointRange, mPaint) canvas?.drawRoundRect(rectMinute, mPointRange, mPointRange, mPaint)
val rectMinute2 = RectF( val rectMinute2 = RectF(
centerX - 10f / 2, centerX - (width * 0.01f) / 2,
centerY - radius * 3.5f / 5, centerY - radius + 20f,
centerX + 10f / 2, centerX + (width * 0.01f) / 2,
centerY centerY
) )
// 設定分針畫筆屬性 // 設定分針畫筆屬性
mPaint.color = Color.BLACK mPaint.color = Color.BLACK
mPaint.style = Paint.Style.FILL mPaint.style = Paint.Style.FILL_AND_STROKE
mPaint.strokeWidth = 5f mPaint.strokeWidth = 5f
canvas?.drawRoundRect(rectMinute2, mPointRange, mPointRange, mPaint) canvas?.drawRoundRect(rectMinute2, mPointRange, mPointRange, mPaint)
canvas?.restore() canvas?.restore()
@ -233,16 +234,16 @@ class IOSClockView: View {
// 繪製秒針 // 繪製秒針
canvas?.save() canvas?.save()
// 旋轉到分針的角度 // 旋轉到分針的角度
canvas?.rotate(angleSecond.toFloat(), centerX, centerY) canvas?.rotate(angleSecond, centerX, centerY)
val rectSecond = RectF( val rectSecond = RectF(
centerX - 5f / 2, centerX - (width * 0.005f) / 2,
centerY - radius + 20, centerY - radius + 20,
centerX + 5f / 2, centerX + (width * 0.005f) / 2,
centerY + radius / 6 centerY + radius / 6
) )
// 設定秒針畫筆屬性 // 設定秒針畫筆屬性
mPaint.strokeWidth = 5f mPaint.strokeWidth = 5f
mPaint.style = Paint.Style.STROKE mPaint.style = Paint.Style.FILL_AND_STROKE
mPaint.color = Color.parseColor("#FF7F27") mPaint.color = Color.parseColor("#FF7F27")
canvas?.drawRoundRect(rectSecond, mPointRange, mPointRange, mPaint) canvas?.drawRoundRect(rectSecond, mPointRange, mPointRange, mPaint)
canvas?.restore() canvas?.restore()

View File

@ -1,7 +1,7 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item <item
android:width="15dp" android:width="15dp"
android:height="150dp"> android:height="200dp">
<shape android:shape="rectangle"> <shape android:shape="rectangle">
<solid android:color="@android:color/transparent" /> <solid android:color="@android:color/transparent" />
<corners android:radius="10dp" /> <corners android:radius="10dp" />
@ -9,7 +9,7 @@
</item> </item>
<item <item
android:width="5dp" android:width="5dp"
android:height="75dp" android:height="100dp"
android:top="0dp" android:top="0dp"
android:left="5dp" android:left="5dp"
android:right="5dp"> android:right="5dp">
@ -20,7 +20,7 @@
</item> </item>
<item <item
android:width="7.5dp" android:width="7.5dp"
android:height="50dp" android:height="75dp"
android:top="0dp" android:top="0dp"
android:left="3.75dp" android:left="3.75dp"
android:right="3.75dp"> android:right="3.75dp">
@ -32,7 +32,7 @@
<item <item
android:width="15dp" android:width="15dp"
android:height="15dp" android:height="15dp"
android:top="68dp"> android:top="92.5dp">
<shape android:shape="oval"> <shape android:shape="oval">
<stroke android:color="@color/black" android:width="2dp" /> <stroke android:color="@color/black" android:width="2dp" />
<solid android:color="@color/black" /> <solid android:color="@color/black" />

View File

@ -1,7 +1,7 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item <item
android:width="15dp" android:width="15dp"
android:height="250dp"> android:height="295dp">
<shape android:shape="rectangle"> <shape android:shape="rectangle">
<solid android:color="@android:color/transparent" /> <solid android:color="@android:color/transparent" />
<corners android:radius="10dp" /> <corners android:radius="10dp" />
@ -9,7 +9,7 @@
</item> </item>
<item <item
android:width="5dp" android:width="5dp"
android:height="125dp" android:height="147.5dp"
android:top="0dp" android:top="0dp"
android:left="5dp" android:left="5dp"
android:right="5dp"> android:right="5dp">
@ -20,7 +20,7 @@
</item> </item>
<item <item
android:width="7.5dp" android:width="7.5dp"
android:height="100dp" android:height="125dp"
android:top="0dp" android:top="0dp"
android:left="3.75dp" android:left="3.75dp"
android:right="3.75dp"> android:right="3.75dp">
@ -32,7 +32,7 @@
<item <item
android:width="15dp" android:width="15dp"
android:height="15dp" android:height="15dp"
android:top="118dp"> android:top="140dp">
<shape android:shape="oval"> <shape android:shape="oval">
<stroke android:color="@color/black" android:width="2dp" /> <stroke android:color="@color/black" android:width="2dp" />
<solid android:color="@color/black" /> <solid android:color="@color/black" />