android 判断左右滑动,上下滑动的GestureDetector简单手势检测

Posted Sharley

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 判断左右滑动,上下滑动的GestureDetector简单手势检测相关的知识,希望对你有一定的参考价值。

直接加入监听GestureDetector放在需要判断滑动手势的地方:

 1 import android.app.Activity;  
 2 import android.os.Bundle;  
 3 import android.util.Log;  
 4 import android.view.GestureDetector;  
 5 import android.view.GestureDetector.OnGestureListener;  
 6 import android.view.MotionEvent;  
 7   
 8 public class MainActivity extends Activity {  
 9     protected static final float FLIP_DISTANCE = 50;  
10     GestureDetector mDetector;  
11   
12     @Override  
13     protected void onCreate(Bundle savedInstanceState) {  
14         super.onCreate(savedInstanceState);  
15         setContentView(R.layout.activity_main);  
16   
17         mDetector = new GestureDetector(this, new OnGestureListener() {  
18   
19             @Override  
20             public boolean onSingleTapUp(MotionEvent e) {  
21                 // TODO Auto-generated method stub  
22                 return false;  
23             }  
24   
25             @Override  
26             public void onShowPress(MotionEvent e) {  
27                 // TODO Auto-generated method stub  
28   
29             }  
30   
31             @Override  
32             public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
33                 // TODO Auto-generated method stub  
34                 return false;  
35             }  
36   
37             @Override  
38             public void onLongPress(MotionEvent e) {  
39                 // TODO Auto-generated method stub  
40   
41             }  
42   
43             /** 
44              *  
45              * e1 The first down motion event that started the fling. e2 The 
46              * move motion event that triggered the current onFling. 
47              */  
48             @Override  
49             public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
50                 if (e1.getX() - e2.getX() > FLIP_DISTANCE) {  
51                     Log.i("TAG", "<--- left, left, go go go");  
52                     return true;  
53                 }  
54                 if (e2.getX() - e1.getX() > FLIP_DISTANCE) {  
55                     Log.i("TAG", "right, right, go go go --->");  //忽然觉得这个log好智障...
56                     return true;  
57                 }  
58                 if (e1.getY() - e2.getY() > FLIP_DISTANCE) {  
59                     Log.i("TAG", "向上滑...");  
60                     return true;  
61                 }  
62                 if (e2.getY() - e1.getY() > FLIP_DISTANCE) {  
63                     Log.i("TAG", "向下滑...");  
64                     return true;  
65                 }  
66   
67                 Log.d("TAG", e2.getX() + " " + e2.getY());  
68   
69                 return false;  
70             }  
71   
72             @Override  
73             public boolean onDown(MotionEvent e) {  
74                 // TODO Auto-generated method stub  
75                 return false;  
76             }  
77         });  
78     }  
79   
80     @Override  
81     public boolean onTouchEvent(MotionEvent event) {  
82         return mDetector.onTouchEvent(event);  
83     }  
84 }  

 

以上是关于android 判断左右滑动,上下滑动的GestureDetector简单手势检测的主要内容,如果未能解决你的问题,请参考以下文章

android fragment怎么监听上下滑动

解决Android下元素滑动问题

Android 中怎么判断WebView已经滑动到底部或顶部还在滑动类似上拉刷新下来加载更多.

android实现上下滑动

UIPanGestureRecognizer上下左右滑动方向判断算法

swiperjs怎么判断左右滑动