Cod*_*oet 2 android android-animation kotlin android-jetpack-compose
我正在尝试从下到上定期发射一些盒子。我几乎已经做到了这一点,但由于某种原因,我的盒子间隔不均匀,正如您从 gif 中看到的那样。我的感觉是我的代码不是实现我想要做的事情的最佳方式。有人有什么想法吗?
这是我的代码:
listOf(
Color(0xffDFFF00),
Color(0xffFFBF00),
Color(0xffFF7F50),
Color(0xffDE3163),
Color(0xff9FE2BF),
Color(0xff40E0D0),
Color(0xff6495ED),
Color(0xffCCCCFF),
).forEachIndexed { index, color ->
val infiniteTransition = rememberInfiniteTransition()
val positionState = infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = 2500,
delayMillis = delay,
easing = LinearEasing
)
)
)
delay += 1000
val modifier = Modifier.offset(
x = (maxWidth - tileSize),
y = maxHeight - (maxHeight * positionState.value),
)
Box(
modifier = modifier
.size(tileSize)
.clip(RoundedCornerShape(5.dp))
.background(color = color)
) {
Text(
text = text,
Modifier.align(Alignment.Center),
style = TextStyle(fontWeight = FontWeight.Bold)
)
}
Run Code Online (Sandbox Code Playgroud)
这是动图:
PS:现在尝试了接受的答案,它没有按预期工作。请参阅下面的评论。谢谢!
这是因为delay也在 中重复infiniteRepeatable。换句话说,第一次迭代中间隔 1000 毫秒的动画在第二次迭代中间隔为 2000 毫秒。
无限转换尚不支持非重复延迟。这里我推荐使用Animatable来实现交错的无限动画。
...
forEachIndexed { index, color ->
val positionAnimation = remember { Animatable(0f) }
LaunchedEffect(positionAnimation) {
delay(1000 * index) // this delay increases with the index
launch {
positionAnimation.animateTo(
1f,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = 2500, // remove the delay from infinite repeatable
easing = LinearEasing
)
)
)
}
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2416 次 |
| 最近记录: |