{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

AnimationSpecKt

public final class AnimationSpecKt


Summary

Public methods

static final @NonNull InfiniteRepeatableSpec<@NonNull T>
<T extends Object> infiniteRepeatable(
    @NonNull DurationBasedAnimationSpec<@NonNull T> animation,
    @NonNull RepeatMode repeatMode,
    @NonNull StartOffset initialStartOffset
)

Creates a InfiniteRepeatableSpec that plays a DurationBasedAnimationSpec (e.g. TweenSpec, KeyframesSpec) infinite amount of iterations.

static final @NonNull KeyframesSpec<@NonNull T>

Creates a KeyframesSpec animation, initialized with init.

static final @NonNull RepeatableSpec<@NonNull T>
<T extends Object> repeatable(
    int iterations,
    @NonNull DurationBasedAnimationSpec<@NonNull T> animation,
    @NonNull RepeatMode repeatMode,
    @NonNull StartOffset initialStartOffset
)

Creates a RepeatableSpec that plays a DurationBasedAnimationSpec (e.g. TweenSpec, KeyframesSpec) the amount of iterations specified by iterations.

static final @NonNull SnapSpec<@NonNull T>
<T extends Object> snap(int delayMillis)

Creates a Snap animation for immediately switching the animating value to the end value.

static final @NonNull SpringSpec<@NonNull T>
<T extends Object> spring(
    float dampingRatio,
    float stiffness,
    T visibilityThreshold
)

Creates a SpringSpec that uses the given spring constants (i.e. dampingRatio and stiffness.

static final @NonNull TweenSpec<@NonNull T>
<T extends Object> tween(
    int durationMillis,
    int delayMillis,
    @NonNull Easing easing
)

Creates a TweenSpec configured with the given duration, delay and easing curve.

Public methods

infiniteRepeatable

public static final @NonNull InfiniteRepeatableSpec<@NonNull T> <T extends Object> infiniteRepeatable(
    @NonNull DurationBasedAnimationSpec<@NonNull T> animation,
    @NonNull RepeatMode repeatMode,
    @NonNull StartOffset initialStartOffset
)

Creates a InfiniteRepeatableSpec that plays a DurationBasedAnimationSpec (e.g. TweenSpec, KeyframesSpec) infinite amount of iterations.

For non-infinitely repeating animations, consider repeatable.

initialStartOffset can be used to either delay the start of the animation or to fast forward the animation to a given play time. This start offset will not be repeated, whereas the delay in the animation (if any) will be repeated. By default, the amount of offset is 0.

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.StartOffset
import androidx.compose.animation.core.StartOffsetType
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp

// This is an infinite progress indicator with 3 pulsing dots that grow and shrink.
@Composable
fun Dot(scale: State<Float>) {
    Box(
        Modifier.padding(5.dp).size(20.dp).graphicsLayer {
            scaleX = scale.value
            scaleY = scale.value
        }.background(Color.Gray, shape = CircleShape)
    )
}

val infiniteTransition = rememberInfiniteTransition()
val scale1 = infiniteTransition.animateFloat(
    0.2f,
    1f,
    // No offset for the 1st animation
    infiniteRepeatable(tween(600), RepeatMode.Reverse)
)
val scale2 = infiniteTransition.animateFloat(
    0.2f,
    1f,
    infiniteRepeatable(
        tween(600), RepeatMode.Reverse,
        // Offsets the 2nd animation by starting from 150ms of the animation
        // This offset will not be repeated.
        initialStartOffset = StartOffset(offsetMillis = 150, StartOffsetType.FastForward)
    )
)
val scale3 = infiniteTransition.animateFloat(
    0.2f,
    1f,
    infiniteRepeatable(
        tween(600), RepeatMode.Reverse,
        // Offsets the 3rd animation by starting from 300ms of the animation. This
        // offset will be not repeated.
        initialStartOffset = StartOffset(offsetMillis = 300, StartOffsetType.FastForward)
    )
)
Row {
    Dot(scale1)
    Dot(scale2)
    Dot(scale3)
}
Parameters
@NonNull DurationBasedAnimationSpec<@NonNull T> animation

animation that will be repeated

@NonNull RepeatMode repeatMode

whether animation should repeat by starting from the beginning (i.e. RepeatMode.Restart) or from the end (i.e. RepeatMode.Reverse)

@NonNull StartOffset initialStartOffset

offsets the start of the animation

keyframes

public static final @NonNull KeyframesSpec<@NonNull T> <T extends Object> keyframes(
    @ExtensionFunctionType @NonNull Function1<@NonNull KeyframesSpec.KeyframesSpecConfig<@NonNull T>, Unit> init
)

Creates a KeyframesSpec animation, initialized with init. For example:

import androidx.compose.animation.core.keyframes

keyframes {
    0f at 0 // ms  // Optional
    0.4f at 75 // ms
    0.4f at 225 // ms
    0f at 375 // ms  // Optional
    durationMillis = 375
}

Keyframes can also be associated with a particular Easing function:

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.keyframes

// Use FastOutSlowInEasing for the interval from 0 to 50 ms, and LinearOutSlowInEasing for the
// time between 50 and 100ms
keyframes<Float> {
    durationMillis = 100
    0f at 0 with FastOutSlowInEasing
    1.5f at 50 with LinearOutSlowInEasing
    1f at 100
}
Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull KeyframesSpec.KeyframesSpecConfig<@NonNull T>, Unit> init

Initialization function for the KeyframesSpec animation

repeatable

public static final @NonNull RepeatableSpec<@NonNull T> <T extends Object> repeatable(
    int iterations,
    @NonNull DurationBasedAnimationSpec<@NonNull T> animation,
    @NonNull RepeatMode repeatMode,
    @NonNull StartOffset initialStartOffset
)

Creates a RepeatableSpec that plays a DurationBasedAnimationSpec (e.g. TweenSpec, KeyframesSpec) the amount of iterations specified by iterations.

The iteration count describes the amount of times the animation will run. 1 means no repeat. Recommend infiniteRepeatable for creating an infinity repeating animation.

Note: When repeating in the RepeatMode.Reverse mode, it's highly recommended to have an odd number of iterations. Otherwise, the animation may jump to the end value when it finishes the last iteration.

initialStartOffset can be used to either delay the start of the animation or to fast forward the animation to a given play time. This start offset will not be repeated, whereas the delay in the animation (if any) will be repeated. By default, the amount of offset is 0.

Parameters
int iterations

the total count of iterations, should be greater than 1 to repeat.

@NonNull DurationBasedAnimationSpec<@NonNull T> animation

animation that will be repeated

@NonNull RepeatMode repeatMode

whether animation should repeat by starting from the beginning (i.e. RepeatMode.Restart) or from the end (i.e. RepeatMode.Reverse)

@NonNull StartOffset initialStartOffset

offsets the start of the animation

snap

public static final @NonNull SnapSpec<@NonNull T> <T extends Object> snap(int delayMillis)

Creates a Snap animation for immediately switching the animating value to the end value.

Parameters
int delayMillis

the number of milliseconds to wait before the animation runs. 0 by default.

spring

public static final @NonNull SpringSpec<@NonNull T> <T extends Object> spring(
    float dampingRatio,
    float stiffness,
    T visibilityThreshold
)

Creates a SpringSpec that uses the given spring constants (i.e. dampingRatio and stiffness. The optional visibilityThreshold defines when the animation should be considered to be visually close enough to round off to its target.

Parameters
float dampingRatio

damping ratio of the spring. Spring.DampingRatioNoBouncy by default.

float stiffness

stiffness of the spring. Spring.StiffnessMedium by default.

T visibilityThreshold

optionally specifies the visibility threshold.

tween

public static final @NonNull TweenSpec<@NonNull T> <T extends Object> tween(
    int durationMillis,
    int delayMillis,
    @NonNull Easing easing
)

Creates a TweenSpec configured with the given duration, delay and easing curve.

Parameters
int durationMillis

duration of the animation spec

int delayMillis

the amount of time in milliseconds that animation waits before starting

@NonNull Easing easing

the easing curve that will be used to interpolate between start and end