InterruptionAwareAnimationSpec


A FiniteAnimationSpec that uses animationSpec for uninterrupted animations and switches to interruptionSpec when a Transition, animateValueAsState, or Animatable.animateTo animation is interrupted mid-flight.

When a Transition is interrupted mid-flight, most FiniteAnimationSpec implementations (such as TweenSpec or KeyframesSpec, which ignore initial velocity) fall back to a default SpringSpec to avoid abrupt velocity discontinuities. Wrapping a spec in InterruptionAwareAnimationSpec overrides this fallback so Transition instead uses interruptionSpec (which defaults to animationSpec) and passes the current in-flight value and velocity to it. Similarly, when used with Animatable.animateTo or animate*AsState APIs (such as animateFloatAsState or animateValueAsState), animationSpec is used when starting from rest and interruptionSpec is used when the target value changes while an animation is already in-flight.

import androidx.compose.animation.core.InterruptionAwareAnimationSpec
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

// Use a 300ms tween for regular transitions, and a shorter 100ms tween when the transition
// is interrupted mid-flight instead of falling back to a default spring.
val transition = updateTransition(targetState = expanded, label = "BoxExpansion")
val size by
    transition.animateDp(
        transitionSpec = {
            InterruptionAwareAnimationSpec(
                animationSpec = tween(durationMillis = 300),
                interruptionSpec = tween(durationMillis = 100),
            )
        },
        label = "Size",
    ) { isExpanded ->
        if (isExpanded) 120.dp else 48.dp
    }
Box(Modifier.size(size).background(Color.Blue))

Summary

Public constructors

<T : Any?> InterruptionAwareAnimationSpec(
    animationSpec: FiniteAnimationSpec<T>,
    interruptionSpec: FiniteAnimationSpec<T>
)
Cmn

Public functions

open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open VectorizedFiniteAnimationSpec<V>
<V : AnimationVector> vectorize(converter: TwoWayConverter<T, V>)

Creates a VectorizedAnimationSpec with the given TwoWayConverter.

Cmn

Public properties

FiniteAnimationSpec<T>

the FiniteAnimationSpec used when starting an animation from rest.

Cmn
FiniteAnimationSpec<T>

the FiniteAnimationSpec used when an animation is interrupted mid-flight.

Cmn

Public constructors

InterruptionAwareAnimationSpec

<T : Any?> InterruptionAwareAnimationSpec(
    animationSpec: FiniteAnimationSpec<T>,
    interruptionSpec: FiniteAnimationSpec<T> = animationSpec
)

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

vectorize

open fun <V : AnimationVector> vectorize(converter: TwoWayConverter<T, V>): VectorizedFiniteAnimationSpec<V>

Creates a VectorizedAnimationSpec with the given TwoWayConverter.

The underlying animation system operates on AnimationVectors. T will be converted to AnimationVector to animate. VectorizedAnimationSpec describes how the converted AnimationVector should be animated. E.g. The animation could simply interpolate between the start and end values (i.e.TweenSpec), or apply spring physics to produce the motion (i.e. SpringSpec), etc)

Parameters
converter: TwoWayConverter<T, V>

converts the type T from and to AnimationVector type

Public properties

animationSpec

val animationSpec: FiniteAnimationSpec<T>

the FiniteAnimationSpec used when starting an animation from rest.

interruptionSpec

val interruptionSpec: FiniteAnimationSpec<T>

the FiniteAnimationSpec used when an animation is interrupted mid-flight. Defaults to animationSpec.