更改导航抽屉的过渡

时间:2018-11-27 14:08:18

标签: android android-layout android-animation navigation-drawer android-transitions

我想打开带有淡入和淡出过渡的导航抽屉, 我尝试了大多数事情,但没有成功,因此请帮助我在导航抽屉上存档这种过渡效果。

我有这种类型的代码。

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (drawer.isDrawerOpen(Gravity.LEFT)) {
                drawer.closeDrawers();

//                    TransitionManager.beginDelayedTransition(drawer, fadeOut);

            } else {
                drawer.openDrawer(Gravity.LEFT);

//                    TransitionManager.beginDelayedTransition(drawer, fadeIn);
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以通过创建以下动画xml来创建淡入/淡出动画:

<!-- fade out-->    
<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator">
        <alpha
            android:duration="2000"
            android:fromAlpha="1.0"
            android:toAlpha="0.1" >
        </alpha>
    </set>

r

<!--fade in-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
   <alpha
        android:duration="2000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0">
    </alpha>
</set>

然后覆盖DrawerLayout.DrawerListener中的onDrawerSlide,以便在滑动菜单打开/关闭时将适当的动画添加到您的抽屉菜单中。例如:

mDrawerLayout.addDrawerListener(
                new DrawerLayout.DrawerListener() {
                    @Override
                    public void onDrawerSlide(View drawerView, float slideOffset) {
                        // Respond when the drawer's position changes
                        Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
                        drawerView.startAnimation(animFadeIn);
                        }

                    @Override
                    public void onDrawerOpened(View drawerView) {
                        // Respond when the drawer is opened
                    }

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        // Respond when the drawer is closed
                    }

                    @Override
                    public void onDrawerStateChanged(int newState) {
                        // Respond when the drawer motion state changes
                    }
                }
        );