What is the best practice to reverse animation direction on android? Also, I track ram usage on the profiler and the result was normal as it is. I did like the below code:
var flagHeight: Int = 100
private fun startAnimation() {
val animation = tv_hello_world.animate().apply {
translationYBy(flagHeight.toFloat())
setListener(object : Animator.AnimatorListener {
override fun onAnimationStart(p0: Animator?) {
// do nothing
}
override fun onAnimationEnd(p0: Animator?) {
flagHeight = flagHeight.not()
startAnimation()
}
override fun onAnimationCancel(p0: Animator?) {
// do nothing
}
override fun onAnimationRepeat(p0: Animator?) {
// do nothing
}
})
duration = 1000
}
animation.start()
}
fun Int.not() = run { if (this > 0) -this else (this * -1) }
The proper way to do it by using ValueAnimator
instead on PropertyAnimator
since you have more control over it. Try it this way:
val animator = ValueAnimator.ofFloat(0f, 100f).apply {
duration = 1000
repeatCount = ValueAnimator.INFINITE
repeatMode = ValueAnimator.REVERSE
addUpdateListener {
tv_hello_world.translationY = it.animatedValue as Float
}
}
animator.start()