gpt4 book ai didi

android - 如果更改为新动画,UpdateTransition 动画会保持其运行速度吗?

转载 作者:行者123 更新时间:2023-12-04 23:49:23 24 4
gpt4 key购买 nike

我使用 AnimationAsState 创建相同的动画, Animatable , 和 UpdateTransition并尝试一下。
所有动画都使用向前或向后移动。

    tween(
durationMillis = 3000,
easing = LinearOutSlowInEasing
)
我们可以在下面看到
enter image description here
如果动画执行到一半,我再次单击按钮,我会假设正在运行的动画速度保持不变并传递给后续动画。
这对于 AnimateAsState 似乎是正确的和 Animatable .然而,令我惊讶的是, UpdateTransition似乎没有那样做。我猜它恢复到默认动画持续时间(运行速度非常快)。
我们可以看到如下。
enter image description here
我的问题是,是吗
  • 预计 UpdateTransition如果后续动画中途取消,将不会保留正在运行的动画规范?
  • 我错过了我的代码中的某些内容(我在下面分享了整个代码)?
  • 这是 UpdateTransition错误,需要向 Google 报告吗?

  • 上述动画的整个工作代码如下
    import androidx.compose.animation.core.*
    import androidx.compose.animation.core.Animatable
    import androidx.compose.foundation.background
    import androidx.compose.foundation.layout.*
    import androidx.compose.material.Button
    import androidx.compose.material.Text
    import androidx.compose.runtime.*
    import androidx.compose.ui.Alignment
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.graphics.Color
    import androidx.compose.ui.unit.Dp
    import androidx.compose.ui.unit.dp

    @Composable
    fun Combination() {
    var enabled by remember { mutableStateOf(false) }

    val dbAnimateAsState: Dp by animateDpAsState(
    targetValue = switch(enabled),
    animationSpec = animationSpec()
    )

    val dbAnimatable = remember { Animatable(0.dp) }

    val transition = updateTransition(enabled, label = "")
    val dbTransition by transition.animateDp(
    transitionSpec = { animationSpec() }, label = "") {
    switch(it)
    }

    Column(
    modifier = Modifier.fillMaxSize(),
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally
    ) {

    Text("AnimateAsState")
    animateBoxHorizontal(dbAnimateAsState)
    Text("Animatable")
    animateBoxHorizontal(dbAnimatable.value)
    Text("UpdateTransition")
    animateBoxHorizontal(dbTransition)

    Button(onClick = { enabled = !enabled }) {
    Text("Click Me")
    }
    }

    LaunchedEffect(key1 = enabled) {
    dbAnimatable.animateTo(
    targetValue = switch(enabled),
    animationSpec = animationSpec()
    )
    }
    }

    private fun animationSpec(): TweenSpec<Dp> =
    tween(
    durationMillis = 3000,
    easing = LinearOutSlowInEasing
    )

    private fun switch(enabled: Boolean) = if (enabled) 268.dp else 0.dp

    fun Animatable(initialValue: Dp) = Animatable(
    initialValue,
    DpToVector,
    )

    private val DpToVector: TwoWayConverter<Dp, AnimationVector1D> =
    TwoWayConverter({ AnimationVector1D(it.value) }, { it.value.dp })

    @Composable
    private fun animateBoxHorizontal(dbAnimateAsState: Dp) {
    Box(
    modifier = Modifier
    .height(32.dp)
    .width(300.dp)
    .background(Color.Yellow)
    ) {
    Box(
    modifier = Modifier
    .offset(dbAnimateAsState, 0.dp)
    .size(32.dp)
    .background(Color.Red)
    )
    }
    Spacer(modifier = Modifier.height(16.dp))
    }
    注意:更新信息
    如果我从 tweenSpec 更改至 springSpec IE。
        tween(
    durationMillis = 3000,
    easing = LinearOutSlowInEasing
    )
        spring(stiffness =20f, dampingRatio = 0.25f)
    然后 updateTransition像往常一样工作,当我们用新的动画中断动画时, Spring 速度和动画被保留并连续。

    最佳答案

    updateTransition仍然保持速度 - 如果你可以放慢速度可能会更明显。它切换到 spring被打断时,而 animateAsStateAnimatable使用 AnimationSpec你提供的。那个后备 Spring 可能有点太僵硬了,因此它被认为是一个非常快速的变化。
    此设计的目的是允许 Transition在使用不同类型的 AnimationSpec 时适应中断s。例如,从状态 A 到状态 B,您可能正在使用 keyFrames ,从状态 B 到状态 C 或者可能正在使用 snap() (因为状态 B 和 C 中的值相同)。因此,当您以 C 作为新目的地从 A -> B 中断动画时,keyframessnap看起来会很奇怪。这就是我们回退到 spring 的原因。当一个 Transition被打断。
    请注意,如果您已经使用 spring Transition 中的动画,该 Spring 将用于处理该动画中的中断,因为它与您的动画更相关。这就是为什么当您在上面的代码 fragment 中为过渡提供低刚度 Spring 时,您会看到动画的“路线校正”要慢得多。
    我们确实计划支持中断处理的自定义,您可以在其中指定中断时使用的 AnimationSpec。这是计划作为 future 的工作。如果您对如何处理中断有任何具体要求,或者回退 Spring 应该慢一点,请随时提交功能请求。 :)

    关于android - 如果更改为新动画,UpdateTransition 动画会保持其运行速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71170430/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com