Anton Anton - 15 days ago 4
Android Question

Can I reuse ValueAnimator?

I have the following code (Android project in Scala):

val animator = new ValueAnimator
animator.setFloatValues(0f, 100f)
animator.setDuration(20000L)
animator.addUpdateListener(this) // prints current value to console
animator.start

override def onTouch(v: View, event: MotionEvent) = {
animator.setFloatValues(100f, 0f)
animator.setCurrentPlayTime(0)

if (!animator.isRunning) animator.start
true
}


If I touch the screen while
animator
is running then it correctly starts working backwards (since I've swapped the values). But if I touch the screen after it is finished then nothing happens, it does not start over.

Question is can I reuse this animator somehow and make it work again for given values after it has been stopped?

Answer

You cannot reuse an animation.

You need to reset() and reinitialize an animation object by calling the initialize() method before using the same object again. This is similar to instantiating a new animation object.