Crossfade

Functions summary

Unit
@Composable
<T : Any?> Crossfade(
    targetState: T,
    modifier: Modifier,
    animationSpec: FiniteAnimationSpec<Float>,
    label: String,
    content: @Composable (T) -> Unit
)

Crossfade allows to switch between two layouts with a crossfade animation.

Cmn
Unit
@ExperimentalAnimationApi
@Composable
<T : Any?> Transition<T>.Crossfade(
    modifier: Modifier,
    animationSpec: FiniteAnimationSpec<Float>,
    contentKey: (targetState) -> Any?,
    content: @Composable (targetState) -> Unit
)

Crossfade allows to switch between two layouts with a crossfade animation.

Cmn

Functions

Crossfade

@Composable
fun <T : Any?> Crossfade(
    targetState: T,
    modifier: Modifier = Modifier,
    animationSpec: FiniteAnimationSpec<Float> = tween(),
    label: String = "Crossfade",
    content: @Composable (T) -> Unit
): Unit

Crossfade allows to switch between two layouts with a crossfade animation.

import androidx.compose.animation.Crossfade
import androidx.compose.material.Text

Crossfade(targetState = "A") { screen ->
    when (screen) {
        "A" -> Text("Page A")
        "B" -> Text("Page B")
    }
}
Parameters
targetState: T

is a key representing your target layout state. Every time you change a key the animation will be triggered. The content called with the old key will be faded out while the content called with the new key will be faded in.

modifier: Modifier = Modifier

Modifier to be applied to the animation container.

animationSpec: FiniteAnimationSpec<Float> = tween()

the AnimationSpec to configure the animation.

label: String = "Crossfade"

An optional label to differentiate from other animations in Android Studio.

content: @Composable (T) -> Unit

A mapping from a given state to the content corresponding to that state.

Transition.Crossfade

@ExperimentalAnimationApi
@Composable
fun <T : Any?> Transition<T>.Crossfade(
    modifier: Modifier = Modifier,
    animationSpec: FiniteAnimationSpec<Float> = tween(),
    contentKey: (targetState) -> Any? = { it },
    content: @Composable (targetState) -> Unit
): Unit

Crossfade allows to switch between two layouts with a crossfade animation. The target state of this Crossfade will be the target state of the given Transition object. In other words, when the Transition changes target, the Crossfade will fade in the target content while fading out the current content.

content is a mapping between the state and the composable function for the content of that state. During the crossfade, content lambda will be invoked multiple times with different state parameter such that content associated with different states will be fading in/out at the same time.

contentKey will be used to perform equality check for different states. For example, when two states resolve to the same content key, there will be no animation for that state change. By default, contentKey is the same as the state object. contentKey can be particularly useful if target state object gets recreated across save & restore while a more persistent key is needed to properly restore the internal states of the content.

Parameters
modifier: Modifier = Modifier

Modifier to be applied to the animation container.

animationSpec: FiniteAnimationSpec<Float> = tween()

the AnimationSpec to configure the animation.

contentKey: (targetState) -> Any? = { it }

A mapping from a given state to an object of Any.

content: @Composable (targetState) -> Unit

A mapping from a given state to the content corresponding to that state.