【问题标题】:How can I an animation on boolean change with jetpack compose?如何使用 Jetpack Compose 制作布尔值变化动画?
【发布时间】:2023-01-03 02:21:19
【问题描述】:

当布尔值发生变化时,如何使用 jetpack compose 实现类似摆动动画的效果? 据我了解,该库仅支持过渡动画或无限动画。然而,在我的例子中,没有可组合对象实际上改变了它们的目标值。在我发现的示例中,总是需要更改目标值才能真正看到动画。像这样

var isError by remember { mutableStateOf(false) }
val offset = animateIntAsState(if (isError) $targetValue else 0)
// Then have something that uses the offset and add a button that is changing the state of isError

但是,我不希望 targetValue 与最初的值不同。例如,我只想看一个关键帧动画。

【问题讨论】:

    标签: android-jetpack-compose jetpack-compose-animation


    【解决方案1】:

    为此,您可以使用所有animateXAsState函数附带的finishedListener参数来切换触发动画的状态。

    下面的示例将一行添加到您的代码 sn-p:

    var animationState by remember { mutableStateOf(false) }
    val xOffset: Dp by animateDpAsState(
        targetValue = if (animationState) 16.dp else 0.dp,
        finishedListener = { animationState = false }   // Add this
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多