Animation Modifiers and Composables - Jetpack Compose - Android Developers
Animation Modifiers and Composables - Jetpack Compose - Android Developers
The AnimatedVisibility
(/reference/kotlin/androidx/compose/animation/package-
summary#AnimatedVisibility(kotlin.Boolean,androidx.compose.ui.Modifier,androidx.compose.animatio
n.EnterTransition,androidx.compose.animation.ExitTransition,kotlin.Function1))
composable animates the appearance and disappearance of its content.
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 1/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
AnimatedVisibility(visible) {
// your composable here
// ...
}
ippets/src/main/java/com/example/compose/snippets/animations/AnimationQuickGuide.kt#L133-L148)
By default, the content appears by fading in and expanding, and it disappears by fading
out and shrinking. The transition can be customized by specifying EnterTransition
(/reference/kotlin/androidx/compose/animation/EnterTransition) and ExitTransition
(/reference/kotlin/androidx/compose/animation/ExitTransition).
As you can see in the example above, you can combine multiple EnterTransition or
ExitTransition objects with a + operator, and each accepts optional parameters to
customize its behavior. See the references for more information.
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 2/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
EnterTransition
fadeIn (/reference/kotlin/androidx/compose/animation/package-summary#fadeIn(androidx.compose.
slideIn (/reference/kotlin/androidx/compose/animation/package-summary#slideIn(androidx.compose
slideInHorizontally
(/reference/kotlin/androidx/compose/animation/package-summary#slideInHorizontally(androidx.comp
slideInVertically
(/reference/kotlin/androidx/compose/animation/package-summary#slideInVertically(androidx.compose
scaleIn
(/reference/kotlin/androidx/compose/animation/package-
summary#scaleIn(androidx.compose.animation.core.FiniteAnimationSpec,kotlin.Float,androidx.compos
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 3/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
expandIn
(/reference/kotlin/androidx/compose/animation/package-
summary#expandIn(androidx.compose.animation.core.FiniteAnimationSpec,androidx.compose.ui.Align
expandHorizontally
(/reference/kotlin/androidx/compose/animation/package-
summary#expandHorizontally(androidx.compose.animation.core.FiniteAnimationSpec,androidx.compo
expandVertically
(/reference/kotlin/androidx/compose/animation/package-
summary#expandVertically(androidx.compose.animation.core.FiniteAnimationSpec,androidx.compose
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 4/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
AnimatedVisibility(
visible = visible,
enter = fadeIn(),
exit = fadeOut()
) {
// Fade in/out the background and the foreground.
Box(Modifier.fillMaxSize().background(Color.DarkGray)) {
Box(
Modifier
.align(Alignment.Center)
.animateEnterExit(
// Slide in/out the inner box.
enter = slideInVertically(),
exit = slideOutVertically()
)
.sizeIn(minWidth = 256.dp, minHeight = 64.dp)
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 5/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
.background(Color.Red)
) {
// Content of the notification…
}
}
}
/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L198-L221)
In some cases, you may want to have AnimatedVisibility apply no animations at all so
that children can each have their own distinct animations by animateEnterExit . To
achieve this, specify EnterTransition.None and ExitTransition.None at the
AnimatedVisibility composable.
AnimatedVisibility(
visible = visible,
enter = fadeIn(),
exit = fadeOut()
) { // this: AnimatedVisibilityScope
// Use AnimatedVisibilityScope#transition to add a custom animation
// to the AnimatedVisibility.
val background by transition.animateColor(label = "color") { state ->
if (state == EnterExitState.Visible) Color.Blue else Color.Gray
}
Box(modifier = Modifier.size(128.dp).background(background))
}
snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L230-L243)
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 6/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
Row {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Add")
}
AnimatedContent(targetState = count) { targetCount ->
// Make sure to use `targetCount`, not `count`.
Text(text = "Count: $targetCount")
}
}
snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L267-L276)
Note that you should always use the lambda parameter and reflect it to the content. The
API uses this value as the key to identify the content that's currently shown.
By default, the initial content fades out and then the target content fades in (this
behavior is called fade through
(https://material.io/design/motion/the-motion-system.html#fade-through)). You can customize this
animation behavior by specifying a ContentTransform
(/reference/kotlin/androidx/compose/animation/ContentTransform) object to the transitionSpec
parameter. You can create ContentTransform by combining an EnterTransition
(/reference/kotlin/androidx/compose/animation/EnterTransition) with an ExitTransition
(/reference/kotlin/androidx/compose/animation/ExitTransition) using the with infix function. You
can apply SizeTransform
(/reference/kotlin/androidx/compose/animation/package-
summary#SizeTransform(kotlin.Boolean,kotlin.Function2))
to the ContentTransform by attaching it with the using infix function.
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 7/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
AnimatedContent(
targetState = count,
transitionSpec = {
// Compare the incoming number with the previous number.
if (targetState > initialState) {
// If the target number is larger, it slides up and fades in
// while the initial (smaller) number slides up and fades out.
slideInVertically { height -> height } + fadeIn() with
slideOutVertically { height -> -height } + fadeOut()
} else {
// If the target number is smaller, it slides down and fades in
// while the initial number slides down and fades out.
slideInVertically { height -> -height } + fadeIn() with
slideOutVertically { height -> height } + fadeOut()
}.using(
// Disable clipping since the faded slide-in/out should
// be displayed out of bounds.
SizeTransform(clip = false)
)
}
) { targetCount ->
Text(text = "$targetCount")
}
snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L284-L306)
EnterTransition defines how the target content should appear, and ExitTransition
defines how the initial content should disappear. In addition to all of the
EnterTransition and ExitTransition functions available for AnimatedVisibility ,
AnimatedContent offers slideIntoContainer
(/reference/kotlin/androidx/compose/animation/AnimatedContentScope#slideIntoContainer(androidx.
compose.animation.AnimatedContentScope.SlideDirection,androidx.compose.animation.core.FiniteAni
mationSpec,kotlin.Function1))
and slideOutOfContainer
(/reference/kotlin/androidx/compose/animation/AnimatedContentScope#slideOutOfContainer(android
x.compose.animation.AnimatedContentScope.SlideDirection,androidx.compose.animation.core.FiniteA
nimationSpec,kotlin.Function1))
. These are convenient alternatives to slideInHorizontally/Vertically and
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 8/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 9/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 10/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L363-L369)
Note: The ordering of where animateContentSize is placed in your modifier chain matters. For
smooth animations, ensure you place it before any size modifiers such as size or defaultMinSize to
ensure that animateContentSize reports the animated value change to the Layout.
.animateContentSize()
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 11/12
8/9/24, 10:30 AM Animation modifiers and composables | Jetpack Compose | Android Developers
ppets/src/main/java/com/example/compose/snippets/animations/AnimationQuickGuide.kt#L260-L275)
Previous
arrow_back Quick guide (/develop/ui/compose/animation/quick-guide)
Next
Value-based animations (/develop/ui/compose/animation/value-based) arrow_forward
Content and code samples on this page are subject to the licenses described in the Content License
(/license). Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://developer.android.com/develop/ui/compose/animation/composables-modifiers 12/12