自定义控件之侧滑菜单

自定义控件在应用中非常重要,因此需要认真学习


自定义控件逻辑部分

package com.bwei.xmo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MainLinearLayout extends LinearLayout {
    //左边距
    int left;

    /**
     * 判断侧滑菜单是否为展开
     * true为关闭,false 为展开
     */

    boolean isClose=false;
    //判断是不是第一次进入的标记
    boolean isFirst=false;
    int x;
    int y;

    int distancex;
    int distancey;
    //以恒定的速度移动
    int speed=5;
    LinearLayout ll_left;
    MainLinearLayout ll_main;

    public void addrest(LinearLayout ll_left,MainLinearLayout ll_main){
        this.ll_left=ll_left;
        this.ll_main=ll_main;
    }

    public MainLinearLayout(Context context) {
        super(context);
    }

    public MainLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MainLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                        /*
                        *  通过宽度获取左边距,因为宽度和左边距相等
                        *  获取左边距
                        *  left 必须为负
                        */

                if (isFirst==false){

                    left=ll_left.getWidth()*(-1);

                    isFirst=true;
                }
                x= (int) event.getX();
                y= (int) event.getY();

                break;
            case MotionEvent.ACTION_MOVE:
                 /**
                 * 得到滑动的距离
                 *比较华东的x和y的值判断时水平滑动还是上下滑动
                 *
                 */

                distancex= (int) (event.getX()-x);
                distancey= (int) (event.getY()-y);
                 //如果滑动的x值大于y则为水平滑动
                if (Math.abs(distancex)>Math.abs(distancey)){
                                /*
                            *  判断是向左滑动还是向右滑动
                            *  distancex 大于0 向右滑动, distancex 小于0 向左滑动
                            */

                    if (distancex>0){
                        left+=speed;

                         //如果左边距大于0,则全部显示
                        if (left>0){
                            left=0;

                        }
                    }else {

                        left-=speed;
                        if (left<ll_left.getWidth()*(-1)){
                            left=ll_left.getWidth()*(-1);

                        }
                    }
                     //动态设置左边距
                    LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) ll_left.getLayoutParams();
                    params.leftMargin=left;
                    ll_left.setLayoutParams(params);

                    LinearLayout.LayoutParams params1= (LinearLayout.LayoutParams) ll_main.getLayoutParams();
                    params1.rightMargin=-(ll_left.getWidth()-Math.abs(left));
                    ll_main.setLayoutParams(params1);

                }

                break;
            case MotionEvent.ACTION_UP:
                          /*
                        * 抬起的时候,首先判断是左滑动还是右滑动
                        * 根据 distancex 判断,如果大于0 是向右滑动,如果小于 0 向左滑动
                        */

                            /*
                            * 向右滑动的时候如果划出来的左边宽度小于整体宽度的一般的时候,设置自定弹回去
                            * 向左滑动的时候,如果进去的宽度大于一般,设置宽度为宽度,负责反之
                            */

                if (Math.abs(left)>ll_left.getWidth()/2){

                    left=ll_left.getWidth()*(-1);
                    isClose=true;
                }else{
                    left=0;
                    isClose=false;
                }

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_left.getLayoutParams();
                params.leftMargin = left;
                ll_left.setLayoutParams(params);

                LinearLayout.LayoutParams params1= (LinearLayout.LayoutParams) ll_main.getLayoutParams();
                params1.rightMargin=-(ll_left.getWidth()-Math.abs(left));
                ll_main.setLayoutParams(params1);
                break;
        }
        return true;
    }

    public void LeftClose(){

        if (isClose==false){
             //当isClose==false展开侧滑菜单
            isClose=true;
            Left(0);
        }else{
            isClose=false;
            Left(-ll_left.getWidth());
        }


    }
    public void Left(int left){
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_left.getLayoutParams();
        params.leftMargin = left;
        ll_left.setLayoutParams(params);


        LinearLayout.LayoutParams params1= (LinearLayout.LayoutParams) ll_main.getLayoutParams();
        params1.rightMargin=-(ll_left.getWidth()-Math.abs(left));
        ll_main.setLayoutParams(params1);
    }

}


//xml布局部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        >
   <!--左边的布局-->
    <LinearLayout
        android:id="@+id/ll_left"
        android:background="#ff00ff"
        android:layout_marginLeft="-150dp"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
    </LinearLayout>
    <!--主布局-->
    <com.bwei.xmo.MainLinearLayout
        android:id="@+id/ll_main"
        android:background="#00ff00"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv_main_click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="展开"
            android:textSize="30sp"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/ic_launcher"
            />
    </com.bwei.xmo.MainLinearLayout>

</LinearLayout>


//主函数调用

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_main = (MainLinearLayout) findViewById(R.id.ll_main);
        main = (LinearLayout) findViewById(R.id.activity_main);
        tv_main_click = (TextView) findViewById(R.id.tv_main_click);
        ll_main.addrest(ll_left,ll_main);
        tv_main_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               ll_main.LeftClose();
            }
        });

    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值