国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

自定義控件之側滑菜單

2019-11-09 15:11:11
字體:
來源:轉載
供稿:網(wǎng)友

自定義控件在應用中非常重要,因此需要認真學習

自定義控件邏輯部分

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);                        }                    }                    //動態(tài)設置左邊距                    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:                         /*                        * 抬起的時候,首先判斷是左滑動還是右滑動                        * 根據(jù) 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>

//主函數(shù)調用

 @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();            }        });    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 井冈山市| 柳州市| 岳西县| 舞阳县| 崇阳县| 黔东| 客服| 杭锦后旗| 垣曲县| 大理市| 宁明县| 鄯善县| 都安| 崇信县| 句容市| 开阳县| 揭东县| 莲花县| 江源县| 元朗区| 卢龙县| 铜梁县| 安顺市| 通化市| 黔江区| 伊宁县| 江永县| 革吉县| 鸡东县| 临沂市| 卫辉市| 星子县| 军事| 开鲁县| 绍兴县| 正定县| 通城县| 县级市| 大宁县| 郑州市| 长泰县|