-
-
Save oas004/46620026b061495869c22e7cbf360ed9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class PreviewAnimationState(val target: Float) { | |
Start(0f), End(1f) | |
} | |
@WearSquareDevicePreview | |
@Composable | |
fun PreviewProgressAnimation() { | |
var progressState by remember { mutableStateOf(PreviewAnimationState.Start) } | |
val transition = updateTransition( | |
targetState = progressState, | |
label = "Square Progress Indicator" | |
) | |
val progress by transition.animateFloat( | |
label = "Progress", | |
targetValueByState = { it.target }, | |
transitionSpec = { | |
tween(durationMillis = 1000, easing = LinearEasing) | |
} | |
) | |
val cornerRadiusDp = 10.dp | |
Box(modifier = Modifier.size(300.dp)) { | |
SquareSegmentedProgressIndicator( | |
modifier = Modifier | |
.align(Alignment.Center) | |
.height(300.dp) | |
.width(300.dp) | |
.clickable { | |
progressState = if (progressState == PreviewAnimationState.Start) { | |
PreviewAnimationState.End | |
} else { | |
PreviewAnimationState.Start | |
} | |
}, | |
progress = progress, | |
trackSegments = previewProgressSections, | |
cornerRadiusDp = cornerRadiusDp, | |
paddingDp = 8.dp | |
) | |
Text( | |
modifier = Modifier.align(Alignment.Center), | |
text = "${(progress * 100).toInt()}%", | |
color = Color.White | |
) | |
val cornerRadiusPx: Float = with(LocalDensity.current) { cornerRadiusDp.toPx() } | |
Canvas(modifier = Modifier.fillMaxSize()) { | |
drawLine( | |
Color.LightGray, | |
Offset(size.width / 2, 0f), | |
Offset(size.width / 2, size.height), | |
strokeWidth = 0.2f | |
) | |
drawLine( | |
Color.LightGray, | |
Offset(0f, size.height / 2), | |
Offset(size.width, size.height / 2), | |
strokeWidth = 0.2f | |
) | |
drawLine( | |
Color.LightGray, | |
Offset(cornerRadiusPx, 0f), | |
Offset(cornerRadiusPx, size.height), | |
strokeWidth = 0.2f | |
) | |
drawLine( | |
Color.LightGray, | |
Offset(size.width - cornerRadiusPx, 0f), | |
Offset(size.width - cornerRadiusPx, size.height), | |
strokeWidth = 0.2f | |
) | |
drawLine( | |
Color.LightGray, | |
Offset(0f, cornerRadiusPx), | |
Offset(size.width, cornerRadiusPx), | |
strokeWidth = 0.2f | |
) | |
drawLine( | |
Color.LightGray, | |
Offset(0f, size.height - cornerRadiusPx), | |
Offset(size.width, size.height - cornerRadiusPx), | |
strokeWidth = 0.2f | |
) | |
} | |
} | |
LaunchedEffect(Unit) { | |
while (true) { | |
progressState = PreviewAnimationState.End | |
delay(1000) | |
progressState = PreviewAnimationState.Start | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment