Fire-and-forget animation function for [Float].
AlphaAnimationSample
@Composable
fun AlphaAnimationSample() {
@Composable
fun alphaAnimation(visible: Boolean) {
// Animates to 1f or 0f based on [visible].
// This [animateState] returns a State<Float> object. The value of the State object is
// being updated by animation. (This method is overloaded for different parameter types.)
// Here we use the returned [State] object as a property delegate.
val alpha: Float by animateFloatAsState(if (visible) 1f else 0f)
// Updates the alpha of a graphics layer with the float animation value. It is more
// performant to modify alpha in a graphics layer than using `Modifier.alpha`. The former
// limits the invalidation scope of alpha change to graphicsLayer's draw stage (i.e. no
// recomposition would be needed). The latter triggers recomposition on each animation
// frame.
Box(modifier = Modifier.graphicsLayer { this.alpha = alpha }.background(Color.Red))
}
}