自定义slideMenu

     android:theme="@style/Theme.AppCompat.Light.NoActionBar">


自定义view

package com.example.slidemenu;

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;

import com.nineoldandroids.view.ViewHelper;

/**
 * Name: SlideMenu
 * Action:
 * Author: liuan
 * creatTime:2017-06-24 19:27
 */

public class SlideMenu extends HorizontalScrollView {

    private LinearLayout mWapepr;
    private ViewGroup mMenu;
    private ViewGroup mContent;
    private int mScreenWidth;
    private int mMenuWidth;

    private int mMenuRightPadding = 50;
    private boolean once = false;
    private boolean isOpen = false;
    private boolean isDrawe = false;
    private boolean isSpeciallyGoodEffct = false;

    public SlideMenu(Context context) {
        this(context, null);
    }

    /**
     * 当使用了自定义属性的时候 会走此构造方法
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取我们自定以的属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenu, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.SlideMenu_rightPadding:
                    mMenuRightPadding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
//
//        //把dp转化为px
//        mMenuRightPadding= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics());
    }

    /**
     * 未使用自定义属性时候 调用
     *
     * @param context
     * @param attrs
     */
    public SlideMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }


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

            once = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 通过设置偏移量 将menu隐藏
     *
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @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) {
        switch (ev.getAction()) {

            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;
        } else {
            this.smoothScrollTo(0, 0);
            isOpen = true;
        }
    }

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


    //切换菜单
    public void toggle() {
        if (isOpen) {
            CloseMenu();
            return;
        }
        openMenu();
    }

    public void setIsDrawe(Boolean is) {
        this.isDrawe = is;
    }

    public void setSpeciallyGoodEffect(Boolean is) {
        this.isSpeciallyGoodEffct = is;
    }

    /**
     * 滚动发生的时候
     *
     * @param l
     * @param t
     * @param oldl
     * @param oldt
     */
    @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
        float translationX = mMenuWidth * scale;
        if(isDrawe&&!isSpeciallyGoodEffct){

        }else if(isDrawe&&isSpeciallyGoodEffct){
            translationX*=0.8;
        }


        if (isDrawe) {

            //调用属性动画 设置

            ViewHelper.setTranslationX(mMenu, translationX);
            //区别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);
        * */
            if (isSpeciallyGoodEffct) {
                float rightScale = 0.7f + 0.3f * scale;
                float leftScale = 1.0f-0.3f * scale;
                float leftAlpha = 0.6f+0.4f * (1-scale);
                //设置content的缩放的中心点
                ViewHelper.setPivotX(mContent,0);
                ViewHelper.setPivotY(mContent,mContent.getHeight()/2);
                ViewHelper.setScaleX(mContent,rightScale);
                ViewHelper.setScaleY(mContent,rightScale);







                ViewHelper.setScaleX(mMenu,leftScale);
                ViewHelper.setScaleY(mMenu,leftScale);
                ViewHelper.setAlpha(mMenu,leftAlpha);




            }

        }
    }
}

//布局 样式1  2 是一样的   


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

    <com.example.slidemenu.SlideMenu
        android:id="@+id/view_slideMenu"
       app:rightPadding="100dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent">
                <Button
                    android:id="@+id/bt_toogle"
                    android:text="切换菜单"
                    android:layout_width="80dp"

                    android:layout_height="wrap_content" />


            </LinearLayout>
        </LinearLayout>

    </com.example.slidemenu.SlideMenu>
</RelativeLayout>
//样式3 4又是一个样子的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.slidemenu.SlideMenu
        android:background="@color/colorPrimary"
        android:id="@+id/view_slideMenu"
        app:rightPadding="100dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent">
                <Button
                    android:id="@+id/bt_toogle"
                    android:text="切换菜单"
                    android:layout_width="80dp"

                    android:layout_height="wrap_content" />


            </LinearLayout>
        </LinearLayout>

    </com.example.slidemenu.SlideMenu>
</RelativeLayout>
left

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_pic1"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp" />
    <TextView
        android:text="第1个item"
        android:layout_toRightOf="@id/iv_pic1"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout><RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_pic2"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp" />
    <TextView
        android:text="第2个item"
        android:layout_toRightOf="@id/iv_pic2"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout><RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_pic3"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp" />
    <TextView
        android:text="第3个item"
        android:layout_toRightOf="@id/iv_pic3"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout><RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_pic4"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp" />
    <TextView
        android:text="第4个item"
        android:layout_toRightOf="@id/iv_pic4"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout><RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_pic5"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp" />
    <TextView
        android:text="第5个item"
        android:layout_toRightOf="@id/iv_pic5"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>



</LinearLayout>
</RelativeLayout>
values/arrr.xml


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

    <declare-styleable name="SlideMenu" >

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

    </declare-styleable>

</resources>


样式1 onCreate未做处理    1-4都有下面这句话

    @OnClick(R.id.bt_toogle)
    public void onClick() {
        viewSlideMenu.toggle();
    }

2   的话onCreate  中多了

   viewSlideMenu.setIsDrawe(true);

3 的话

 viewSlideMenu.setIsDrawe(true);
        viewSlideMenu.setSpeciallyGoodEffect(true);

4 的话

   viewSlideMenu.setIsDrawe(false);
        viewSlideMenu.setSpeciallyGoodEffect(true);


需要的jar包 放在libry目录下点击下载需要2币




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值