| <html devsite="true"> |
| <head> |
| <title>SuspendAnimationKt</title> |
| {% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} |
| {% include "_shared/_reference-head-tags.html" %} |
| </head> |
| <body> |
| <h1>SuspendAnimationKt</h1> |
| <p> |
| <pre>public final class <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html">SuspendAnimationKt</a></pre> |
| </p> |
| <hr> |
| <h2>Summary</h2> |
| <div class="devsite-table-wrapper"> |
| <table class="responsive"> |
| <colgroup> |
| <col width="40%"> |
| <col> |
| </colgroup> |
| <thead> |
| <tr> |
| <th colspan="100%"><h3>Public methods</h3></th> |
| </tr> |
| </thead> |
| <tbody class="list"> |
| <tr> |
| <td><code>static final void</code></td> |
| <td> |
| <div><code><a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</a>(<br> float initialValue,<br> float targetValue,<br> float initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</code></div> |
| <p>Target based animation that animates from the given <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialValue</a></code> towards the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">targetValue</a></code>, with an optional <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialVelocity</a></code>.</p> |
| </td> |
| </tr> |
| <tr> |
| <td><code>static final void</code></td> |
| <td> |
| <div><code><T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/TwoWayConverter.html">TwoWayConverter</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> typeConverter,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T initialValue,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T targetValue,<br> T initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> value, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> velocity, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</code></div> |
| <p>Target based animation for animating any data type <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">T</a></code>, so long as <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">T</a></code> can be converted to an <code><a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a></code> using <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">typeConverter</a></code>.</p> |
| </td> |
| </tr> |
| <tr> |
| <td><code>static final void</code></td> |
| <td> |
| <div><code><T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html">SuspendAnimationKt</a>.<a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">animateDecay</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> receiver,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/DecayAnimationSpec.html">DecayAnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> boolean sequentialAnimation,<br> @<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-extension-function-type/index.html">ExtensionFunctionType</a> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function1<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</code></div> |
| <p>Decay animation that slows down from the current velocity and value captured in <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> until the velocity reaches 0.</p> |
| </td> |
| </tr> |
| <tr> |
| <td><code>static final void</code></td> |
| <td> |
| <div><code><a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">animateDecay</a>(<br> float initialValue,<br> float initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/FloatDecayAnimationSpec.html">FloatDecayAnimationSpec</a> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</code></div> |
| <p>Decay animation that slows down from the given <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">initialVelocity</a></code> starting at <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">initialValue</a></code> until the velocity reaches 0.</p> |
| </td> |
| </tr> |
| <tr> |
| <td><code>static final void</code></td> |
| <td> |
| <div><code><T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html">SuspendAnimationKt</a>.<a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animateTo</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> receiver,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T targetValue,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> boolean sequentialAnimation,<br> @<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-extension-function-type/index.html">ExtensionFunctionType</a> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function1<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</code></div> |
| <p>Target based animation that takes the value and velocity from the <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> as the starting condition, and animate to the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">targetValue</a></code>, using the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animationSpec</a></code>.</p> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| <div class="list"> |
| <h2>Public methods</h2> |
| <div class="api-item"><a name="animate(kotlin.Float, kotlin.Float, kotlin.Float, androidx.compose.animation.core.AnimationSpec, kotlin.Function2)"></a><a name="animate-kotlin.Float-kotlin.Float-kotlin.Float-androidx.compose.animation.core.AnimationSpec-kotlin.Function2-"></a><a name="animate"></a> |
| <h3 class="api-name" id="animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</h3> |
| <pre class="api-signature no-pretty-print">public static final void <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</a>(<br> float initialValue,<br> float targetValue,<br> float initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</pre> |
| <p>Target based animation that animates from the given <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialValue</a></code> towards the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">targetValue</a></code>, with an optional <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialVelocity</a></code>. The <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialVelocity</a></code> defaults to 0f. By default, a <code><a href="/reference/androidx/compose/animation/core/package-summary.html#spring(kotlin.Float,kotlin.Float,kotlin.Any)">spring</a></code> will be used for the animation. An alternative <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animationSpec</a></code> can be provided to replace the default <code><a href="/reference/androidx/compose/animation/core/package-summary.html#spring(kotlin.Float,kotlin.Float,kotlin.Any)">spring</a></code>. On each frame, the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">block</a></code> will be invoked with up-to-date value and velocity.</p> |
| <p>This is a convenient method for Float animation. If there's a need to access more info related to the animation such as start time, target, etc, consider using <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">AnimationState.animateTo</a></code>. To animate non-<code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html">Float</a></code> data types, consider the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(kotlin.Float,kotlin.Float,kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</a></code> overload/variant for generic types.</p> |
| <pre class="prettyprint"> |
| import androidx.compose.animation.core.animate |
| import androidx.compose.animation.core.infiniteRepeatable |
| import androidx.compose.animation.core.tween |
| import androidx.compose.foundation.layout.Box |
| import androidx.compose.foundation.layout.fillMaxSize |
| import androidx.compose.material.Icon |
| import androidx.compose.runtime.LaunchedEffect |
| import androidx.compose.runtime.mutableStateOf |
| import androidx.compose.runtime.remember |
| import androidx.compose.ui.graphics.graphicsLayer |
| |
| @Composable |
| fun InfiniteAnimationDemo() { |
| // Create a mutable state for alpha, and update it in the animation. |
| val alpha = remember { mutableStateOf(1f) } |
| LaunchedEffect(Unit) { |
| // Animate from 1f to 0f using an infinitely repeating animation |
| animate( |
| initialValue = 1f, |
| targetValue = 0f, |
| animationSpec = infiniteRepeatable( |
| animation = tween(1000), |
| repeatMode = RepeatMode.Reverse |
| ) |
| ) { value, /* velocity */ _ -> |
| // Update alpha mutable state with the current animation value |
| alpha.value = value |
| } |
| } |
| Box(Modifier.fillMaxSize()) { |
| Icon( |
| Icons.Filled.Favorite, |
| contentDescription = null, |
| modifier = Modifier.align(Alignment.Center) |
| .graphicsLayer( |
| scaleX = 3.0f, |
| scaleY = 3.0f, |
| alpha = alpha.value |
| ), |
| tint = Color.Red |
| ) |
| } |
| }</pre> |
| <div class="devsite-table-wrapper"> |
| <table class="responsive"> |
| <colgroup> |
| <col width="40%"> |
| <col> |
| </colgroup> |
| <thead> |
| <tr> |
| <th colspan="100%">See also</th> |
| </tr> |
| </thead> |
| <tbody class="list"> |
| <tr> |
| <td><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animateTo</a></code></td> |
| <td></td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| </div> |
| <div class="api-item"><a name="animate(androidx.compose.animation.core.TwoWayConverter, kotlin.Any, kotlin.Any, kotlin.Any, androidx.compose.animation.core.AnimationSpec, kotlin.Function2)"></a><a name="animate-androidx.compose.animation.core.TwoWayConverter-kotlin.Any-kotlin.Any-kotlin.Any-androidx.compose.animation.core.AnimationSpec-kotlin.Function2-"></a><a name="animate"></a> |
| <h3 class="api-name" id="animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</h3> |
| <pre class="api-signature no-pretty-print">public static final void <T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animate</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/TwoWayConverter.html">TwoWayConverter</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> typeConverter,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T initialValue,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T targetValue,<br> T initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> value, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> velocity, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</pre> |
| <p>Target based animation for animating any data type <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">T</a></code>, so long as <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">T</a></code> can be converted to an <code><a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a></code> using <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">typeConverter</a></code>. The animation will start from the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialValue</a></code> and animate to the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">targetValue</a></code> value. The <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">initialVelocity</a></code> will be derived from an all-0 <code><a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a></code> unless specified. <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animate(androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Function2)">animationSpec</a></code> can be provided to create a specific look and feel for the animation. By default, a <code><a href="/reference/androidx/compose/animation/core/package-summary.html#spring(kotlin.Float,kotlin.Float,kotlin.Any)">spring</a></code> will be used.</p> |
| <p>This is a convenient method for target-based animation. If there's a need to access more info related to the animation such as start time, target, etc, consider using <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">AnimationState.animateTo</a></code>.</p> |
| <div class="devsite-table-wrapper"> |
| <table class="responsive"> |
| <colgroup> |
| <col width="40%"> |
| <col> |
| </colgroup> |
| <thead> |
| <tr> |
| <th colspan="100%">See also</th> |
| </tr> |
| </thead> |
| <tbody class="list"> |
| <tr> |
| <td><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animateTo</a></code></td> |
| <td></td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| </div> |
| <div class="api-item"><a name="(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec, kotlin.Boolean, kotlin.Function1)"></a><a name="-androidx.compose.animation.core.AnimationState-.animateDecay-androidx.compose.animation.core.DecayAnimationSpec-kotlin.Boolean-kotlin.Function1-"></a><a name="animatedecay"></a> |
| <h3 class="api-name" id="(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">SuspendAnimationKt.animateDecay</h3> |
| <pre class="api-signature no-pretty-print">public static final void <T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html">SuspendAnimationKt</a>.<a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">animateDecay</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> receiver,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/DecayAnimationSpec.html">DecayAnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> boolean sequentialAnimation,<br> @<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-extension-function-type/index.html">ExtensionFunctionType</a> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function1<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</pre> |
| <p>Decay animation that slows down from the current velocity and value captured in <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> until the velocity reaches 0. During the animation, the given <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> 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.</p> |
| <p><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">animationSpec</a></code> defines the decay animation that will be used for this animation. Some options for <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">animationSpec</a></code> include: <code><a href="/reference/androidx/compose/animation/package-summary.html#splineBasedDecay(androidx.compose.ui.unit.Density)">splineBasedDecay</a></code> and <code><a href="/reference/androidx/compose/animation/core/package-summary.html#exponentialDecay(kotlin.Float,kotlin.Float)">exponentialDecay</a></code>.</p> |
| <p>During the animation, <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> will be invoked on every frame, and the <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a></code> will be checked against cancellation before the animation continues. To cancel the animation from the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code>, simply call <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html#cancelAnimation()">AnimationScope.cancelAnimation</a></code>. After <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html#cancelAnimation()">AnimationScope.cancelAnimation</a></code> is called, <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> will not be invoked again. The animation loop will exit after the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> returns. All the animation related info can be accessed via <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a></code>.</p> |
| <p><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">sequentialAnimation</a></code> indicates whether the animation should use the <code><a href="/reference/androidx/compose/animation/core/AnimationState.html#lastFrameTimeNanos()">AnimationState.lastFrameTimeNanos</a></code> as the starting time (if true), or start in a new frame. By default, <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateDecay(androidx.compose.animation.core.DecayAnimationSpec,kotlin.Boolean,kotlin.Function1)">sequentialAnimation</a></code> is false, to start the animation in a few frame. In cases where an on-going animation is interrupted and a new animation is started to carry over the momentum, using the interruption time (captured in <code><a href="/reference/androidx/compose/animation/core/AnimationState.html#lastFrameTimeNanos()">AnimationState.lastFrameTimeNanos</a></code> creates a smoother animation.</p> |
| </div> |
| <div class="api-item"><a name="animateDecay(kotlin.Float, kotlin.Float, androidx.compose.animation.core.FloatDecayAnimationSpec, kotlin.Function2)"></a><a name="animateDecay-kotlin.Float-kotlin.Float-androidx.compose.animation.core.FloatDecayAnimationSpec-kotlin.Function2-"></a><a name="animatedecay"></a> |
| <h3 class="api-name" id="animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">animateDecay</h3> |
| <pre class="api-signature no-pretty-print">public static final void <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">animateDecay</a>(<br> float initialValue,<br> float initialVelocity,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/FloatDecayAnimationSpec.html">FloatDecayAnimationSpec</a> animationSpec,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function2<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Float.html">Float</a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</pre> |
| <p>Decay animation that slows down from the given <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">initialVelocity</a></code> starting at <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">initialValue</a></code> until the velocity reaches 0. This is often used after a fling gesture.</p> |
| <p><code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">animationSpec</a></code> defines the decay animation that will be used for this animation. Some options for this <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">animationSpec</a></code> include: <code><a href="/reference/androidx/compose/animation/package-summary.html#splineBasedDecay(androidx.compose.ui.unit.Density)">splineBasedDecay</a></code> and <code><a href="/reference/androidx/compose/animation/core/package-summary.html#exponentialDecay(kotlin.Float,kotlin.Float)">exponentialDecay</a></code>. <code><a href="/reference/androidx/compose/animation/core/package-summary.html#animateDecay(kotlin.Float,kotlin.Float,androidx.compose.animation.core.FloatDecayAnimationSpec,kotlin.Function2)">block</a></code> will be invoked on each animation frame with up-to-date value and velocity.</p> |
| <p>This is a convenient method for decay animation. If there's a need to access more info related to the animation such as start time, target, etc, consider using <code><a href="/reference/[JVM root]/package-summary.html">AnimationState.animateDecay</a></code>.</p> |
| <div class="devsite-table-wrapper"> |
| <table class="responsive"> |
| <colgroup> |
| <col width="40%"> |
| <col> |
| </colgroup> |
| <thead> |
| <tr> |
| <th colspan="100%">See also</th> |
| </tr> |
| </thead> |
| <tbody class="list"> |
| <tr> |
| <td><code><a href="/reference/[JVM root]/package-summary.html">[JVM root]</a></code></td> |
| <td> |
| <p><code><a href="/reference/[JVM root]/package-summary.html">AnimationState.animateDecay</a></code></p> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| </div> |
| <div class="api-item"><a name="(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any, androidx.compose.animation.core.AnimationSpec, kotlin.Boolean, kotlin.Function1)"></a><a name="-androidx.compose.animation.core.AnimationState-.animateTo-kotlin.Any-androidx.compose.animation.core.AnimationSpec-kotlin.Boolean-kotlin.Function1-"></a><a name="animateto"></a> |
| <h3 class="api-name" id="(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">SuspendAnimationKt.animateTo</h3> |
| <pre class="api-signature no-pretty-print">public static final void <T extends <a href="https://developer.android.com/reference/java/lang/Object.html">Object</a>, V extends <a href="/reference/androidx/compose/animation/core/AnimationVector.html">AnimationVector</a>> <a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html">SuspendAnimationKt</a>.<a href="/reference/androidx/compose/animation/core/SuspendAnimationKt.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animateTo</a>(<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V> receiver,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T targetValue,<br> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationSpec.html">AnimationSpec</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T> animationSpec,<br> boolean sequentialAnimation,<br> @<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-extension-function-type/index.html">ExtensionFunctionType</a> @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> Function1<@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a><@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T, @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> V>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>> block<br>)</pre> |
| <p>Target based animation that takes the value and velocity from the <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> as the starting condition, and animate to the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">targetValue</a></code>, using the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">animationSpec</a></code>. During the animation, the given <code><a href="/reference/androidx/compose/animation/core/AnimationState.html">AnimationState</a></code> will be updated with the up-to-date value/velocity, frame time, etc.</p> |
| <p><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">sequentialAnimation</a></code> indicates whether the animation should use the <code><a href="/reference/androidx/compose/animation/core/AnimationState.html#lastFrameTimeNanos()">AnimationState.lastFrameTimeNanos</a></code> as the starting time (if true), or start in a new frame. By default, <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">sequentialAnimation</a></code> is false, to start the animation in a few frame. In cases where an on-going animation is interrupted and a new animation is started to carry over the momentum, using the interruption time (captured in <code><a href="/reference/androidx/compose/animation/core/AnimationState.html#lastFrameTimeNanos()">AnimationState.lastFrameTimeNanos</a></code> creates a smoother animation.</p> |
| <p><code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> will be invoked on every frame, and the <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a></code> will be checked against cancellation before the animation continues. To cancel the animation from the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code>, simply call <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html#cancelAnimation()">AnimationScope.cancelAnimation</a></code>. After <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html#cancelAnimation()">AnimationScope.cancelAnimation</a></code> is called, <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> will not be invoked again. The animation loop will exit after the <code><a href="/reference/androidx/compose/animation/core/package-summary.html#(androidx.compose.animation.core.AnimationState).animateTo(kotlin.Any,androidx.compose.animation.core.AnimationSpec,kotlin.Boolean,kotlin.Function1)">block</a></code> returns. All the animation related info can be accessed via <code><a href="/reference/androidx/compose/animation/core/AnimationScope.html">AnimationScope</a></code>.</p> |
| <pre class="prettyprint"> |
| import androidx.compose.animation.core.AnimationState |
| import androidx.compose.animation.core.animateTo |
| import androidx.compose.animation.core.spring |
| 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 |
| }</pre> |
| </div> |
| </div> |
| </body> |
| </html> |