自定義控件在應用中非常重要,因此需要認真學習
自定義控件邏輯部分
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(); } }); }
新聞熱點
疑難解答