美文网首页
Android侧滑菜单

Android侧滑菜单

作者: 百里漫步 | 来源:发表于2017-10-29 10:28 被阅读0次

一、侧拉菜单
1、首先创建一个菜单页
layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:id="@+id/img_1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/img_1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/img_1"
        android:text="@string/amaze"
        android:textSize="20sp"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true"
        />
</RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:id="@+id/img_2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_centerVertical="true"
                android:src="@mipmap/img_2"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_2"
                android:text="@string/caculator"
                android:textSize="20sp"
                android:layout_marginLeft="20dp"
                android:layout_centerVertical="true"
                />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:id="@+id/img_3"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_centerVertical="true"
                android:src="@mipmap/img_3"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_3"
                android:text="@string/rili"
                android:textSize="20sp"
                android:layout_marginLeft="20dp"
                android:layout_centerVertical="true"
                />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:id="@+id/img_4"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_centerVertical="true"
                android:src="@mipmap/img_4"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_4"
                android:text="@string/weather"
                android:textSize="20sp"
                android:layout_marginLeft="20dp"
                android:layout_centerVertical="true"
                />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:id="@+id/img_5"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_centerVertical="true"
                android:src="@mipmap/img_5"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_5"
                android:text="@string/setting"
                android:textSize="20sp"
                android:layout_marginLeft="20dp"
                android:layout_centerVertical="true"
                />
        </RelativeLayout>

    </LinearLayout>
</RelativeLayout>

2、用到控件HorizontalScrollView将菜单和内容页面放到一起
我们需要自己实现HorizontalScrollView类
main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:hyman="http://schemas.android.com/apk/res-auto"
    tools:context="example.com.myapplication.MainActivity">
    <example.com.myapplication.SlidingMenu
        android:id="@+id/id_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/img_frame_background"
        hyman:rightPadding="80dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <include layout="@layout/layout"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@mipmap/qq" >

                <TextView
                    android:onClick="toggleMenu"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:textColor="#ffffff"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:text="菜单"
                    />
            </LinearLayout>
        </LinearLayout>
    </example.com.myapplication.SlidingMenu>

</android.support.constraint.ConstraintLayout>

SlidingMenu.java

package example.com.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SlidingMenu extends HorizontalScrollView {

    private LinearLayout mWapper;
    private ViewGroup mMenu;
    private ViewGroup mContent;
    private int mScreenWidth;

    private int mMenuWidth;
    // dp
    private int mMenuRightPadding = 50;

    private boolean once;

    private boolean isOpen;


    /**
     * 未使用自定义属性时使用attrs
     * @param context
     * @param attrs
     */
    //两个构造方法指向三个
    public SlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    /**
     * 当使用了自定义属性时,会调用此构造方法
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        // 获取我们定义的属性,xml中的
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.SlidingMenu, defStyle, 0);

        int n = a.getIndexCount();
        //循环获取属性
        for (int i = 0; i < n; i++)
        {
            int attr = a.getIndex(i);
            switch (attr)
            {
                case R.styleable.SlidingMenu_rightPadding:
                    //取属性值,dp转换成像素px
                    mMenuRightPadding = a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_DIP, 50, context
                                            .getResources().getDisplayMetrics()));
                    break;
            }
        }
        //释放TypeArray
        a.recycle();

        //获取屏幕宽度
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;

    }

    //一个参数构造方法指向两个参数构造方法
    public SlidingMenu(Context context)
    {
        this(context, null);
    }

    /**
     * 设置子View的宽和高 设置自己的宽和高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (!once)
        {
            mWapper = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) mWapper.getChildAt(0);
            mContent = (ViewGroup) mWapper.getChildAt(1);
            mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth
                    - mMenuRightPadding;
            mContent.getLayoutParams().width = mScreenWidth;
            once = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 通过设置偏移量,将menu隐藏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        if (changed)
        {
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_UP:
                // 隐藏在左边的宽度
                int scrollX = getScrollX();
                if (scrollX >= mMenuWidth / 2)
                {
                    this.smoothScrollTo(mMenuWidth, 0);
                    isOpen = false;
                } else
                {
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 打开菜单
     */
    public void openMenu()
    {
        if (isOpen)
            return;
        this.smoothScrollTo(0, 0);
        isOpen = true;
    }

    public void closeMenu()
    {
        if (!isOpen)
            return;
        this.smoothScrollTo(mMenuWidth, 0);
        isOpen = false;
    }

    /**
     * 切换菜单
     */
    public void toggle()
    {
        if (isOpen)
        {
            closeMenu();
        } else
        {
            openMenu();
        }
    }
    //实现抽屉式侧拉菜单
//    @Override
//    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//        super.onScrollChanged(l, t, oldl, oldt);
//        float scale = l * 1.0f / mMenuWidth; // 1 ~ 0
//        // 调用属性动画,设置TranslationX
//        ViewHelper.setTranslationX(mMenu, mMenuWidth * scale );
//    }




    //带有动画的侧拉菜单
//    /**
//     * 滚动发生时
//     */
//    @Override
//    protected void onScrollChanged(int l, int t, int oldl, int oldt)
//    {
//        super.onScrollChanged(l, t, oldl, oldt);
//        float scale = l * 1.0f / mMenuWidth; // 1 ~ 0
//
//        /**
//         * 区别1:内容区域1.0~0.7 缩放的效果 scale : 1.0~0.0 0.7 + 0.3 * scale
//         *
//         * 区别2:菜单的偏移量需要修改
//         *
//         * 区别3:菜单的显示时有缩放以及透明度变化 缩放:0.7 ~1.0 1.0 - scale * 0.3 透明度 0.6 ~ 1.0
//         * 0.6+ 0.4 * (1- scale) ;
//         *
//         */
//        float rightScale = 0.7f + 0.3f * scale;
//        float leftScale = 1.0f - scale * 0.3f;
//        float leftAlpha = 0.6f + 0.4f * (1 - scale);
//
//        // 调用属性动画,设置TranslationX
//        ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.8f);
//
//        ViewHelper.setScaleX(mMenu, leftScale);
//        ViewHelper.setScaleY(mMenu, leftScale);
//        ViewHelper.setAlpha(mMenu, leftAlpha);
//        // 设置content的缩放的中心点
//        ViewHelper.setPivotX(mContent, 0);
//        ViewHelper.setPivotY(mContent, mContent.getHeight() / 2);
//        ViewHelper.setScaleX(mContent, rightScale);
//        ViewHelper.setScaleY(mContent, rightScale);
//
//    }
}

3、再设置自定义属性,可自行修改菜单距右边距
res/values/attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="rightPadding" format="dimension"></attr>

    <declare-styleable name="SlidingMenu">
        <attr name="rightPadding"></attr>
    </declare-styleable>

</resources>

在main_activity.xml中添加属性 hyman:rightPadding="80dp"
注意添加自己的命名空间 xmlns:hyman="http://schemas.android.com/apk/res-auto"

到这里就可以实现侧拉菜单,效果如下:

sliding1.gif

将上面的抽屉式菜单代码解除注释,可以实现抽屉式侧滑,需引入相关第三方jar包:nineoldandroids-2.4.0.jar

效果如下:


sliding2.gif

将上方带动画效果解除注释,实现带有动画的侧滑菜单,效果如下:

sliding3.gif

相关文章

网友评论

      本文标题:Android侧滑菜单

      本文链接:https://www.haomeiwen.com/subject/rfzepxtx.html