Ganjira
Ganjira

Reputation: 976

How to cancel an animation, but not to end it?

I have a TranslateAnimation on my class. The animation starts automatically.

I set button that if it is clicked, the animation will be canceled (animation.cancel();).

I also set an AnimationListener for my class. If my animation ends, I will start a new Activity (you go to the menu).

public void onAnimationEnd(Animation animation) {
    startActivity(new Intent(Class.this, Class2.class));
}

My app is relying on that the user has to click the button before the animation ends. The problem is that animation.cancel(); is admitted as the end of the animation.

How to cancel the animation in the other way that was not counted as the end of the animation? Is that possible?

Thanks in advance!

Upvotes: 12

Views: 7722

Answers (3)

Dannie
Dannie

Reputation: 175

Remove listeners of animators in the set:

animatorSet.childAnimations.forEach { it.removeAllListeners() }
animatorSet.removeAllListeners()
animatorSet.cancel()

Upvotes: 0

Simas
Simas

Reputation: 44188

Once the animation is cancelled, you can remove the listener thus preventing onAnimationEnd from being called:

@Override
public void onAnimationCancel(Animator animation) {
  animation.removeAllListeners();
}

Upvotes: 37

abbath
abbath

Reputation: 2482

animation.cancel() is calling the animation listener as API documentation describes:

Cancelling an animation invokes the animation listener, if set, to notify the end of the animation. If you cancel an animation manually, you must call reset() before starting the animation again.

If you want different behaviour on cancel() and onAnimationEnd() i would suggest a boolean variable, which can be set on button click, and onanimationend checks whether it is true.

Upvotes: 4

Related Questions