Junior — Middle
How to implement animation effects when using Jetpack Compose?
sobes.tech AI
Answer from AI
In Jetpack Compose, animation effects are implemented using built-in APIs that allow smooth changes of state values and creation of transitions. The main tools are:
animate*AsState— for animating individual values (e.g., color, size, position).updateTransition— for animating between multiple states.AnimatedVisibility— for animating the appearance and disappearance of elements.
Example of color change animation on click:
var selected by remember { mutableStateOf(false) }
val color by animateColorAsState(if (selected) Color.Red else Color.Gray)
Box(
modifier = Modifier
.size(100.dp)
.background(color)
.clickable { selected = !selected }
)
Thus, changing the state, Compose automatically animates the transitions, making the interface more lively and responsive.