2.4 Animations
Animations are crucial in providing a smooth and engaging experience for the users. Jetpack Compose provides an extensive list of APIs that can be used to implement different animation in an app. Many Jetpack Compose animation APIs can be used as composable functions like other layout and ui elements.
AnimatedVisibility
The AnimatedVisibility
composable animates the appearance and dissapearane of elements. By default, the enter transition is fading in and expanding, and the exit transition is fading out and shrinking. The transition can be customized by specifying EnterTransition
and ExitTransition
.
EnterTransition
examples: fadeIn
, slideIn
, slideInHorizontally
, slideInVertically
, scaleIn
, expandIn
, expandHorizontally
, expandVertically
ExitTransition
examples: fadeOut
, slideOut
, slideOutHorizontally
, slideOutVertically
, scaleOut
, expandOut
, shrinkHorizontally
, shrinkVertically
Transitions can be combined using a +
operator in between EnterTransition
or ExitTransition
objects.
Content within AnimatedVisibility
, direct or indirect children, can use the animateEnterExit
modifier to specify a different animation behavior for each child. The visual effect for each child is a combonation of the animation specified in the AnimatedVisibility
composable and the child's own animation.
If each child wants its own distinct animation, AnimatedVisibility can apply no animations by specifying EnterTransition.None
and ExitTransition.None
in the AnimatedVisibility composable.
animate*AsState
The animate*AsState
functions are the easiest to animate a single value. The function takes in an end value, and the API starts the animation from the current value to the end value.
Jetpack Compose provides animate*AsState
functions for Float
, Color
, DP
, Size
, Offset
, Rect
, Int
, IntOffset
, and IntSize
. Other data types can be supported by providing a TwoWayConverter
to animateValueAsState
that takes a generic type.
Customize Animations
Most animation APIs allow developers to cusomize the animation by an optional AnimationSpec
parameter. Dependent on the animation type, the AnimationSpec
will also be different.
Last updated