android手勢滑動關閉當前activity
阿新 • • 發佈:2019-02-12
package com.bruce.testeventandscroll; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.Window; /** * 手勢滑動測試demo * 步驟, * 1、例項化GestureDetector物件 * 2、例項化 GestureDetector.OnGestureListener 手勢監聽物件 * 3、覆寫onTouchEvent方法,在onTouchEvent方法中將event物件傳給gestureDetector.onTouchEvent(event);處理。 */ public class MainActivity extends Activity { final int RIGHT = 0; final int LEFT = 1; private GestureDetector gestureDetector;//要想使用手勢滑動,就必須要這個GestureDetector物件 /** * 要實現手指在螢幕上左右滑動的事件需要例項化物件GestureDetector,new GestureDetector(MainActivity.this,onGestureListener); * 首先實現監聽物件GestureDetector.OnGestureListener,根據x或y軸前後變化座標來判斷是左滑動還是右滑動 * 並根據不同手勢滑動做出事件處理doResult(int action), 然後覆寫onTouchEvent方法,在onTouchEvent方法中將event物件傳給gestureDetector.onTouchEvent(event);處理。 */ /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 此處需要gestureDetector物件例項化*/ gestureDetector = new GestureDetector(MainActivity.this, onGestureListener); /** * //需要傳入一個Context和一個手勢監聽OnGestureListener * //下面是原始碼 public GestureDetector(Context context, OnGestureListener listener) { this(context, listener, null); } */ } /** * 在此例項化OnGestureListener監聽的例項 */ private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //e1就是初始狀態的MotionEvent物件,e2就是滑動了過後的MotionEvent物件 //velocityX和velocityY就是滑動的速率 float x = e2.getX() - e1.getX();//滑動後的x值減去滑動前的x值 就是滑動的橫向水平距離(x) float y = e2.getY() - e1.getY();//滑動後的y值減去滑動前的y值 就是滑動的縱向垂直距離(y) Log.w("tag", "x>" + x); Log.w("tag", "y>" + y); Log.w("tag", "velocityX>" + velocityX); Log.w("tag", "velocityY>" + velocityY); //如果滑動的橫向距離大於100,表明是右滑了,那麼就執行下面的方法,可以是關閉當前的activity if (x > 100) { doResult(RIGHT); Log.w("tag", "RIGHT>" + x); } //如果滑動的橫向距離大於100,表明是左滑了(因為左滑為負數,所以距離大於100就是x值小於-100) if (x < -100) { Log.w("tag", "LEFT>" + x); doResult(LEFT); } return true; } }; /** * 將MotionEvent事件處理交給gestureDetector物件 */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println(" ACTION_DOWN");//手指在螢幕上按下 break; case MotionEvent.ACTION_MOVE: System.out.println(" ACTION_MOVE");//手指正在螢幕上滑動 break; case MotionEvent.ACTION_UP: System.out.println(" ACTION_UP");//手指從螢幕擡起了 break; default: break; } return gestureDetector.onTouchEvent(event); } public void doResult(int action) { switch (action) { case RIGHT: System.out.println("go right"); finish(); break; case LEFT: System.out.println("go left"); break; } } }
手指滑動了一下,log列印如下
03-23 23:18:38.888 17282-17282/com.bruce.testeventandscroll I/System.out: Button ACTION_DOWN 03-23 23:18:38.956 17282-17282/com.bruce.testeventandscroll I/System.out: Button ACTION_MOVE 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll I/System.out: Button ACTION_MOVE 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll I/System.out: Button ACTION_MOVE 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll I/System.out: Button ACTION_UP 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll W/tag: x>-3.994461 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll W/tag: y>-22.982056 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll W/tag: velocityX>6.796303 03-23 23:18:39.008 17282-17282/com.bruce.testeventandscroll W/tag: velocityY>-1417.2805