Animatable
class Animatable<T, V : AnimationVector>
kotlin.Any | |
↳ | androidx.compose.animation.core.Animatable |
Animatable is a value holder that automatically animates its value when the value is changed via animateTo. If animateTo is invoked during an ongoing value change animation, a new animation will transition Animatable from its current value (i.e. value at the point of interruption) to the new targetValue. This ensures that the value change is always continuous using animateTo. If a spring animation (e.g. default animation) is used with animateTo, the velocity change will guarantee to be continuous as well.
Unlike AnimationState, Animatable ensures mutual exclusiveness on its animations. To achieve this, when a new animation is started via animateTo (or animateDecay), any ongoing animation will be canceled via a CancellationException.
import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.spring import androidx.compose.foundation.background import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.size import androidx.compose.material.Text import androidx.compose.runtime.remember import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.unit.IntOffset // Creates an `Animatable` to animate Offset and `remember` it. val animatedOffset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) } Box( Modifier.fillMaxSize().background(Color(0xffb99aff)).pointerInput(Unit) { coroutineScope { while (true) { val offset = awaitPointerEventScope { awaitFirstDown().position } // Launch a new coroutine for animation so the touch detection thread is not // blocked. launch { // Animates to the pressed position, with the given animation spec. animatedOffset.animateTo( offset, animationSpec = spring(stiffness = Spring.StiffnessLow) ) } } } } ) { Text("Tap anywhere", Modifier.align(Alignment.Center)) Box( Modifier .offset { // Use the animated offset as the offset of the Box. IntOffset( animatedOffset.value.x.roundToInt(), animatedOffset.value.y.roundToInt() ) } .size(40.dp) .background(Color(0xff3c1361), CircleShape) ) }
Summary
Public constructors | |
---|---|
<init>(initialValue: T, typeConverter: TwoWayConverter<T, V>, visibilityThreshold: T? = null) Animatable is a value holder that automatically animates its value when the value is changed via animateTo. |
Public methods | |
---|---|
suspend AnimationResult<T, V> |
animateDecay(initialVelocity: T, animationSpec: DecayAnimationSpec<T>, block: Animatable<T, V>.() -> Unit = null) Start a decay animation (i. |
suspend AnimationResult<T, V> |
animateTo(targetValue: T, animationSpec: AnimationSpec<T> = defaultSpringSpec, initialVelocity: T = velocity, block: Animatable<T, V>.() -> Unit = null) Starts an animation to animate from value to the provided targetValue. |
State<T> |
asState() Returns a State representing the current value of this animation. |
suspend Unit |
snapTo(targetValue: T) Sets the current value to the target value, without any animation. |
suspend Unit |
stop() Stops any on-going animation with a CancellationException. |
Unit |
updateBounds(lowerBound: T? = this.lowerBound, upperBound: T? = this.upperBound) Updates either lowerBound or upperBound, or both. |
Properties | |
---|---|
Boolean |
Indicates whether the animation is running. |
T? |
Lower bound of the animation, null by default (meaning no lower bound). |
T |
The target of the current animation. |
TwoWayConverter<T, V> |
A two-way converter that converts the given type T from and to AnimationVector |
T? |
Upper bound of the animation, null by default (meaning no upper bound). |
T |
Current value of the animation. |
T |
Returns the velocity, converted from velocityVector. |
V |
Velocity vector of the animation (in the form of AnimationVector. |
Public constructors
<init>
Animatable(
initialValue: T,