How to animate a background to a target value and back in Android Compose

Viewed 759

I'm probably missing something very obvious, but anyway. Say, I want to animate a background color like this: Grey -> Red -> Grey. However the function animateColorAsState does not seem to allow such a transition. What is the canonical way in Android Compose to make it happen?

1 Answers

Here's how you can do it with Animatable:

val color = remember { Animatable(Color.Gray) }
LaunchedEffect(Unit) {
    color.animateTo(Color.Red, animationSpec = tween(1000))
    color.animateTo(Color.Gray, animationSpec = tween(1000))
}
Box(Modifier.fillMaxSize().background(color.value))
Related