有没有办法延迟 Jetpack Compose 动画中的 Spring AnimationSpec?

Ely*_*lye 7 android android-jetpack-compose

在Jetpack Compose Animation中,对于tweenAnimationSpec,我们可以有延迟

                val translationY by transition.animateFloat(
                    transitionSpec = {
                        tween(
                            delayMillis = 1000, // Delay here
                            durationMillis = 2000
                        )
                    },
                    label = ""
                ) { if (it) 0f else 180f }
Run Code Online (Sandbox Code Playgroud)

然而,对于Spring AnimationSpec,我们只有dampingRatio(如下所示的0.25f)和Stiffness(如下所示的100f)。没有办法添加delay.

                val translationY by transition.animateFloat(
                    transitionSpec = {
                        spring(0.25f, 100f)
                    },
                    label = ""
                ) { if (it) 0f else 180f }
Run Code Online (Sandbox Code Playgroud)

如何使用 Spring AnimationSpec 为动画添加延迟?

小智 7

我设法将弹跳效果添加到补间中,这与弹簧非常相似。只需使用

easing = { OvershootInterpolator().getInterpolation(it) }
Run Code Online (Sandbox Code Playgroud)

OvershootInterpolator 来自android.view.animation包。

您可以通过 OvershootInterpolator 的构造函数向 OvershootInterpolator 提供张力参数来更改弹力。