【问题标题】:Create animation image going up with delay and duration when opening the activity jetpack compose在打开活动喷气背包组合时创建具有延迟和持续时间的动画图像
【发布时间】:2021-04-11 23:49:31
【问题描述】:

我需要你的帮助。

一切都在标题中,我不知道该怎么做...请帮助我。 我使用了很多东西,如补间、枚举类和其他,但没有用,我想要一个 200dp 的初始值,当活动打开时,2 秒后,图像在 2 秒内从 200dp 变为 100dp。 谢谢。

【问题讨论】:

    标签: android kotlin android-jetpack android-jetpack-compose


    【解决方案1】:

    @Gabrial 的答案是完美的,但可以通过使用图形图层属性而不是大小进行优化,这将导致更好的性能,因为在动画期间不会发生重组。

    编辑。无罪 @加布里尔。 您的分析器仍会触发重组,因为您没有使用带有 lambda 修饰符的图形层。

    根据文档 如果图层参数由 androidx.compose.runtime.State 或动画值支持,则更喜欢在 GraphicsLayerScope 上使用 lambda 块的重载,因为读取块内的状态只会导致图层属性更新,而不会触发重组和重新布局。

    所以上面的代码片段应该是这样的

    val animatedProgress = remember { Animatable(1f) }
    
    LaunchedEffect(animatedProgress) {
        animatedProgress.animateTo(0.5f,
            animationSpec = tween(
                durationMillis = 2000,  
                delayMillis = 2000
            ))
    }
    
    Image(
        painterResource(id = R.drawable.xxx), "contentDescription",
        modifier = Modifier
                .size(100.dp)
                .graphicsLayer{
                    scaleY = animatedProgress.value,
                    scaleX = animatedProgress.value
    }
    

    【讨论】:

    • 谢谢!我会接受的。
    • 你是对的。谢谢指正。
    【解决方案2】:

    通过1.0.0-beta04,您可以使用Animatable API 和LaunchedEffect 组合。

    val animatedProgress = remember { Animatable(1f) }
    
    LaunchedEffect(animatedProgress) {
        animatedProgress.animateTo(0.5f,
            animationSpec = tween(
                durationMillis = 2000,  
                delayMillis = 2000
            ))
    }
    
    Image(
        painterResource(id = R.drawable.xxx), "contentDescription",
        modifier = Modifier
                .size(100.dp)
                .graphicsLayer{
                    scaleY = animatedProgress.value;
                    scaleX = animatedProgress.value}
    )
    

    感谢@Sheikh Zakir Ahmad 提供有关.graphicsLayer 的提示。

    【讨论】:

    • 谢谢!!!!就是这样,因为在 beta 3 和更早的版本中,我对这种类型的动画很困惑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2013-08-18
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多