Last active
September 4, 2017 10:48
-
-
Save darnmason/f820aa337e960c3a4a055c46fd954659 to your computer and use it in GitHub Desktop.
Animation utility for showing and hiding a View with a slide animation, used for Views that are anchored to the bottom of the screen overlaying the main content, usually confirmation buttons.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnimationUtils { | |
private static final int SLIDE_DURATION = 150; | |
private static final int FADE_DURATION = 100; | |
private static final ArrayMap<View, ObjectAnimator> showing = new ArrayMap<>(); | |
private static final ArrayMap<View, ObjectAnimator> hiding = new ArrayMap<>(); | |
private AnimationUtils() { | |
} | |
private static boolean isRunning(View view, ArrayMap map) { | |
return map.containsKey(view); | |
} | |
private static long cancelAnimations(View view, ArrayMap<View, ObjectAnimator> map) { | |
ObjectAnimator anim = map.remove(view); | |
long duration = 0; | |
if (anim != null) { | |
duration = anim.getCurrentPlayTime(); | |
anim.cancel(); | |
} | |
return duration; | |
} | |
private static void adjustPlayTime(ObjectAnimator anim, long elapsed) { | |
if (elapsed > 0) { | |
anim.setCurrentPlayTime(anim.getDuration() - elapsed); | |
} | |
} | |
public static void slideUp(View view) { | |
if (isRunning(view, showing) || view.getVisibility() == View.VISIBLE && !isRunning(view, hiding)) { | |
return; | |
} | |
int height = view.getHeight(); | |
if (height == 0) { | |
view.measure(makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); | |
height = view.getMeasuredHeight(); | |
} | |
long elapsed = cancelAnimations(view, hiding); | |
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", height, 0f); | |
anim.setDuration(SLIDE_DURATION); | |
adjustPlayTime(anim, elapsed); | |
anim.addListener(new ViewObjectAnimatorListener() { | |
@Override | |
public void onAnimationStart(Animator animation, View view) { | |
view.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void onAnimationEnd(Animator animation, View view) { | |
view.setTranslationY(0f); | |
showing.remove(view); | |
} | |
}); | |
anim.start(); | |
showing.put(view, anim); | |
} | |
public static void slideDown(View view) { | |
if (isRunning(view, hiding) || view.getVisibility() == View.GONE) { | |
return; | |
} | |
long elapsed = cancelAnimations(view, showing); | |
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", 0f, view.getHeight()); | |
anim.setDuration(SLIDE_DURATION); | |
adjustPlayTime(anim, elapsed); | |
anim.addListener(new ViewObjectAnimatorListener() { | |
@Override | |
public void onAnimationEnd(Animator animation, View view) { | |
view.setVisibility(View.GONE); | |
view.setTranslationY(0f); | |
hiding.remove(view); | |
} | |
}); | |
anim.start(); | |
hiding.put(view, anim); | |
} | |
public static void fadeIn(View view) { | |
if (isRunning(view, showing) || view.getVisibility() == View.VISIBLE && !isRunning(view, hiding)) { | |
return; | |
} | |
long elapsed = cancelAnimations(view, hiding); | |
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f); | |
anim.setDuration(FADE_DURATION); | |
adjustPlayTime(anim, elapsed); | |
anim.addListener(new ViewObjectAnimatorListener() { | |
@Override | |
public void onAnimationStart(Animator animation, View view) { | |
view.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void onAnimationEnd(Animator animation, View view) { | |
view.setAlpha(1f); | |
showing.remove(view); | |
} | |
}); | |
anim.start(); | |
showing.put(view, anim); | |
} | |
public static void fadeOut(View view, int visibility) { | |
if (isRunning(view, hiding) || view.getVisibility() == visibility) { | |
return; | |
} | |
long elapsed = cancelAnimations(view, showing); | |
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f); | |
anim.setDuration(FADE_DURATION); | |
adjustPlayTime(anim, elapsed); | |
anim.addListener(new ViewObjectAnimatorListener() { | |
@Override | |
public void onAnimationEnd(Animator animation, View view) { | |
view.setVisibility(visibility); | |
view.setAlpha(1f); | |
hiding.remove(view); | |
} | |
}); | |
anim.start(); | |
hiding.put(view, anim); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment