0

I need to create an animation to periodically make the hamburger menu button shake. If this were just a view I could figure it out but it isn't. Here's how I'm setting up the icon with a custom image:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.post(new Runnable() {
    @Override
    public void run() {
        Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.ham_menu, null);
        toolbar.setNavigationIcon(d);
    }
});

I can't just animate the toolbar (which is a View where I could set an animation), I need to animate just the menu icon. Searching for an answer is not working because all the results are about the animation between the icon and the arrow when the drawer opens and closes. Is this even possible?

nasch
  • 5,330
  • 6
  • 31
  • 52

1 Answers1

0

I worked around it by switching the drawable back and forth, but if anyone has a better solution that would be great.

private void animateMenu(final Toolbar toolbar){

            // Flash the menu several times quickly, then wait a few seconds. Repeat.
            animateMenu(toolbar, 6, R.drawable.ham_menu_2, new GenericCallback() {
                @Override
                public void success(Object result) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            animateMenu(toolbar);
                        }
                    }, 5000);
                }

                @Override
                public void failure(Throwable error) {

                }
            });
    }

    private void animateMenu(final Toolbar toolbar, final int count, final int drawable, final GenericCallback callback){

        toolbar.postDelayed(new Runnable() {
            @Override
            public void run() {

                Drawable d2 = ResourcesCompat.getDrawable(getResources(), drawable, null);
                toolbar.setNavigationIcon(d2);
                int i = count - 1;
                if(i == 0){
                    callback.success(null);
                }
                else {
                    animateMenu(toolbar, i, drawable == R.drawable.ham_menu ? R.drawable.ham_menu_2 : R.drawable.ham_menu, callback);
                }
            }
        }, 200);
    }
nasch
  • 5,330
  • 6
  • 31
  • 52