Is it possible to use the “Hamburger icon” animati

2019-07-18 08:57发布

问题:

I would like to have two Hamburger icon with its animation in two different positions, one button in the usually place (at the left in my toolbar) and other in a button placed inside the Navigation Drawer.

Looking the Android documentation I couldn't see anything talking about using the hamburguer icon in other position more than the toolbar, so I would like to know if there is an easy way to implement or copy the animation to other position/button without having to do my own animation.

Thank you in advance

回答1:

So I am a little late on this one (only just came across the same problem). You can use the underlying drawable DrawerArrowDrawable from the v7 support library. It handles the drawing based on it's progress setProgress(floatValue) but you need to handle the animation between states. I did this by wrapping it with a ValueAnimator. See example below:

final DrawerArrowDrawable drawable = new DrawerArrowDrawable(this);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(drawable);

ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override public void onAnimationUpdate(ValueAnimator animation) {
    drawable.setProgress((Float)animation.getAnimatedValue());
  }
});

...

// burger to arrow
animator.start();

...

// arrow to burger
animator.reverse();

This is an untested adaption from a production app, should work though :-)



回答2:

There are several implementations of the hamburger menu icon animation. You could try to use one of those. Take a look at this project:

https://github.com/keklikhasan/LDrawer

You could use the animation without the drawer. I hope this helps you.