Skip to content

Instantly share code, notes, and snippets.

@titoaesj
Last active December 2, 2021 12:33
Show Gist options
  • Save titoaesj/fd89d09ccfac01130bca85b109261693 to your computer and use it in GitHub Desktop.
Save titoaesj/fd89d09ccfac01130bca85b109261693 to your computer and use it in GitHub Desktop.
Compose component animate text crescent Integer 0..N - [Android]
@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun AnimatedIntValue(
targetValue: Int,
delayOfDigits: Long = 100L
) {
// create variable for current time
var currentValue by remember {
mutableStateOf(0)
}
LaunchedEffect(key1 = currentValue) {
if (currentValue < targetValue) {
delay(delayOfDigits)
currentValue += 1
}
}
AnimatedContent(targetState = currentValue,
transitionSpec = {
if (targetState > initialState) {
slideInVertically({ height -> height }) + fadeIn() with
slideOutVertically({ height -> -height }) + fadeOut()
} else {
slideInVertically({ height -> -height }) + fadeIn() with
slideOutVertically({ height -> height }) + fadeOut()
}.using(SizeTransform(clip = false))
}
) { targetCount ->
Text(
targetCount.toString(),
style = MaterialTheme.typography.h4,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment