Android 将抽屉图标动画成 setDisplayHomeAsUpEnabled 上的箭头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26577006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:53:30  来源:igfitidea点击:

Animate drawer icon into arrow on setDisplayHomeAsUpEnabled?

androidanimationtoggledrawer

提问by Vextil

I'm using setDisplayHomeAsUpEnabled in order to show the arrow instead of the drawer "burger" icon but it's not getting animated or anything. Instead it shows the arrow drawable instantaneously.

我正在使用 setDisplayHomeAsUpEnabled 来显示箭头而不是抽屉“汉堡”图标,但它没有动画或任何东西。相反,它显示了可立即绘制的箭头。

Home screen: (Album 1)

主屏幕:(专辑 1)

When you tap a movie: (Album 2)

轻点电影时:(专辑 2)

The thing is, the icon does the animation just fine when I slide the drawer, which makes me think that maybe I'm not supposed to use setDisplayHomeAsUpEnabled for this: (Album 3)

问题是,当我滑动抽屉时,图标的动画效果很好,这让我觉得也许我不应该为此使用 setDisplayHomeAsUpEnabled:(专辑 3)

Album:http://imgur.com/a/LkXbh

专辑:http : //imgur.com/a/LkXbh

Here's my drawer toggle code:

这是我的抽屉切换代码:

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

    drawerAdapter = new DrawerAdapter(this, App.getNavItems(), getSupportFragmentManager());
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ExpandableListView) findViewById(R.id.left_drawer);

    // Set onGroupClick and onChildClick
    drawerAdapter.setClickEvents(MainActivity.this, drawerLayout, drawerList);
    drawerList.setAdapter(drawerAdapter);

    ActionBarDrawerToggle toolbarDrawerToggle = new ActionBarDrawerToggle(
            this,                 
            drawerLayout,        
            toolbar,             
            R.string.drawer_open, 
            R.string.drawer_close 
    ) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View view) {
            super.onDrawerOpened(view);
            invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(toolbarDrawerToggle);
    toolbarDrawerToggle.syncState();

EDIT:I want the animation not when opening the drawer, that works already. I would like to manually trigger the animation when I load a specific fragment. I may not have explained myself correctly.

编辑:我不想要打开抽屉时的动画,那已经可以了。我想在加载特定片段时手动触发动画。我可能没有正确解释自己。

回答by Nathan Walters

I haven't tested this, but you may be able to achieve this by animating a float between 0 (drawer closed) and 1 (drawer open) and then passing the value into ActionBarDrawerToggle.onDrawerSlide(View, float). I believe that's how the toggle determines what state the animated toggle should be in.

我还没有测试过这个,但是你可以通过在 0(抽屉关闭)和 1(抽屉打开)之间设置一个浮动动画,然后将值传递给ActionBarDrawerToggle.onDrawerSlide(View, float). 我相信这就是切换确定动画切换应该处于什么状态的方式。

Something like this should work.

像这样的事情应该有效。

ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        float slideOffset = (Float) valueAnimator.getAnimatedValue();
        toolbarDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
    }
});
anim.setInterpolator(new DecelerateInterpolator());
// You can change this duration to more closely match that of the default animation.
anim.setDuration(500);
anim.start();

回答by Kirill Rakhman

Since the question was asked, an alternative way has become available. The animated arrow is implemented by the now public class DrawerArrowDrawablewhich implements Drawable.

自从提出这个问题以来,已经有了另一种方法。动画箭头由现在实现的公共类DrawerArrowDrawable实现Drawable

In your code, set the navigation icon as follows:

在您的代码中,按如下方式设置导航图标:

DrawerArrowDrawable drawerArrow = new DrawerArrowDrawable(this);
drawerArrow.setColor(myColor);

toolbar.setNavigationIcon(drawerArrow);

Register an OnBackStackChangedListenerand animate the arrow manually:

OnBackStackChangedListener手动注册和动画箭头:

@Override
public void onBackStackChanged() {
    boolean drawer = getSupportFragmentManager().getBackStackEntryCount() == 0;
    ObjectAnimator.ofFloat(drawerArrow, "progress", drawer ? 0 : 1).start();
}

回答by Philio

Sounds like it's working as intended, you can use setDisplayHomeAsUpEnabledto simply enable the home button to be used as a back button, there is no animation.

听起来它按预期工作,您可以setDisplayHomeAsUpEnabled简单地将主页按钮用作后退按钮,没有动画。

If you're using a navigation drawer and want the animation don't use setDisplayHomeAsUpEnabledand for material theme (at least with AppCompat v21) make sure you use ActionBarDrawerTogglefrom the v7 package.

如果您正在使用导航抽屉并希望不使用动画setDisplayHomeAsUpEnabled和材质主题(至少使用 AppCompat v21),请确保使用ActionBarDrawerTogglev7 包。

The Play Store is a good example. At the top level you have an activity with a nav drawer and a hamburger that animates when you open the drawer. If you tap on an app it opens a new activity that has a back arrow.

Play 商店就是一个很好的例子。在顶层,您有一个带有导航抽屉的活动和一个在您打开抽屉时动画的汉堡包。如果你点击一个应用程序,它会打开一个带有后退箭头的新活动。