Skip to content

Instantly share code, notes, and snippets.

@NikolaDespotoski
Last active January 19, 2016 00:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaDespotoski/6d6f177b391f39e1d52d to your computer and use it in GitHub Desktop.
Save NikolaDespotoski/6d6f177b391f39e1d52d to your computer and use it in GitHub Desktop.
Animate and toggle Toolbar navigation icon without opening the navigation drawer.
private enum ActionDrawableState{
BURGER, ARROW
}
private static void toggleActionBarIcon(ActionDrawableState state, final ActionBarDrawerToggle toggle, boolean animate){
if(animate) {
float start = state == ActionDrawableState.ARROW ? 0f : 1.0f;
final float end = Math.abs(start - 1);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ValueAnimator offsetAnimator = ValueAnimator.ofFloat(start, end);
offsetAnimator.setDuration(300);
offsetAnimator.setEvaluator(new FloatEvaluator());
offsetAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
offsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float offset = 0;
offset = (Float) animation.getAnimatedValue();
offset = Math.abs(offset);
offset = Math.min(1f, Math.max(0, offset));
toggle.onDrawerSlide(null, offset);
}
});
offsetAnimator.start();
}else{
//do the same with nine-old-androids lib :)
}
}else{
if(state == ActionDrawableState.BURGER){
toggle.onDrawerClosed(null);
}else{
toggle.onDrawerOpened(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment