Last active
January 19, 2016 00:30
-
-
Save NikolaDespotoski/6d6f177b391f39e1d52d to your computer and use it in GitHub Desktop.
Animate and toggle Toolbar navigation icon without opening the navigation drawer.
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
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