添加了拖動功能,按住小表徵圖,拖動超過一半的時候開啟,沒到一半的時候關閉。只添加了OnGestureListener介面和OnTouchListener。
具體代碼看下面:
package com.dl.test;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewTreeObserver;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout.LayoutParams;public class App extends Activity implements OnPanelStatusChangedListener,OnTouchListener,GestureDetector.OnGestureListener{private boolean hasMeasured=false;private LinearLayout layout_left;private LinearLayout layout_right;private ImageView iv;private int layout_left_width,layout_right_width=0;/**每次自動延伸/收縮的範圍*/ private int MAX_WIDTH=0;/**每次自動延伸/收縮的速度*/ private final static int SPEED=20; private GestureDetector mGestureDetector;private boolean isScrolling=false;private float mScrollX; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); layout_left=(LinearLayout)findViewById(R.id.layout_left); layout_right=(LinearLayout)findViewById(R.id.layout_right); iv=(ImageView)findViewById(R.id.iv); iv.setOnTouchListener(this); //定義手勢識別 mGestureDetector = new GestureDetector(this,this); mGestureDetector.setIsLongpressEnabled(false); calculatorWidth(); } @Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();}@Overridepublic void onAttachedToWindow() {// TODO Auto-generated method stubsuper.onAttachedToWindow();}private void calculatorWidth(){ ViewTreeObserver observer = layout_right.getViewTreeObserver();//為了取得控制項的寬// observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {// @Override// public void onGlobalLayout() {// if (hasMeasured == false){// layout_right_width = layout_right.getMeasuredWidth();//105// layout_left_width=layout_left.getMeasuredWidth();//480// MAX_WIDTH=layout_left_width-layout_right_width;//375// hasMeasured = true;// }// }// }); //為了取得控制項的寬 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){ public boolean onPreDraw(){ if (hasMeasured == false){ layout_right_width = layout_right.getMeasuredWidth(); layout_left_width=layout_left.getMeasuredWidth(); MAX_WIDTH=layout_left_width-layout_right_width;// Log.i("tag", "MAX_WIDTH=="+MAX_WIDTH); hasMeasured = true;//設定可拉動容器的寬為全屏(即不可拉動容器)的寬 View layout_max_width=findViewById(R.id.layout_max_width); LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)layout_max_width.getLayoutParams(); lp.width=layout_left_width; layout_max_width.setLayoutParams(lp); } return true; } });}class AsynMove extends AsyncTask<Integer, Integer, Void> { @Override protected Void doInBackground(Integer... params) { int times; if (MAX_WIDTH % Math.abs(params[0]) == 0)// 整除 times = MAX_WIDTH / Math.abs(params[0]); else times = MAX_WIDTH / Math.abs(params[0]) + 1;// 有餘數 for (int i = 0; i < times; i++) { publishProgress(params); try { Thread.sleep(Math.abs(params[0])); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } @Override protected void onProgressUpdate(Integer... params) { LayoutParams lp = (LayoutParams)layout_right.getLayoutParams(); if (params[0] < 0) lp.leftMargin = Math.max(lp.leftMargin + params[0], 0); else lp.leftMargin = Math.min(lp.leftMargin + params[0], MAX_WIDTH); if(lp.leftMargin<=0){//展開之後 onPanelOpened();//調用OPEN回呼函數 } else if(lp.leftMargin>=MAX_WIDTH){//收縮之後 onPanelClosed();//調用CLOSE回呼函數 } layout_right.setLayoutParams(lp); } }@Overridepublic void onPanelOpened() {// TODO Auto-generated method stub//Log.i("tag", "=========onPanelOpened========");}@Overridepublic void onPanelClosed() {// TODO Auto-generated method stub//Log.i("tag", "=========onPanelClosed========");}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubmScrollX=0;isScrolling=false;return true;//需要返回ture才能觸發onSingleTapUp}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubLayoutParams lp = (android.widget.RelativeLayout.LayoutParams)layout_right.getLayoutParams();if (lp.leftMargin >=MAX_WIDTH)// CLOSE的狀態 new AsynMove().execute(new Integer[] { -SPEED });// 負數展開 else if (lp.leftMargin >= 0)// OPEN的狀態 new AsynMove().execute(new Integer[] { SPEED });// 正數收縮return true;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubisScrolling=true;mScrollX+=distanceX;//distanceX:向左為正,右為負LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();lp.leftMargin=lp.leftMargin-(int)mScrollX;if(lp.leftMargin<=0){//展開之後 isScrolling=false;//拖過頭了不需要再執行AsynMove了lp.leftMargin=0;onPanelOpened();//調用OPEN回呼函數 }if(lp.leftMargin>=MAX_WIDTH){//收縮之後 isScrolling=false;lp.leftMargin=MAX_WIDTH;onPanelClosed();//調用CLOSE回呼函數 }layout_right.setLayoutParams(lp);return false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub//拖到一半放開手指時的運動if(event.getAction()==MotionEvent.ACTION_UP && isScrolling==true){LayoutParams lp=(LayoutParams) layout_right.getLayoutParams();if (lp.leftMargin >= (MAX_WIDTH>>1)) {//往左沒超過一半new AsynMove().execute(new Integer[] { SPEED });} else{new AsynMove().execute(new Integer[] { -SPEED });}}return mGestureDetector.onTouchEvent(event); } }
TestMenu.zip (157.1
KB)