自定义侧滑框

来源:互联网 发布:小米开源软件 编辑:程序博客网 时间:2024/06/07 05:09

/////布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main"
    android:orientation="horizontal">



    <!-- 左边布局 -->
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/ll_left"
        android:layout_marginLeft="-300dp"
        android:background="#929292"
        android:layout_width="150dp"
        android:layout_height="match_parent">

    </LinearLayout>



    <!-- 主布局 -->
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/ll_main"
        android:background="#FF0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>



</LinearLayout>

////////////////代码实现

package com.bawei.testdown;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class MyActivity extends AppCompatActivity {


    /*
    * ll_left 左边布局
    * ll_main 主布局
    */
    LinearLayout ll_left;
    LinearLayout ll_main;

    LinearLayout main;


    /*
    * x 点击初始位置x
    * y 单击出事位置y
    */

    int x;
    int y;

    /*
    * distancex 滑动距离
    * distancey 滑动距离
    */

    int distancex;
    int distancey;


    //左边距
    int left;


    /*
    * 是不是第一次点击
    * false 第一次点击,true 不是第一次点击
    */
    boolean isClick = false;


    /*
    * speed 以恒定的速度移动
    *
    */

    int speed = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        //布局
        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_main = (LinearLayout) findViewById(R.id.ll_main);

        main = (LinearLayout) findViewById(R.id.main);


        main.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN: //按下


                        /*
                        *  通过宽度获取左边距,因为宽度和左边距相等
                        *  获取左边距
                        *  left 必须为负
                        */

                        if (isClick == false) {
                            left = ll_left.getWidth();
                            left = left * (-1);
                            isClick = true;
                        }


                        x = (int) event.getX();
                        y = (int) event.getY();

                        break;
                    case MotionEvent.ACTION_MOVE: //滑动

                        distancex = (int) (event.getX() - x);
                        distancey = (int) (event.getY() - y);



                        /*
                        * 如何判断当前是水平滑动还是垂直滑动
                        * distancex 绝对值大于 distancey 水平滑动
                        */

                        if (Math.abs(distancex) > Math.abs(distancey)) {


                            /*
                            *  判断是向左滑动还是向右滑动
                            *  distancex 大于0 向右滑动, distancex 小于0 向左滑动
                            */

                            if (distancex >= 0) {//向右滑动

                                /*
                                * 想右滑动的时候 左边距等于 控件的宽度 减去 滑动的距离 distancex
                                *
                                */

//                                left += distancex;
                                left += speed;
                                //坐边距变为负的

                                /*
                                * left 如果大于0 就设置我 0
                                *
                                * */

                                if (left > 0) {
                                    left = 0;
                                }


                            } else {//向左滑动


                                /*
                                *  向左滑动
                                *  左边距变为负值
                                */

//                                left += distancex;
                                left -= speed;

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

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

                            }



                            /*
                            * left 的宽度如果大于0px 停止滑动
                            */

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


                        }

                        break;

                    case MotionEvent.ACTION_UP: //抬起

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


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

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

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

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


                        break;
                }


                return true;
            }
        });
    }


}



0 0
原创粉丝点击