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

InfiniteRepeatableSpec

public final class InfiniteRepeatableSpec<T extends Object> implements AnimationSpec


InfiniteRepeatableSpec repeats the provided animation infinite amount of times. It will never naturally finish. This means the animation will only be stopped via some form of manual cancellation. When used with transition or other animation composables, the infinite animations will stop when the composable is removed from the compose tree.

For non-infinite repeating animations, consider RepeatableSpec.

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)
}
See also
infiniteRepeatable

Summary

Public constructors

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

Public methods

boolean
equals(Object other)
final @NonNull DurationBasedAnimationSpec<@NonNull T>

the AnimationSpec to be repeated

final @NonNull StartOffset

offsets the start of the animation

final @NonNull RepeatMode

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

int
@NonNull VectorizedAnimationSpec<@NonNull V>
<V extends AnimationVector> vectorize(
    @NonNull TwoWayConverter<@NonNull T, @NonNull V> converter
)

Creates a VectorizedAnimationSpec with the given TwoWayConverter.

Public constructors

InfiniteRepeatableSpec

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

the AnimationSpec to 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

Public methods

equals

public boolean equals(Object other)

getAnimation

public final @NonNull DurationBasedAnimationSpec<@NonNull T> getAnimation()

the AnimationSpec to be repeated

getInitialStartOffset

public final @NonNull StartOffset getInitialStartOffset()

offsets the start of the animation

getRepeatMode

public final @NonNull RepeatMode getRepeatMode()

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

hashCode

public int hashCode()

vectorize

public @NonNull VectorizedAnimationSpec<@NonNull V> <V extends AnimationVector> vectorize(
    @NonNull TwoWayConverter<@NonNull T, @NonNull V> converter
)

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
@NonNull TwoWayConverter<@NonNull T, @NonNull V> converter

converts the type T from and to AnimationVector type