【问题标题】:Android hamburger menu button shake animationAndroid 汉堡菜单按钮抖动动画
【发布时间】:2017-07-17 15:31:07
【问题描述】:

我需要创建一个动画来定期让汉堡菜单按钮抖动。如果这只是一个观点,我可以弄清楚,但事实并非如此。以下是我如何使用自定义图像设置图标:

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);
    }
});

我不能只为工具栏设置动画(这是我可以设置动画的View),我只需要为菜单图标设置动画。搜索答案不起作用,因为所有结果都是关于抽屉打开和关闭时图标和箭头之间的动画。这甚至可能吗?

【问题讨论】:

标签: android animation


【解决方案1】:

我通过来回切换drawable来解决它,但如果有人有更好的解决方案,那就太好了。

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2021-05-28
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多