Transition

@Stable
sealed class Transition<S>

Transition manages all the child animations on a state level. Child animations can be created in a declarative way using Transition.animateFloat, Transition.animateValue, androidx.compose.animation.animateColor etc. When the targetState changes, Transition will automatically start or adjust course for all its child animations to animate to the new target values defined for each animation.

After arriving at targetState, Transition will be triggered to run if any child animation changes its target value (due to their dynamic target calculation logic, such as theme-dependent values).

See also

Inheritors

Types

Link copied to clipboard
@RestrictTo(value = [RestrictTo.Scope.LIBRARY])
inner class DeferredAnimation<T, V : AnimationVector>

DeferredAnimation can be constructed using Transition.createDeferredAnimation during composition and initialized later. It is useful for animations, the target values for which are unknown at composition time (e.g. layout size/position, etc).

Link copied to clipboard
interface Segment<S>

Segment holds initialState and targetState, which are the beginning and end of a transition. These states will be used to obtain the animation spec that will be used for this transition from the child animations.

Link copied to clipboard
@Stable
inner class TransitionAnimationState<T, V : AnimationVector> : State<T>

Each animation created using animateFloat, animateDp, etc is represented as a TransitionAnimationState in Transition.

Properties

Link copied to clipboard
Link copied to clipboard

Current state of the transition. This will always be the initialState of the transition until the transition is finished. Once the transition is finished, currentState will be set to targetState. currentState is backed by a MutableState.

Link copied to clipboard

Used internally to know when a SeekableTransitionState is animating initial values after SeekableTransitionState.animateTo or SeekableTransitionState.seekTo has redirected a transition prior to it completing. This is important for knowing when child transitions must be maintained after a parent target state has changed, but the child target state hasn't changed.

Link copied to clipboard

Indicates whether there is any animation running in the transition.

Link copied to clipboard
@get:RestrictTo(value = [RestrictTo.Scope.LIBRARY])
var isSeeking: Boolean
Link copied to clipboard
Link copied to clipboard
@get:RestrictTo(value = [RestrictTo.Scope.LIBRARY])
val parentTransition: Transition<*>?
Link copied to clipboard

Pending target state of the transition. This is the state that the transition is waiting to animate to. It is non-null only when a deferred update is in progress.

Link copied to clipboard
@get:RestrictTo(value = [RestrictTo.Scope.LIBRARY])
@set:RestrictTo(value = [RestrictTo.Scope.LIBRARY])
var playTimeNanos: Long

Play time in nano-seconds. playTimeNanos is always non-negative. It starts from 0L at the beginning of the transition and increment until all child animations have finished.

Link copied to clipboard

segment contains the initial state and the target state of the currently on-going transition.

Link copied to clipboard

Target state of the transition. This will be read by all child animations to determine their most up-to-date target values.

Link copied to clipboard

Total duration of the Transition, accounting for all the animations and child transitions defined on the Transition.

Link copied to clipboard

List of child transitions in a Transition.

Functions

Link copied to clipboard
@Composable
inline fun <S> Transition<S>.animateFloat(noinline transitionSpec: @Composable Transition.Segment<S>.() -> FiniteAnimationSpec<Float> = { spring() }, label: String = "FloatAnimation", targetValueByState: @Composable (state: S) -> Float): State<Float>

Creates a Float animation as a part of the given Transition. This means the states of this animation will be managed by the Transition.

Link copied to clipboard
@Composable
inline fun <S> Transition<S>.animateInt(noinline transitionSpec: @Composable Transition.Segment<S>.() -> FiniteAnimationSpec<Int> = { spring(visibilityThreshold = 1) }, label: String = "IntAnimation", targetValueByState: @Composable (state: S) -> Int): State<Int>

Creates a Int animation as a part of the given Transition. This means the states of this animation will be managed by the Transition.

Link copied to clipboard
@Composable
inline fun <S, T, V : AnimationVector> Transition<S>.animateValue(typeConverter: TwoWayConverter<T, V>, noinline transitionSpec: @Composable Transition.Segment<S>.() -> FiniteAnimationSpec<T> = { spring() }, label: String = "ValueAnimation", targetValueByState: @Composable (state: S) -> T): State<T>

Creates an animation of type T as a part of the given Transition. This means the states of this animation will be managed by the Transition. typeConverter will be used to convert between type T and AnimationVector so that the animation system knows how to animate it.

Link copied to clipboard
@Composable
inline fun <S, T> Transition<S>.createChildTransition(label: String = "ChildTransition", transformToChildState: @Composable (parentState: S) -> T): Transition<T>

createChildTransition creates a child Transition based on the mapping between parent state to child state provided in transformToChildState. This serves the following purposes:

Link copied to clipboard
@RestrictTo(value = [RestrictTo.Scope.LIBRARY])
@Composable
fun <S, T, V : AnimationVector> Transition<S>.createDeferredAnimation(typeConverter: TwoWayConverter<T, V>, label: String = "DeferredAnimation"): Transition.DeferredAnimation<T, V>

This creates a Transition.DeferredAnimation, which will not animate until it is set up using Transition.DeferredAnimation.animate. Once the animation is set up, it will animate from the currentState to targetState. If the Transition has already arrived at its target state at the time when the animation added, there will be no animation.

@RestrictTo(value = [RestrictTo.Scope.LIBRARY_GROUP_PREFIX])
@JvmName(name = "seek")
fun setPlaytimeAfterInitialAndTargetStateEstablished(initialState: S, targetState: S, playTimeNanos: Long)

This allows tools to set the transition (between initial and target state) to a specific playTimeNanos.

Link copied to clipboard
open override fun toString(): String