Anton Anton - 8 months ago 75
Android Question

Can I reuse ValueAnimator?

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

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

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

if (!animator.isRunning) animator.start

If I touch the screen while
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?


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.