Short answer: Compose animations are state-driven. You change a value, and Compose animates the change. Use animate*AsState for a single value, AnimatedVisibility to show or hide a composable, AnimatedContent or Crossfade to switch content, animateContentSize for size changes, and Animatable for full control.
1. animate*AsState: animate one value
@Composable
fun AnimatedBox() {
var expanded by remember { mutableStateOf(false) }
val size by animateDpAsState(
targetValue = if (expanded) 200.dp else 100.dp,
animationSpec = tween(durationMillis = 400),
label = "size"
)
val color by animateColorAsState(
targetValue = if (expanded) Color(0xFF66BB6A) else Color(0xFF42A5F5),
label = "color"
)
Box(
modifier = Modifier
.size(size)
.background(color)
.clickable { expanded = !expanded }
)
}
Compose has similar functions for floats, colors, dp, offsets and more. Add import androidx.compose.runtime.getValue and setValue if by remember reports an error.
2. AnimatedVisibility: show and hide
@Composable
fun VisibilityDemo() {
var visible by remember { mutableStateOf(true) }
Column(Modifier.padding(16.dp)) {
Button(onClick = { visible = !visible }) { Text("Toggle") }
AnimatedVisibility(
visible = visible,
enter = fadeIn() + slideInVertically(),
exit = fadeOut() + slideOutVertically()
) {
Text("Hello, I animate in and out", style = MaterialTheme.typography.titleLarge)
}
}
}
Combine effects with +. Other options include expandVertically, shrinkVertically, scaleIn and slideInHorizontally.
3. animateContentSize: smooth size changes
var expanded by remember { mutableStateOf(false) }
Text(
text = "A long description that can be expanded or collapsed...",
maxLines = if (expanded) Int.MAX_VALUE else 2,
modifier = Modifier
.animateContentSize()
.clickable { expanded = !expanded }
)
This one modifier animates a text block growing and shrinking, with no extra state.
4. AnimatedContent and Crossfade: switch content
@Composable
fun CounterDemo() {
var count by remember { mutableStateOf(0) }
Column(Modifier.padding(16.dp)) {
Button(onClick = { count++ }) { Text("Add") }
AnimatedContent(targetState = count, label = "counter") { value ->
Text("Count: " + value, style = MaterialTheme.typography.headlineMedium)
}
}
}
Use Crossfade(targetState = screen, label = "screen") { current -> ... } when you only need a fade between two pieces of content.
5. Infinite animations
@Composable
fun PulsingDot() {
val transition = rememberInfiniteTransition(label = "pulse")
val scale by transition.animateFloat(
initialValue = 1f,
targetValue = 1.3f,
animationSpec = infiniteRepeatable(
animation = tween(800),
repeatMode = RepeatMode.Reverse
),
label = "scale"
)
Box(
Modifier
.size(40.dp)
.graphicsLayer(scaleX = scale, scaleY = scale)
.background(Color.Red, CircleShape)
)
}
6. Animatable: full control
Use Animatable when you want to start an animation from an event or run several in sequence:
@Composable
fun ShakeButton() {
val offsetX = remember { Animatable(0f) }
val scope = rememberCoroutineScope()
Button(
onClick = {
scope.launch {
offsetX.animateTo(20f, tween(80))
offsetX.animateTo(-20f, tween(80))
offsetX.animateTo(0f, tween(80))
}
},
modifier = Modifier.offset { IntOffset(offsetX.value.toInt(), 0) }
) { Text("Shake") }
}
Choose an animation spec
| Spec | What it does |
|---|---|
tween(durationMillis, easing) |
Fixed duration with easing |
spring(dampingRatio, stiffness) |
Natural, physics-based bounce (default for many APIs) |
keyframes { ... } |
Different values at specific times |
infiniteRepeatable(...) |
Repeats an animation forever |
Common mistakes
- Forgetting the
labelparameter. It is not required for the animation to work, but it makes animations easier to debug in Android Studio’s tools. - Reading animated values too high up. Reading an animated state in the parent recomposes the whole subtree on every frame. Read it as late as possible, for example inside
graphicsLayer { }orModifier.offset { }. - Using
delayloops to animate. Use the animation APIs, which run on the frame clock. - Animating layout size with state in a lazy list. Prefer
animateItemoranimateContentSize.
FAQ
How do I animate a value in Jetpack Compose?
Use animate*AsState, for example animateDpAsState or animateColorAsState, with a target value that depends on state.
How do I animate a composable appearing and disappearing?
Wrap it in AnimatedVisibility and set enter and exit transitions.
What is the difference between animate*AsState and Animatable?
animate*AsState is the simple, declarative API. Animatable gives you imperative control inside a coroutine.
How do I repeat an animation forever?
Use rememberInfiniteTransition with infiniteRepeatable.
More: Jetpack Compose tutorials and LazyColumn lists.