Make number of lines dynamic and calculates the rotation degrees for each line drawn

pull/1431/head
JackEblan 1 year ago
parent 88c3eb0b90
commit 1cfcdbaf6f
No known key found for this signature in database
GPG Key ID: 14D6478AC2D9C323

@ -54,14 +54,16 @@ import kotlinx.coroutines.launch
fun NiaLoadingWheel( fun NiaLoadingWheel(
contentDesc: String, contentDesc: String,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
rotationTimeInMillis: Int = 12000,
numberOfLines: Int = 12,
) { ) {
val infiniteTransition = rememberInfiniteTransition(label = "wheel transition") val infiniteTransition = rememberInfiniteTransition(label = "wheel transition")
// Specifies the float animation for slowly drawing out the lines on entering // Specifies the float animation for slowly drawing out the lines on entering
val startValue = if (LocalInspectionMode.current) 0F else 1F val startValue = if (LocalInspectionMode.current) 0F else 1F
val floatAnimValues = (0 until NUM_OF_LINES).map { remember { Animatable(startValue) } } val floatAnimValues = (0 until numberOfLines).map { remember { Animatable(startValue) } }
LaunchedEffect(floatAnimValues) { LaunchedEffect(floatAnimValues) {
(0 until NUM_OF_LINES).map { index -> (0 until numberOfLines).map { index ->
launch { launch {
floatAnimValues[index].animateTo( floatAnimValues[index].animateTo(
targetValue = 0F, targetValue = 0F,
@ -80,7 +82,7 @@ fun NiaLoadingWheel(
initialValue = 0F, initialValue = 0F,
targetValue = 360F, targetValue = 360F,
animationSpec = infiniteRepeatable( animationSpec = infiniteRepeatable(
animation = tween(durationMillis = ROTATION_TIME, easing = LinearEasing), animation = tween(durationMillis = rotationTimeInMillis, easing = LinearEasing),
), ),
label = "wheel rotation animation", label = "wheel rotation animation",
) )
@ -89,18 +91,18 @@ fun NiaLoadingWheel(
val baseLineColor = MaterialTheme.colorScheme.onBackground val baseLineColor = MaterialTheme.colorScheme.onBackground
val progressLineColor = MaterialTheme.colorScheme.inversePrimary val progressLineColor = MaterialTheme.colorScheme.inversePrimary
val colorAnimValues = (0 until NUM_OF_LINES).map { index -> val colorAnimValues = (0 until numberOfLines).map { index ->
infiniteTransition.animateColor( infiniteTransition.animateColor(
initialValue = baseLineColor, initialValue = baseLineColor,
targetValue = baseLineColor, targetValue = baseLineColor,
animationSpec = infiniteRepeatable( animationSpec = infiniteRepeatable(
animation = keyframes { animation = keyframes {
durationMillis = ROTATION_TIME / 2 durationMillis = rotationTimeInMillis / 2
progressLineColor at ROTATION_TIME / NUM_OF_LINES / 2 using LinearEasing progressLineColor at rotationTimeInMillis / numberOfLines / 2 using LinearEasing
baseLineColor at ROTATION_TIME / NUM_OF_LINES using LinearEasing baseLineColor at rotationTimeInMillis / numberOfLines using LinearEasing
}, },
repeatMode = RepeatMode.Restart, repeatMode = RepeatMode.Restart,
initialStartOffset = StartOffset(ROTATION_TIME / NUM_OF_LINES / 2 * index), initialStartOffset = StartOffset(rotationTimeInMillis / numberOfLines / 2 * index),
), ),
label = "wheel color animation", label = "wheel color animation",
) )
@ -115,8 +117,8 @@ fun NiaLoadingWheel(
.semantics { contentDescription = contentDesc } .semantics { contentDescription = contentDesc }
.testTag("loadingWheel"), .testTag("loadingWheel"),
) { ) {
repeat(NUM_OF_LINES) { index -> repeat(numberOfLines) { index ->
rotate(degrees = index * 30f) { rotate(degrees = (360 / numberOfLines * index).toFloat()) {
drawLine( drawLine(
color = colorAnimValues[index].value, color = colorAnimValues[index].value,
// Animates the initially drawn 1 pixel alpha from 0 to 1 // Animates the initially drawn 1 pixel alpha from 0 to 1
@ -168,6 +170,3 @@ fun NiaOverlayLoadingWheelPreview() {
} }
} }
} }
private const val ROTATION_TIME = 12000
private const val NUM_OF_LINES = 12

Loading…
Cancel
Save