class ClockActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Column(modifier = Modifier.fillMaxSize()) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(0.5f)
) {
Clock()
}
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(0.5f)
) {
Clock1()
}
}
}
}
@Composable
fun Clock1() {
val hour = remember {
mutableStateOf(0f)
}
val minute = remember {
mutableStateOf(0f)
}
val second = remember {
mutableStateOf(0f)
}
LaunchedEffect(key1 = "") {
while (true) {
val cal = Calendar.getInstance()
hour.value = cal.get(Calendar.HOUR).toFloat()
minute.value = cal.get(Calendar.MINUTE).toFloat()
second.value = cal.get(Calendar.SECOND).toFloat()
delay(1000)
}
}
val diameter = 200.dp
val color1 = Color.Black
Canvas(
modifier = Modifier.size(diameter, diameter)
) {
val outCircleR = size.width / 2
val innerCircleR = (size.width / 2 * 0.8).toFloat()
drawCircle(
brush = Brush.radialGradient(
colors = listOf(Color.DarkGray, Color.Gray)
)
,
radius = outCircleR,
alpha = 0.5f
)
drawCircle(color = color1, radius = size.width * 0.02f)
drawCircle(color = color1, radius = outCircleR, style = Stroke(4f))
translate(size.width / 2, size.height / 2) {
drawIntoCanvas { canvas ->
val textPaint = Paint().asFrameworkPaint().apply {
isAntiAlias = true
typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
textAlign = android.graphics.Paint.Align.CENTER
}
textPaint.color = android.graphics.Color.BLACK
textPaint.textSize = 50f
val nativeCanvas = canvas.nativeCanvas
var text = 3
for (index in 0..330 step 30) {
val centerX = (innerCircleR * cos(index * Math.PI / 180)).toFloat()
val centerY = (innerCircleR * sin(index * Math.PI / 180)).toFloat()
nativeCanvas.drawText(
text.toString(),
centerX, centerY + 25f, textPaint
)
text++
if (text > 12) {
text = 1
}
}
}
val hourLength = innerCircleR * 0.6f
val hourAngle = (hour.value.plus(minute.value.div(60)).minus(3)).div(12) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(hourLength * cos(hourAngle * Math.PI / 180)).toFloat(),
(hourLength * sin(hourAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 8f,
cap = StrokeCap.Round
)
val minuteLength = innerCircleR * 0.9f
val minuteAngle =
(minute.value.plus(second.value.div(60)).minus(15)).div(60) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(minuteLength * cos(minuteAngle * Math.PI / 180)).toFloat(),
(minuteLength * sin(minuteAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 6f,
cap = StrokeCap.Round
)
val secondLength = outCircleR * 0.9f
val secondAngle = second.value.div(60) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(secondLength * cos(secondAngle * Math.PI / 180)).toFloat(),
(secondLength * sin(secondAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 2f,
cap = StrokeCap.Round
)
}
}
}
@Composable
fun Clock() {
val hour = remember {
mutableStateOf(0f)
}
val minute = remember {
mutableStateOf(0f)
}
val second = remember {
mutableStateOf(0f)
}
LaunchedEffect(key1 = "") {
while (true) {
val cal = Calendar.getInstance()
hour.value = cal.get(Calendar.HOUR).toFloat()
minute.value = cal.get(Calendar.MINUTE).toFloat()
second.value = cal.get(Calendar.SECOND).toFloat()
delay(1000)
}
}
val diameter = 200.dp
val color1 = Color.Black
Canvas(
modifier = Modifier
.size(diameter, diameter)
) {
val outCircleR = size.width / 2
val innerCircleR = (size.width / 2 * 0.8).toFloat()
drawCircle(color = color1, radius = size.width * 0.02f)
drawCircle(color = color1, radius = outCircleR, style = Stroke(4f))
drawCircle(
color = color1,
radius = innerCircleR,
style = Stroke(2f)
)
translate(size.width / 2, size.height / 2) {
for (index in 0..330 step 30) {
drawLine(
color = color1,
start = Offset(
(innerCircleR * 1.05f * cos(index * Math.PI / 180)).toFloat(),
(innerCircleR * 1.05f * sin(index * Math.PI / 180)).toFloat()
),
end = Offset(
(outCircleR * 0.95f * cos(index * Math.PI / 180)).toFloat(),
(outCircleR * 0.95f * sin(index * Math.PI / 180)).toFloat()
),
strokeWidth = 8f,
cap = StrokeCap.Round
)
}
val hourLength = innerCircleR * 0.6f
val hourAngle = (hour.value.plus(minute.value.div(60)).minus(3)).div(12) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(hourLength * cos(hourAngle * Math.PI / 180)).toFloat(),
(hourLength * sin(hourAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 8f,
cap = StrokeCap.Round
)
val minuteLength = innerCircleR * 0.9f
val minuteAngle =
(minute.value.plus(second.value.div(60)).minus(15)).div(60) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(minuteLength * cos(minuteAngle * Math.PI / 180)).toFloat(),
(minuteLength * sin(minuteAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 6f,
cap = StrokeCap.Round
)
val secondLength = outCircleR * 0.9f
val secondAngle = second.value.div(60) * 360
drawLine(
color = color1,
start = Offset.Zero,
end = Offset(
(secondLength * cos(secondAngle * Math.PI / 180)).toFloat(),
(secondLength * sin(secondAngle * Math.PI / 180)).toFloat()
),
strokeWidth = 2f,
cap = StrokeCap.Round
)
}
}
}
}