How to stop an ValueAnimator without Listener.onAnimationEnd called
Asked Answered
M

2

11

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?

Moslemism answered 18/1, 2016 at 5:2 Comment(3)
Why not use a boolean field somewhere to inform your onAnimationEnd() implementation "hey, don't do your normal processing"?Jameyjami
@Jameyjami This is the answer.Moslemism
@Jameyjami can you post the answer so that I can accept it.Moslemism
J
10

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.

Jameyjami answered 10/3, 2017 at 11:58 Comment(4)
Is there any documentation that suggests that this is the behavior of onAnimationEnd() ?Germany
@toobsco42: I am uncertain what documentation you are seeking. I am describing an implementation of onAnimationEnd(), where it would use a boolean to determine whether it should do its normal work or not.Jameyjami
ValueAnimator.cancel(boolean) not foundAshaashamed
@maXp: developer.android.com/reference/android/animation/…Jameyjami
S
17

Under certain circumstances, the use of the flag may cause problems.

You should using removeListener before call cancel method:

animator.removeAllListeners(); // Or animator.removeListener(your listener);
animator.cancel();
Second answered 9/1, 2020 at 13:28 Comment(0)
J
10

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.

Jameyjami answered 10/3, 2017 at 11:58 Comment(4)
Is there any documentation that suggests that this is the behavior of onAnimationEnd() ?Germany
@toobsco42: I am uncertain what documentation you are seeking. I am describing an implementation of onAnimationEnd(), where it would use a boolean to determine whether it should do its normal work or not.Jameyjami
ValueAnimator.cancel(boolean) not foundAshaashamed
@maXp: developer.android.com/reference/android/animation/…Jameyjami

© 2022 - 2024 — McMap. All rights reserved.