Target based animation that animates from the given [initialValue] towards the [targetValue], with an optional [initialVelocity].
suspendAnimateFloatVariant
fun suspendAnimateFloatVariant() {
@Composable
fun InfiniteAnimationDemo() {
// Create a mutable state for alpha, and update it in the animation.
val alpha = remember { mutableStateOf(1f) }
LaunchedEffect(Unit) {
// Animate from 1f to 0f using an infinitely repeating animation
animate(
initialValue = 1f,
targetValue = 0f,
animationSpec =
infiniteRepeatable(animation = tween(1000), repeatMode = RepeatMode.Reverse),
) { value, /* velocity */ _ ->
// Update alpha mutable state with the current animation value
alpha.value = value
}
}
Box(Modifier.fillMaxSize()) {
Icon(
Icons.Filled.Favorite,
contentDescription = null,
modifier =
Modifier.align(Alignment.Center)
.graphicsLayer(scaleX = 3.0f, scaleY = 3.0f, alpha = alpha.value),
tint = Color.Red,
)
}
}
}