Skip to content

Instantly share code, notes, and snippets.

@AnderWeb
Last active January 12, 2024 17:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnderWeb/85a5aed5c7da3398980c to your computer and use it in GitHub Desktop.
Save AnderWeb/85a5aed5c7da3398980c to your computer and use it in GitHub Desktop.
AnimatorListenerAdapter extension to bypass android 4.0.x bug where each method was called twice
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
/**
* Created by Gustavo Claramunt (AnderWeb) on 2014/07/10
*
*
*
* This class extends android.animation.AnimatorListenerAdapter to bypass 2 issues:
* Issue 1: on Android 4.0.x onAnimationStart/onAnimationEnd are called twice.
*
* Issue 2: when you cancel() an animation, there's no way for your listener to know
* if the animation has been canceled before executing your code.
*
* This class is meant to be used by implementing 3 (optional) methods:
* -onAnimationStartOk()
* -onAnimationEndOk()
* -onAnimationEndCanceled()
*
**/
@TargetApi(11)
public class OneShotCancelableAnimatorListenerAdapter extends AnimatorListenerAdapter {
private boolean doneStart = false;
private boolean doneEnd = false;
private boolean canceled = false;
@Override
public final void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
if (!doneStart) {
doneStart = true;
onAnimationStartOk(animation);
}
}
@Override
public final void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
canceled = true;
}
@Override
public final void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (!doneEnd) {
doneEnd = true;
if (canceled) {
onAnimationEndCanceled(animation);
} else {
onAnimationEndOk(animation);
}
}
}
/**
* This method will be called ONLY ONCE when your animation starts
* @param animation
*/
public void onAnimationStartOk(Animator animation) {
}
/**
* This method will be called ONLY ONCE when your animation ends and ONLY if it wasn't previously canceled
* @param animation
*/
public void onAnimationEndOk(Animator animation) {
}
/**
* This method will be called ONLY ONCE when your animation ends and ONLY because it was canceled
* @param animation
*/
public void onAnimationEndCanceled(Animator animation) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment