如何使用ObjectAnimator依次运行多个平移动画?
我必须像这样对图像进行动画处理 -> 从中心向左移动,然后返回中心,之后它必须向右移动并返回中心,然后无休止地重复该序列,我尝试这样做:
val finalizer = Runnable {
animateCenterToLeft()
animateCenterToRight()
}
Handler().postDelayed(finalizer, 0)
private fun animateCenterToLeft() {
val finalizerR = Runnable {
binding.logoAnimated.post {
val point = Point()
requireActivity().windowManager.defaultDisplay.getSize(point)
val width = binding.logoAnimated.measuredWidth.toFloat()
val objectAnimator = ObjectAnimator.ofFloat(
binding.logoAnimated,
"translationX",
0f,
-(width - point.x)
)
objectAnimator.repeatMode = ValueAnimator.REVERSE
objectAnimator.repeatCount = 1
objectAnimator.setDuration(10000)
objectAnimator.start()
}
}
Handler().postDelayed(finalizerR, 500)
}
private fun animateCenterToRight() {
val finalizerL = Runnable {
binding.logoAnimated.post {
val point = Point()
requireActivity().windowManager.defaultDisplay.getSize(point)
val width = binding.logoAnimated.measuredWidth.toFloat()
val objectAnimator = ObjectAnimator.ofFloat(
binding.logoAnimated,
"translationX",
0f,
+(width - point.x)
)
objectAnimator.repeatMode = ValueAnimator.REVERSE
objectAnimator.repeatCount = 1
objectAnimator.setDuration(10000)
objectAnimator.start()
}
}
Handler().postDelayed(finalizerL, 500)
}
我希望第一个动画先开始,然后是第二个,但只有第二个动画开始
更新
我尝试了这个,但动画太快了
val animator1 = ObjectAnimator
.ofFloat(binding.logoAnimated,
"translationX", 0f, -(width - point.x)).apply {
repeatCount = 1
repeatMode = ValueAnimator.REVERSE
}
val animator2 = ObjectAnimator
.ofFloat(binding.logoAnimated,"translationX",
0f, +(width - point.x)).apply {
repeatCount = 1
repeatMode = ValueAnimator.REVERSE
}
val animatorSet = AnimatorSet()
animatorSet.playSequentially(animator1, animator2)
animatorSet.start()
您可以使用
AnimatorSet
依次播放两个 ObjectAnimator
:
val animator1 = ObjectAnimator.ofInt(1, 100).apply {
repeatCount = ObjectAnimator.INFINITE
repeatMode = ObjectAnimator.RESTART
}
val animator2 = ObjectAnimator.ofInt(1, 100).apply {
repeatCount = ObjectAnimator.INFINITE
repeatMode = ObjectAnimator.RESTART
}
val animatorSet = AnimatorSet()
animatorSet.playSequentially(animator1, animator2)
animatorSet.start()