DrawerLayout侧拉

本文介绍了一种在Android应用中实现侧滑菜单效果的方法。通过使用DrawerLayout组件,并结合nineoldandroids库进行动画效果定制,实现了流畅的侧滑动画。文章提供了详细的布局配置和代码示例。
导入nineoldandroids-2.4.0.jar包

布局:将DrawerLayout作为跟布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/drawerLayout"
    tools:context="com.example.e.toutiao.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_above="@+id/radioGroup"
            android:layout_weight="1"></FrameLayout>

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:padding="5dp">

            <RadioButton
                android:id="@+id/radio01"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/shouye"
                android:gravity="center"
                android:tag="0"
                android:text="首页"
                android:textColor="@drawable/color_ziti"
                android:textSize="15dp" />

            <RadioButton
                android:id="@+id/radio02"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/shiping"
                android:gravity="center"
                android:tag="1"
                android:text="视频"
                android:textColor="@drawable/color_ziti"
                android:textSize="15dp" />

            <RadioButton
                android:id="@+id/radio03"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/weitoutiao"
                android:gravity="center"
                android:tag="2"
                android:text="微头条"
                android:textColor="@drawable/color_ziti"
                android:textSize="15dp" />

            <RadioButton
                android:id="@+id/radio04"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/denglu"
                android:gravity="center"
                android:tag="3"
                android:text="未登录"
                android:textColor="@drawable/color_ziti"
                android:textSize="15dp" />
        </RadioGroup>
    </LinearLayout>
    <fragment
        android:id="@+id/left_menu"
        android:name="fragment.MyLeftFragment"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:tag="LEFT"></fragment>

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

------------------------------------------------------
代码: 
/**
     * 侧拉菜单效果
     */
    private void initEvents() {
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            /**
             * 当抽屉被滑动的时候调用此方法
             * slideOffset 表示 滑动的幅度(0-1)
             */
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //获得drawerLayout里View
                View content = drawerLayout.getChildAt(0);
                View menu = drawerView;
                //侧拉比例
                float scale = 1 - slideOffset;

//                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT")) {
                    float leftScale = 1 - 0.3f * scale;
                    //横向缩放比例
                    ViewHelper.setScaleX(menu, leftScale);
                    //纵向缩放比例
                    ViewHelper.setScaleY(menu, leftScale);
                    //透明度
                    ViewHelper.setAlpha(menu, 0.6f + 0.4f * (1 - scale));
                    //横向平移值
                    ViewHelper.setTranslationX(content, menu.getMeasuredWidth() * (1 - scale));
                    //设置横向中心点
                    ViewHelper.setPivotX(content, 0);
                    //设置纵向中心点
                    ViewHelper.setPivotY(content, content.getMeasuredHeight() / 2);
                    //屏幕刷新
                    content.invalidate();
//                    ViewHelper.setScaleX(content, rightScale);
//                    ViewHelper.setScaleY(content, rightScale);
                } else {

                    ViewHelper.setTranslationX(content, -menu.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotY(content, content.getMeasuredHeight() / 2);
                    ViewHelper.setPivotX(content, content.getMeasuredWidth());
                    content.invalidate();
//                    ViewHelper.setScaleY(content, rightScale);
//                    ViewHelper.setScaleX(content, rightScale);
                }
            }
            /**
             * 当一个抽屉被完全打开的时候被调用
             */
            @Override
            public void onDrawerOpened(View drawerView) {

            }
            /**
             * 当一个抽屉完全关闭的时候调用此方法
             */
            @Override
            public void onDrawerClosed(View drawerView) {
                //只有编程才能将其弹出
                drawerLayout.setDrawerLockMode(
                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
            }
            /**
             * 当抽屉滑动状态改变的时候被调用
             * 状态值是STATE_IDLE(闲置--0), STATE_DRAGGING(拖拽的--1), STATE_SETTLING(固定--2)中之一。
             * 抽屉打开的时候,点击抽屉,drawer的状态就会变成STATE_DRAGGING,然后变成STATE_IDLE
             */
            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    private void initView() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
//        只有编程才能将其弹出
        drawerLayout.setDrawerLockMode(
                DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值