自定義控件在應(yīng)用中非常重要,因此需要認(rèn)真學(xué)習(xí)
自定義控件邏輯部分
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; /** * 判斷側(cè)滑菜單是否為展開(kāi) * true為關(guān)閉,false 為展開(kāi) */ boolean isClose=false; //判斷是不是第一次進(jìn)入的標(biāo)記 boolean isFirst=false; int x; int y; int distancex; int distancey; //以恒定的速度移動(dòng) 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: /* * 通過(guò)寬度獲取左邊距,因?yàn)閷挾群妥筮吘嘞嗟?nbsp; * 獲取左邊距 * left 必須為負(fù) */ if (isFirst==false){ left=ll_left.getWidth()*(-1); isFirst=true; } x= (int) event.getX(); y= (int) event.getY(); break; case MotionEvent.ACTION_MOVE: /** * 得到滑動(dòng)的距離 *比較華東的x和y的值判斷時(shí)水平滑動(dòng)還是上下滑動(dòng) * */ distancex= (int) (event.getX()-x); distancey= (int) (event.getY()-y); //如果滑動(dòng)的x值大于y則為水平滑動(dòng) if (Math.abs(distancex)>Math.abs(distancey)){ /* * 判斷是向左滑動(dòng)還是向右滑動(dòng) * distancex 大于0 向右滑動(dòng), distancex 小于0 向左滑動(dòng) */ 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); } } //動(dòng)態(tài)設(shè)置左邊距 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: /* * 抬起的時(shí)候,首先判斷是左滑動(dòng)還是右滑動(dòng) * 根據(jù) distancex 判斷,如果大于0 是向右滑動(dòng),如果小于 0 向左滑動(dòng) */ /* * 向右滑動(dòng)的時(shí)候如果劃出來(lái)的左邊寬度小于整體寬度的一般的時(shí)候,設(shè)置自定彈回去 * 向左滑動(dòng)的時(shí)候,如果進(jìn)去的寬度大于一般,設(shè)置寬度為寬度,負(fù)責(zé)反之 */ 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){ //當(dāng)isClose==false展開(kāi)側(cè)滑菜單 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="展開(kāi)" 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ù)調(diào)用
@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(); } }); }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注