Android 超调器的等效成分是什么?

Android 超调器的等效成分是什么?,android,android-animation,android-jetpack-compose,Android,Android Animation,Android Jetpack Compose,在Android视图中,我们使用了一个OvershootInterpolator 我们想在Jetpack Compose中复制相同的动画。 我看到中描述了几个动画规范,但我不知道哪一个可以复制OvershootInterpolator tweenspec似乎没有做任何超调,只是在起始值和结束值之间设置动画而没有超调 springspec有超调,但是它没有tween的durationMillis参数,所以我们无法控制它的播放速度 keyFramesspec似乎是一个可行的解决方案,方法如下:

在Android视图中,我们使用了一个
OvershootInterpolator

我们想在Jetpack Compose中复制相同的动画。 我看到中描述了几个动画规范,但我不知道哪一个可以复制
OvershootInterpolator

  • tween
    spec似乎没有做任何超调,只是在起始值和结束值之间设置动画而没有超调

  • spring
    spec有超调,但是它没有tween的
    durationMillis
    参数,所以我们无法控制它的播放速度

  • keyFrames
    spec似乎是一个可行的解决方案,方法如下:

              animationSpec = keyframes {
                  durationMillis = 500
                  0f at 100 with FastOutSlowInEasing
                  // Overshoot value to 50f
                  50f * 2 at 300 with LinearOutSlowInEasing
                  // Actual final value after overshoot animation
                  25f at 500
              }
    

有没有比使用
关键帧
复制
超调器
更好/更简单的方法?

我们可以使用compose的
弹簧动画
来实现
超调器
。即使我们不能为动画提供任何自定义持续时间,我们也可以使用
stiffness
属性控制动画的速度

我们可以将任何自定义的
浮点值
放入
刚度
属性。
androidx.compose.animation.core
当前默认提供
4刚度
常量值,即

object Spring {
    /**
     * Stiffness constant for extremely stiff spring
     */
    const val StiffnessHigh = 10_000f

    /**
     * Stiffness constant for medium stiff spring. This is the default stiffness for spring
     * force.
     */
    const val StiffnessMedium = 1500f

    /**
     * Stiffness constant for a spring with low stiffness.
     */
    const val StiffnessLow = 200f

    /**
     * Stiffness constant for a spring with very low stiffness.
     */
    const val StiffnessVeryLow = 50f
    .....
}

我们可以检查哪个
刚度
适合我们的情况

例如:-(中速)

我们还可以将自定义值设置为
刚度
,例如:-

 animationSpec = spring(
    dampingRatio = Spring.DampingRatioHighBouncy, // this would define how far the overshoot would happen.
    stiffness = 1000f // with custom speed less than medium speed.
)

您可以使用
自定义浮点值来调整刚度,以达到理想的动画持续时间。

您可以使用
Animator
world中的任何
插值器,通过自定义
松弛

animationSpec = tween(easing = Easing {
    OvershootInterpolator().getInterpolation(it) 
})
请参见
Easing
界面:

尽管我建议使用弹簧而不是插值器,尤其是模拟弹簧的插值器。弹簧的外观会自然得多。:)

animationSpec = tween(easing = Easing {
    OvershootInterpolator().getInterpolation(it) 
})