androidandroid-animationobjectanimator

How to stop an ValueAnimator without Listener.onAnimationEnd called


I add an AnimatorListenerAdapter into an ValueAnimator, but i need to stop the ValueAnimator while it's running sometime.

I tried ValueAnimator.cancel() or ValueAnimator.stop(), they both can stop the animator, but the AnimatorListenerAdapter.onAnimationEnd() called which is I don't want.

ValueAnimator.pause() works but it's only above api 19, so i can't use it;

Is there anyway else I can do this?


Solution

  • Use cancel() or stop(), along with a boolean field somewhere. The boolean serves as a flag to tell onAnimationEnd() whether or not it should do its regular work. Default that boolean to true; flip it to false when you want to block normal animation-end processing.