Last active
August 29, 2015 14:18
-
-
Save zsiegel/815517a82dd1d3780730 to your computer and use it in GitHub Desktop.
Animating Drawer Toggle
This file contains hidden or 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 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