Мне нужно анимировать изображение вот так -> из центра оно перемещается влево, а затем возвращается в центр, после который он должен сдвинуть вправо и вернуться в центр, а потом повторяет последовательность бесконечно, я пробовал сделать так:
Код: Выделить всё
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)
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... ctanimator