导航抽屉android过渡

问题描述 投票:6回答:2

任何人都知道如何实现这种类型的过渡。当我们打开Navagation抽屉全屏正在获得这样的动画。我也看了reside menu,但这里的菜单预定义不是我想要的。

我也尝试过NavigationDrawer但没有成功。

   drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            float moveFactor = (linearLayout.getWidth() * slideOffset);

            float min = 0.9f;
            float max = 1.0f;
            float scaleFactor = (max - ((max - min) * slideOffset));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            {
                linearLayout.setTranslationX(moveFactor);
                linearLayout.setScaleX(scaleFactor);
                linearLayout.setScaleY(scaleFactor);
            }
            else
            {
                AnimationSet animSet = new AnimationSet(true);


                TranslateAnimation anim = new TranslateAnimation(lastTranslate, moveFactor, 0.0f, 0.0f);
                anim.setDuration(0);
                anim.setFillAfter(true);
                animSet.addAnimation(anim);
                ScaleAnimation scale = new ScaleAnimation(1.15f, 1.0f, 1.15f, 1.0f);
                scale.setDuration(10);
                scale.setFillAfter(true);
                animSet.addAnimation(scale);

                drawerView.startAnimation(animSet);

                lastTranslate = moveFactor;
            }
        }

        @Override
        public void onDrawerOpened(View drawerView) {

        }

        @Override
        public void onDrawerClosed(View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });

提前致谢

Animation in navigation drawer

android animation navigation-drawer drawerlayout android-navigation-drawer
2个回答
4
投票

最后,我通过这个link for original post得到了答案。请看这里

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="test.pyramidions.com.dynamicview2.MainActivity"
tools:openDrawer="start">


<RelativeLayout
    android:id="@+id/holder"
    android:layout_width="match_parent"
    android:background="@drawable/shadow"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />


        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>
</RelativeLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/drawer_menu" />

 </android.support.v4.widget.DrawerLayout>

而对于MainActivity.java,请看下面

        public class MainActivity extends AppCompatActivity {
public TabsPagerAdapter tabsPagerAdapter;
public static ViewPager pager;
int Numboftabs = 2;
Toolbar toolbar;
public NavigationView navigationView;
public DrawerLayout drawer;
View holderView, contentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    holderView = findViewById(R.id.holder);
    contentView = findViewById(R.id.content);
    tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager(), Numboftabs);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(tabsPagerAdapter);
    toolbar.setNavigationIcon(new DrawerArrowDrawable(this));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                                             @Override
                                             public void onClick(View v) {
                                                 if (drawer.isDrawerOpen(navigationView)) {
                                                     drawer.closeDrawer(navigationView);
                                                 } else {
                                                     drawer.openDrawer(navigationView);
                                                 }
                                             }
                                         }
    );


    drawer.setScrimColor(Color.TRANSPARENT);
    drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                                 @Override
                                 public void onDrawerSlide(View drawer, float slideOffset) {


                                     contentView.setX(navigationView.getWidth() * slideOffset);
                                     RelativeLayout.LayoutParams lp =
                                             (RelativeLayout.LayoutParams) contentView.getLayoutParams();
                                     lp.height = drawer.getHeight() -
                                             (int) (drawer.getHeight() * slideOffset * 0.3f);
                                     lp.topMargin = (drawer.getHeight() - lp.height) / 2;
                                     contentView.setLayoutParams(lp);
                                 }

                                 @Override
                                 public void onDrawerClosed(View drawerView) {
                                 }
                             }
    );

}

0
投票

抽屉行为是一个库使用Android DrawerLayout支持库作为父类[易于迁移],它在抽屉上提供额外的行为,例如,在幻灯片上的抽屉时移动视图或缩放视图的高度。

如果当前项目使用Android DrawerLayout支持库并且有点无聊的效果。然后,只需更改布局代码并调用动画/效果所需的方法。

Check out github code

摇篮

dependencies {
   implementation 'com.infideap.drawerbehavior:drawer-behavior:0.1.5'
}

如果gradle无法同步,您可以在项目级gradle中包含此行,

repositories {
 maven{
   url "https://dl.bintray.com/infideap2/Drawer-Behavior"
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.