Animation
Most animations in Compose follow one idea: you set where a value should end up, and Compose animates it there. When state changes, the animation starts by itself.
Example
var selected by remember { mutableStateOf(false) }
val size by animateDpAsState(if (selected) 64.dp else 48.dp, label = "size")
Box(Modifier.size(size).clickable { selected = !selected })
When selected changes, size moves smoothly from 48 to 64 dp, and the box recomposes with each step. See the State guide for how state works.
Which API to use
| You want to | Use |
|---|---|
| Animate one value: size, color, offset, alpha | animate*AsState |
| Show or hide something | AnimatedVisibility |
| Switch between two contents | AnimatedContent or Crossfade |
| Animate a size change from new content | Modifier.animateContentSize() |
| Move list items when the list changes | Modifier.animateItem() |
| Repeat forever, like a pulse | rememberInfiniteTransition() |
| Start, stop or follow a finger yourself | Animatable |
| Move an element from one screen to another | Shared elements, see LookaheadScope |
One value: animate*AsState
There is one for each common type: animateDpAsState, animateFloatAsState, animateColorAsState, animateIntAsState, animateOffsetAsState, animateSizeAsState.
val color by animateColorAsState(
if (selected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant,
label = "color",
)
label names the animation in Android Studio's animation inspector. It is optional.
How it moves: animation specs
animationSpec sets the timing:
// A fixed duration, with easing at both ends.
animateFloatAsState(target, animationSpec = tween(durationMillis = 300))
// A spring: no fixed duration, can bounce. The default for most animations.
animateDpAsState(target, animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy, // how much it bounces
stiffness = Spring.StiffnessLow, // how fast it moves
))
Springs feel natural, and handle interruptions well: if the target changes mid-way, they continue from the current speed. Prefer them over tween.
Material motion: MotionScheme
The theme has a MotionScheme with ready-made springs, so your animations match the components:
animateDpAsState(target, animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec())
| Spec | For |
|---|---|
fastSpatialSpec, defaultSpatialSpec, slowSpatialSpec |
Movement and size: position, bounds, shape. May bounce |
fastEffectsSpec, defaultEffectsSpec, slowEffectsSpec |
Color and alpha. Never overshoot |
Use spatial specs for where things are, and effects specs for how they look. Pick the speed by distance: small changes fast, large ones slow.
MotionScheme.standard() is calm; MotionScheme.expressive() is bouncier. MaterialExpressiveTheme uses the expressive one. See the Theming guide.
Show and hide: AnimatedVisibility
AnimatedVisibility(
visible = showDetails,
enter = fadeIn() + expandVertically(),
exit = fadeOut() + shrinkVertically(),
) {
Text("More details...")
}
Combine transitions with +. Others: slideInHorizontally, slideOutVertically, scaleIn, scaleOut. The content stays in the composition until the exit animation ends.
Change content: AnimatedContent and Crossfade
AnimatedContent(targetState = count, label = "count") { value ->
Text("$value")
}
Crossfade(targetState = isLoading, label = "fade") { loading ->
if (loading) CircularProgressIndicator() else Content()
}
Use the lambda's parameter (value, loading), not the outer state. During the animation, the old content is still shown with the old value.
Crossfade only fades. AnimatedContent can also slide and resize, with transitionSpec.
Size changes: animateContentSize
Column(Modifier.animateContentSize()) {
Text(if (expanded) longText else shortText)
}
When the content gets bigger or smaller, the size animates instead of jumping. Put it before size modifiers like padding in the chain.
Lists: animateItem
In a LazyColumn, add Modifier.animateItem() to each item. Items then fade in, fade out and slide to their new place when the list changes. Give items a key, or Compose can't tell which item moved. See the Longer Example on the LazyColumn page.
Repeating: rememberInfiniteTransition
val transition = rememberInfiniteTransition(label = "pulse")
val alpha by transition.animateFloat(
initialValue = 0.6f,
targetValue = 1f,
animationSpec = infiniteRepeatable(tween(800), RepeatMode.Reverse),
label = "pulse",
)
Runs as long as the composable is on screen. For loading states, Material's LoadingIndicator and CircularProgressIndicator already do this.
Full control: Animatable
Animatable is a value you animate from coroutines. Use it when the animation is started by code, or follows a gesture:
val offset = remember { Animatable(0f) }
LaunchedEffect(target) {
offset.animateTo(target) // suspends until done; cancelled if target changes
}
It also has snapTo (jump without animation) and stop.
Make it smooth
- Animate in
Modifier.graphicsLayer { }when you can:alpha,translationX,scaleX,rotationZ. Changes there only redraw; they don't measure and place the layout again. - Read fast-changing values in a lambda, like
Modifier.offset { IntOffset(x.roundToPx(), 0) }instead ofModifier.offset(x). The lambda form skips recomposition.
Summary
| You want | Write |
|---|---|
| Animate a value | val x by animateDpAsState(target) |
| Material timing | animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec() |
| Show and hide | AnimatedVisibility(visible) { } |
| Switch content | AnimatedContent(state) { s -> } |
| Smooth size changes | Modifier.animateContentSize() |
| Smooth list changes | Modifier.animateItem() with item keys |
| Pulse forever | rememberInfiniteTransition() |