在反向模式下延迟重复动画



我试图给重复动画添加一些延迟,但startDelay不起作用。当第一次播放动画时,它看起来很好用。

val path = Path().apply {
moveTo(imageView.x, imageView.y)
lineTo(x.toFloat(), y.toFloat())
}
ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
duration = Random.nextLong(500, 1000)
startDelay = 1000
doOnEnd {
startDelay = 3000
} 
start()
}

我也试着使用TimerHandler().postDelayed,但它甚至没有重复:

val path = Path().apply {
moveTo(imageView.x, imageView.y)
lineTo(x.toFloat(), y.toFloat())
}
ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
duration = Random.nextLong(500, 1000)
startDelay = 1000
doOnStart {
Timer().schedule(object : TimerTask() {
override fun run() {
repeatCount = 1
repeatMode = ValueAnimator.REVERSE
}
}, 3000)
}
start()
}

如何在反向模式下延迟实现重复?

您可以使用此代码模拟动画的延迟。

暂停/延迟/恢复对你有用。

val path = Path().apply {
moveTo(imageView.x, imageView.y)
lineTo(x.toFloat(), y.toFloat())
}
val delayBetweenRepeats = 2_000L
ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
duration = Random.nextLong(500, 1000)
startDelay = 1000
repeatCount = 5
repeatMode = ValueAnimator.REVERSE
doOnRepeat {
pause()
Timer().schedule(object : TimerTask() {
override fun run() {
runOnUiThread { resume() }
}
}, delayBetweenRepeats)
}
start()
}

最新更新