以编程方式动画化ActionBarDrawerToggle图标

5

我希望能够手动将抽屉图标从汉堡变成箭头,反之亦然。不仅在拖动抽屉时才进行动画,这种做法可行吗?我正在使用支持库appcompat-v7:21。

此外,我找不到android.support.v7.app.ActionBarDrawerToggle的源代码,这会很有帮助。

2个回答

13

我找到了一种使用简单的ValueAnimator和 .onDrawerSlide 方法来使图标动画化的方法。

    ValueAnimator anim = ValueAnimator.ofFloat(start, end);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float slideOffset = (Float) valueAnimator.getAnimatedValue();
            drawerToggle.onDrawerSlide(drawerLayout, slideOffset);
        }
    });
    anim.setInterpolator(new DecelerateInterpolator());
    anim.setDuration(300);
    anim.start();

但也许还有更好的解决方案。


谢谢你的回答!这对我来说完美地解决了问题,虽然我同意可能有更好的解决方案。但现在,这是唯一有效的解决方案 :) - MathieuMaree

4
// define your drawer layout
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    // define your actionbar toggle
    final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            // Pass in context
            this,
            // Pass in your drawer layout
            drawerLayout,
            // Pass a String resource to describe the "open drawer" action for accessibility
            R.string.open_drawer,
            // Pass a String resource to describe the "close drawer" action for accessibility
            R.string.close_drawer
    );
    // add a drawerListener to your drawer layout
    drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            // Here's where the animation happens.
            // the provided float starts at 0 (drawer closed) and goes to 1 (drawer open) hitting all points in between.

            // ActionBarDrawerToggle has an onDrawerSlide method that takes your drawer layout and the provided float from the onDrawerSlide constructor.
            // By passing the slideOffSet from the drawer's OnDrawerSlide method into the toggle, it maps the animation to the provided float.
            toggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerOpened(View view) {

        }

        @Override
        public void onDrawerClosed(View view) {

        }

        @Override
        public void onDrawerStateChanged(int i) {

        }
    });

编辑:上述方法可行,但我发现一种更加优雅的方法来实现这个功能。通过将切换按钮设置为抽屉的监听器,状态会被默认处理。请参见以下内容:

        // define your drawer layout
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    // define your actionbar drawertoggle
    drawerToggle = new ActionBarDrawerToggle(
            // Pass in context
            this,
            // Pass in your drawer layout
            drawerLayout,
            // Attach the toolbar
            toolbar,
            // Pass a String resource to describe the "open drawer" action for accessibility
            R.string.open_drawer,
            // Pass a String resource to describe the "close drawer" action for accessibility
            R.string.close_drawer
    )  {
        // attach drawer listener states to the ActionBarDrawerToggle
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接