Is it possible to 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 at runtime animator, it will start working correctly backward (since I changed the values). But if I touch the screen after it is finished, nothing will happen, it will not start.

Question: can I somehow reuse this animator and make it work again for the given values ​​after it stops?

+4
source share
2 answers

You cannot reuse animations.

reset() , initialize(), . .

0

:

...
animator.end();
animator.setFloatValues(...);
animator.start();
...

animator.cancel() animator.end() . , 50, animator.setFloatValues(50, 0f), .

, , , , Touch Circle, . BTW, , - , , :

void shapeTremble(long delay) {
    if (null == animatorTremble) {
        ValueAnimator animator = new ValueAnimator();
        animator.setDuration(850).setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                setCircleScale((Float) valueAnimator.getAnimatedValue(), true);
            }
        });
        animatorTremble = animator;
    } else {
        animatorTremble.cancel();
    }
    animatorTremble.setFloatValues(circleScale, 0.85f, 1.05f, 0.95f, 1.025f, 1.0f);
    animatorTremble.setStartDelay(delay);
    animatorTremble.start();
}
+2

All Articles