AnimationState
-
Cmn
class AnimationState<T : Any?, V : AnimationVector> : State
AnimationState contains the necessary information to indicate the state of an animation. Once an AnimationState is constructed, it can only be updated/mutated by animations. If there's a need to mutate some of the fields of an AnimationState, consider using copy functions.
Summary
Public constructors |
|
|---|---|
<T : Any?, V : AnimationVector> AnimationState( |
Cmn
|
Public properties |
||
|---|---|---|
Long |
The time when the animation finished successfully in the System.nanoTime timebase. |
Cmn
|
Boolean |
Indicates whether the animation is currently running. |
Cmn
|
Long |
Last frame time of the animation. |
Cmn
|
TwoWayConverter<T, V> |
|
Cmn
|
open T |
Current value of the |
Cmn
|
T |
Velocity of type |
Cmn
|
V |
Current velocity vector of the |
Cmn
|
Extension functions |
||
|---|---|---|
AnimationState<Float, AnimationVector1D> |
AnimationState<Float, AnimationVector1D>.copy(Creates a new |
Cmn
|
AnimationState<T, V> |
<T : Any?, V : AnimationVector> AnimationState<T, V>.copy(Creates a new |
Cmn
|
suspend Unit |
<T : Any?, V : AnimationVector> AnimationState<T, V>.animateDecay(Decay animation that slows down from the current velocity and value captured in |
Cmn
|
suspend Unit |
<T : Any?, V : AnimationVector> AnimationState<T, V>.animateTo(Target based animation that takes the value and velocity from the |
Cmn
|
Extension properties |
||
|---|---|---|
Boolean |
AnimationState<*, *>.isFinishedIndicates whether the given |
Cmn
|
Public constructors
AnimationState
<T : Any?, V : AnimationVector> AnimationState(
typeConverter: TwoWayConverter<T, V>,
initialValue: T,
initialVelocityVector: V? = null,
lastFrameTimeNanos: Long = AnimationConstants.UnspecifiedTime,
finishedTimeNanos: Long = AnimationConstants.UnspecifiedTime,
isRunning: Boolean = false
)
| Parameters | |
|---|---|
typeConverter: TwoWayConverter<T, V> |
|
initialValue: T |
initial value of the |
initialVelocityVector: V? = null |
initial velocity of the |
lastFrameTimeNanos: Long = AnimationConstants.UnspecifiedTime |
last frame time of the animation, |
finishedTimeNanos: Long = AnimationConstants.UnspecifiedTime |
the time that the animation finished successfully, |
isRunning: Boolean = false |
whether the |
Public properties
finishedTimeNanos
val finishedTimeNanos: Long
The time when the animation finished successfully in the System.nanoTime timebase.
If the animation has never finished (i.e. currently running, interrupted, or never started), this will be AnimationConstants.UnspecifiedTime, unless specified otherwise in AnimationState constructor.
lastFrameTimeNanos
val lastFrameTimeNanos: Long
Last frame time of the animation.
If the animation has never started, this will be AnimationConstants.UnspecifiedTime, unless specified otherwise in the AnimationState constructor. lastFrameTimeNanos is the frame time when the animation is last updated, in the System.nanoTime timebase. It is also used for starting a sequential animation in AnimationState.animateTo. This allows the sequential animation to set its start time to when the previous animation is interrupted or finished.
typeConverter
val typeConverter: TwoWayConverter<T, V>
TwoWayConverter to convert type T from and to AnimationVector
Extension functions
copy
fun AnimationState<Float, AnimationVector1D>.copy(
value: Float = this.value,
velocity: Float = this.velocityVector.value,
lastFrameTimeNanos: Long = this.lastFrameTimeNanos,
finishedTimeNanos: Long = this.finishedTimeNanos,
isRunning: Boolean = this.isRunning
): AnimationState<Float, AnimationVector1D>
Creates a new AnimationState of Float value type from a given AnimationState of the same type. This function allows some of the fields to be different in the new AnimationState.
| Parameters | |
|---|---|
value: Float = this.value |
value of the |
velocity: Float = this.velocityVector.value |
velocity of the |
lastFrameTimeNanos: Long = this.lastFrameTimeNanos |
last frame time of the animation, same as the given |
finishedTimeNanos: Long = this.finishedTimeNanos |
the time that the animation finished successfully, same as the given |
isRunning: Boolean = this.isRunning |
whether the |
| Returns | |
|---|---|
AnimationState<Float, AnimationVector1D> |
A new |
copy
fun <T : Any?, V : AnimationVector> AnimationState<T, V>.copy(
value: T = this.value,
velocityVector: V? = this.velocityVector.copy(),
lastFrameTimeNanos: Long = this.lastFrameTimeNanos,
finishedTimeNanos: Long = this.finishedTimeNanos,
isRunning: Boolean = this.isRunning
): AnimationState<T, V>
Creates a new AnimationState from a given AnimationState. This function allows some of the fields to be different in the new AnimationState.
| Parameters | |
|---|---|
value: T = this.value |
value of the |
velocityVector: V? = this.velocityVector.copy() |
velocity of the |
lastFrameTimeNanos: Long = this.lastFrameTimeNanos |
last frame time of the animation, same as the given |
finishedTimeNanos: Long = this.finishedTimeNanos |
the time that the animation finished successfully, |
isRunning: Boolean = this.isRunning |
whether the |
| Returns | |
|---|---|
AnimationState<T, V> |
A new |
animateDecay
suspend fun <T : Any?, V : AnimationVector> AnimationState<T, V>.animateDecay(
animationSpec: DecayAnimationSpec<T>,
sequentialAnimation: Boolean = false,
block: AnimationScope<T, V>.() -> Unit = {}
): Unit
Decay animation that slows down from the current velocity and value captured in AnimationState until the velocity reaches 0. During the animation, the given AnimationState will be updated with the up-to-date value/velocity, frame time, etc. This is often used to animate the result of a fling gesture.
| Parameters | |
|---|---|
animationSpec: DecayAnimationSpec<T> |
Defines the decay animation that will be used for this animation. Some options for |
sequentialAnimation: Boolean = false |
Indicates whether the animation should use the |
block: AnimationScope<T, V>.() -> Unit = {} |
will be invoked on every frame during the animation, and the |
animateTo
suspend fun <T : Any?, V : AnimationVector> AnimationState<T, V>.animateTo(
targetValue: T,
animationSpec: AnimationSpec<T> = spring(),
sequentialAnimation: Boolean = false,
block: AnimationScope<T, V>.() -> Unit = {}
): Unit
Target based animation that takes the value and velocity from the AnimationState as the starting condition, and animate to the targetValue, using the animationSpec. During the animation, the given AnimationState will be updated with the up-to-date value/velocity, frame time, etc.
import androidx.compose.animation.core.AnimationState import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animate import androidx.compose.animation.core.animateTo import androidx.compose.animation.core.isFinished import androidx.compose.animation.core.spring import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember @Composable fun simpleAnimate(target: Float): Float { // Create an AnimationState to be updated by the animation. val animationState = remember { AnimationState(target) } // Launch the suspend animation into the composition's CoroutineContext, and pass // `target` to LaunchedEffect so that when`target` changes the old animation job is // canceled, and a new animation is created with a new target. LaunchedEffect(target) { // This starts an animation that updates the animationState on each frame animationState.animateTo( targetValue = target, // Use a low stiffness spring. This can be replaced with any type of `AnimationSpec` animationSpec = spring(stiffness = Spring.StiffnessLow), // If the previous animation was interrupted (i.e. not finished), configure the // animation as a sequential animation to continue from the time the animation was // interrupted. sequentialAnimation = !animationState.isFinished, ) // When the function above returns, the animation has finished. } // Return the value updated by the animation. return animationState.value }
| Parameters | |
|---|---|
targetValue: T |
The target value that the animation will animate to. |
animationSpec: AnimationSpec<T> = spring() |
The animation configuration that will be used. |
sequentialAnimation: Boolean = false |
Indicates whether the animation should use the |
block: AnimationScope<T, V>.() -> Unit = {} |
Will be invoked on every frame, and the |
Extension properties
isFinished
val AnimationState<*, *>.isFinished: Boolean
Indicates whether the given AnimationState is for an animation that has finished, indicated by AnimationState.finishedTimeNanos having a specified value.