【发布时间】:2021-04-11 23:49:31
【问题描述】:
我需要你的帮助。
一切都在标题中,我不知道该怎么做...请帮助我。 我使用了很多东西,如补间、枚举类和其他,但没有用,我想要一个 200dp 的初始值,当活动打开时,2 秒后,图像在 2 秒内从 200dp 变为 100dp。 谢谢。
【问题讨论】:
标签: android kotlin android-jetpack android-jetpack-compose
我需要你的帮助。
一切都在标题中,我不知道该怎么做...请帮助我。 我使用了很多东西,如补间、枚举类和其他,但没有用,我想要一个 200dp 的初始值,当活动打开时,2 秒后,图像在 2 秒内从 200dp 变为 100dp。 谢谢。
【问题讨论】:
标签: android kotlin android-jetpack android-jetpack-compose
@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
}
【讨论】:
通过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 的提示。
【讨论】: