//--- 短時間針の表示
fun updateHoure(hour :Int,minu:Int) {
var imgHoure: ImageView? = findViewById(R.id.short_hari)
val bitmap1 = BitmapFactory.decodeResource(
resources,
R.drawable.short_hari
)
imgHoure!!.setImageBitmap(bitmap1)
// 画像中心を基点に回転
val degrees = CircleUtil.computeAngleByHour(hour,minu)
imgHoure.setRotation(degrees)
}
//--- 長針の表示
fun updateMinur(minu:Int) {
var imgMinture: ImageView? = findViewById(R.id.long_hari)
・・ 省略・・
// 画像中心を基点に回転
val degrees = CircleUtil.computeAngleByMinite(minu)
imgMinture.setRotation(degrees)
}
//--- 秒針の表示
fun updateSecond(secnd:Int) {
var imgSecond: ImageView? = findViewById(R.id.second_hari)
・・ 省略・・
// 画像中心を基点に回転
val degrees = CircleUtil.computeAngleByMinite(secnd)
imgSecond.setRotation(degrees)
}
--- 角度の取得I/F--
package com.example.timeclock.activity.MyUtil
object CircleUtil {
private const val CIRCLE_RADIUS = 360
// -- index番目の要素の角度を計算して返す
fun computeAngleByIndex(partitions: Int, index: Int): Float {
val angleUnit = (CIRCLE_RADIUS / partitions).toFloat()
return index * angleUnit
}
// -- 分時間の角度を返す(長針、秒針)
fun computeAngleByMinite(minute: Int): Float {
val angleUnit = (CIRCLE_RADIUS / 60).toFloat()
return minute * angleUnit
}
// -- 時間の角度を返す(短針)
fun computeAngleByHour(hour: Int, minute: Int): Float {
val angleUnit = CIRCLE_RADIUS.toFloat() / (12 * 60)
return (hour * 60 + minute) * angleUnit
}
}