Skip to content

Instantly share code, notes, and snippets.

@zsiegel
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsiegel/815517a82dd1d3780730 to your computer and use it in GitHub Desktop.
Save zsiegel/815517a82dd1d3780730 to your computer and use it in GitHub Desktop.
Animating Drawer Toggle
public class AnimatingDrawerToggle extends ActionBarDrawerToggle {
public enum State {
UP,
HOME
}
private static final float MENU_POSITION = 0f;
private static final float ARROW_POSITION = 1.0f;
private final int duration;
private final DrawerLayout drawerLayout;
private final Activity activity;
private State currentState;
public AnimatingDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescriptionResource, int closeDrawerContentDescriptionResource) {
super(activity, drawerLayout, toolbar, openDrawerContentDescriptionResource, closeDrawerContentDescriptionResource);
duration = activity.getResources().getInteger(android.R.integer.config_shortAnimTime);
this.drawerLayout = drawerLayout;
this.activity = activity;
currentState = State.HOME;
}
public void animateToUp() {
if (currentState != State.UP) {
ValueAnimator anim = ValueAnimator.ofFloat(MENU_POSITION, ARROW_POSITION);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(duration);
anim.start();
currentState = State.UP;
}
}
public void animateToHome() {
if (currentState != State.HOME) {
ValueAnimator anim = ValueAnimator.ofFloat(ARROW_POSITION, MENU_POSITION);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(duration);
anim.start();
currentState = State.HOME;
}
}
public State getCurrentState() {
return currentState;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment