DrawerLayout侧拉

来源:互联网 发布:网络数据采集卡 编辑:程序博客网 时间:2024/05/16 10:11
导入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);    }

原创粉丝点击