Jetpack Compose infiniteRepeatable,重复startDelay

Jetpack Compose infiniteRepeatable, repeats startDelay

我想在 Jetpack Compose 中用一次性 start delay 创建一个无限动画。

我用 infiniteRepeatable()tween():

val value by rememberInfiniteTransition().animateFloat(
    initialValue = 0f,
    targetValue = 1f,
    animationSpec = infiniteRepeatable(
        animation = tween(
            durationMillis = 700,
            delayMillis = 200
        )
    )
)

在这种情况下,delayMillis 将重复:

*delay* 0..1, *delay* 0..1, *delay* 0..1 , *delay* 0..1  ...

但在 ValueAnimatorstart delay 是一次性延迟:

val animator = ValueAnimator.ofFloat(0f, 1f).apply {
    duration = 700
    startDelay = 200
    repeatCount = ValueAnimator.INFINITE
    addListener { /* value */ }
}
animator.start()
*delay* 0..1, 0..1, 0..1, 0..1, 0..1, 0..1, 0..1  ...

有没有办法在Jetpack Compose中为InfiniteRepeatable设置一次性启动延迟?

谢谢

目前尚不支持 (1.0.0-beta07) 个非重复延迟。

您可以将动画更改为类似以下内容:

val animatedFloat = remember { Animatable(0f) }

LaunchedEffect(animatedFloat) {
    delay(200) // to avoid repeated delays
    animatedFloat.animateTo(
        targetValue = 1f, animationSpec = infiniteRepeatable(
            animation = tween(700, easing =  FastOutSlowInEasing),
            repeatMode = RepeatMode.Reverse
        )
    )
}

在我的例子中使用 LaunchedEffect。 希望对你有帮助。

                val spin = animateFloatAsState(
            targetValue = if (rotation.value) 360F else 0F,
            animationSpec = infiniteRepeatable(
                animation = tween(durationMillis = 36_000),
                repeatMode = RepeatMode.Reverse
            ),
            /*finishedListener = {rotation.value = false}*/
        )
        LaunchedEffect(key1 = true) {
            delay(10_000)
            rotation.value = true
        }