如何在AnimatedVisibility中设置弹簧类型animationSpec的持续时间?

问题描述 投票:0回答:1

spring
中使用
animationSpec
类型
AnimatedVisibility
时如何设置持续时间?

AnimatedVisibility(
    enter = fadeIn(
        animationSpec = spring(stiffness = Spring.StiffnessMediumLow)
    ),
    exit = fadeOut(
        animationSpec = spring(stiffness = Spring.StiffnessMediumLow)
    ),
    ...
) {
    ...
}
android animation android-jetpack-compose android-animation
1个回答
0
投票

在 Jetpack compose 中,当使用 spring 作为animationSpec 时,您不需要直接设置持续时间,因为 Spring 动画是由阻尼、刚度和质量等物理属性控制的,而不是由持续时间控制的。但是,您可以通过以下方式操纵动画的时间调整刚度和阻尼比。

AnimatedVisibility(
enter = fadeIn(
    animationSpec = spring(
        stiffness = Spring.StiffnessLow,  // Lower stiffness for slower animation
        dampingRatio = Spring.DampingRatioMediumBouncy // Adjust damping for bounce effect
    )
),
exit = fadeOut(
    animationSpec = spring(
        stiffness = Spring.StiffnessHigh, // Higher stiffness for faster animation
        dampingRatio = Spring.DampingRatioNoBouncy // Less bounce for faster exit
    )
),

如果您需要精确控制时间,请考虑使用补间或关键帧而不是弹簧,因为它们允许您设置以毫秒为单位的持续时间。

AnimatedVisibility(
enter = fadeIn(
    animationSpec = tween(durationMillis = 1000)  // 1 second duration
),
exit = fadeOut(
    animationSpec = tween(durationMillis = 500)  // 0.5 second duration
),)
© www.soinside.com 2019 - 2024. All rights reserved.