免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1446 | 回复: 5

[Android] 【转】用户手势检测-GestureDetector使用详解 [复制链接]

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:41 |显示全部楼层
本帖最后由 baopbird2005 于 2015-09-21 10:02 编辑

一、概述
当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等。
一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)。
Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的。

GestureDetector这个类对外提供了两个接口和一个外部类
接口:OnGestureListener,OnDoubleTapListener
内部类:SimpleOnGestureListener

这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是static class,程序员可以在外部继承这个类,重写里面的手势处理方法。

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:46 |显示全部楼层
二、GestureDetector.OnGestureListener---接口
1、基本讲解
如果我们写一个类并implements OnGestureListener,会提示有几个必须重写的函数,加上之后是这个样子的:
  1. private class gesturelistener implements GestureDetector.OnGestureListener{  
  2.   
  3.     public boolean onDown(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public void onShowPress(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.          
  11.     }  
  12.   
  13.     public boolean onSingleTapUp(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17.   
  18.     public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  19.             float distanceX, float distanceY) {  
  20.         // TODO Auto-generated method stub  
  21.         return false;  
  22.     }  
  23.   
  24.     public void onLongPress(MotionEvent e) {  
  25.         // TODO Auto-generated method stub  
  26.          
  27.     }  
  28.   
  29.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  30.             float velocityY) {  
  31.         // TODO Auto-generated method stub  
  32.         return false;  
  33.     }  
  34.       
  35. }  
复制代码
可见,这里总共重写了六个函数,这些函数都在什么情况下才会触发呢,下面讲一下:
OnDown(MotionEvent e):用户按下屏幕就会触发;
onShowPress(MotionEvent e):如果是按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行,具体这个瞬间是多久,我也不清楚呃……
onLongPress(MotionEvent e):长按触摸屏,超过一定时长,就会触发这个事件
    触发顺序:
    onDown->onShowPress->onLongPress
onSingleTapUp(MotionEvent e):从名子也可以看出,一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再算是Single操作了,所以也就不会触发这个事件
    触发顺序:
    点击一下非常快的(不滑动)Touchup:
    onDown->onSingleTapUp->onSingleTapConfirmed
    点击一下稍微慢点的(不滑动)Touchup:
    onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发   
     参数解释:
    e1:第1个ACTION_DOWN MotionEvent
    e2:最后一个ACTION_MOVE MotionEvent
    velocityX:X轴上的移动速度,像素/秒
    velocityY:Y轴上的移动速度,像素/秒   
onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法       在ACTION_MOVE动作发生时就会触发
    滑屏:手指触动屏幕后,稍微滑动后立即松开
    onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
    拖动
    onDown------》onScroll----》onScroll------》onFiling

    可见,无论是滑屏,还是拖动,影响的只是中间OnScroll触发的数量多少而已,最终都会触发onFling事件!

2、实例
要使用GestureDetector,有三步要走:
1、创建OnGestureListener监听函数:
可以使用构造实例:
  1. GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener(){  
  2.          
  3.     };
复制代码
也可以构造类:
  1.         private class gestureListener implements GestureDetector.OnGestureListener{
  2.        
  3.         }
复制代码
2、创建GestureDetector实例mGestureDetector:
构造函数有下面三个,根据需要选择:
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
复制代码
3、onTouch(View v, MotionEvent event)中拦截
  1. public boolean onTouch(View v, MotionEvent event) {
  2.         return mGestureDetector.onTouchEvent(event);   
  3. }
复制代码
4、控件绑定
  1.       TextView tv = (TextView)findViewById(R.id.tv);
  2.       tv.setOnTouchListener(this);
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:47 |显示全部楼层
进入实例阶段

首先,在主布局页面添加一个textView,并将其放大到整屏,方便在其上的手势识别,代码为:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context="com.example.gesturedetectorinterface.MainActivity" >

  6.     <TextView
  7.         android:id="@+id/tv"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="fill_parent"
  10.         android:layout_margin="50dip"
  11.         android:background="#ff00ff"
  12.         android:text="@string/hello_world" />

  13. </RelativeLayout>
复制代码
然后在JAVA代码中,依据上面的三步走原则,写出代码,并在所有的手势下添加上Toast提示并写上Log
  1. public class MainActivity extends Activity implements OnTouchListener{

  2.         private GestureDetector mGestureDetector;   
  3.        

  4.         @Override
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.activity_main);
  8.                
  9.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener
  10.         
  11.       TextView tv = (TextView)findViewById(R.id.tv);
  12.       tv.setOnTouchListener(this);
  13.       tv.setFocusable(true);   
  14.       tv.setClickable(true);   
  15.       tv.setLongClickable(true);
  16.         }
  17.        
  18.        
  19.         /*
  20.      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
  21.      * 来分析是否有合适的callback函数来处理用户的手势
  22.      */  
  23.         public boolean onTouch(View v, MotionEvent event) {
  24.                 return mGestureDetector.onTouchEvent(event);   
  25.         }
  26.        
  27.         private class gestureListener implements GestureDetector.OnGestureListener{

  28.                 // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发   
  29.                 public boolean onDown(MotionEvent e) {
  30.                         Log.i("MyGesture", "onDown");   
  31.                 Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();   
  32.                         return false;
  33.                 }

  34.                 /*  
  35.              * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  36.              * 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  37.              *
  38.              * 而onDown也是由一个MotionEventACTION_DOWN触发的,但是他没有任何限制,
  39.              * 也就是说当用户点击的时候,首先MotionEventACTION_DOWN,onDown就会执行,
  40.              * 如果在按下的瞬间没有松开或者是拖动的时候onShowPress就会执行,如果是按下的时间超过瞬间
  41.              * (这块我也不太清楚瞬间的时间差是多少,一般情况下都会执行onShowPress),拖动了,就不执行onShowPress。
  42.              */
  43.                 public void onShowPress(MotionEvent e) {
  44.                         Log.i("MyGesture", "onShowPress");   
  45.                 Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();   
  46.                 }

  47.                 // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发   
  48.                 ///轻击一下屏幕,立刻抬起来,才会有这个触发
  49.                 //从名子也可以看出,一次单独的轻击抬起操作,当然,如果除了Down以外还有其它操作,那就不再算是Single操作了,所以这个事件 就不再响应
  50.                 public boolean onSingleTapUp(MotionEvent e) {
  51.                         Log.i("MyGesture", "onSingleTapUp");   
  52.                 Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();   
  53.                 return true;   
  54.                 }

  55.                 // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发   
  56.                 public boolean onScroll(MotionEvent e1, MotionEvent e2,
  57.                                 float distanceX, float distanceY) {
  58.                         Log.i("MyGesture22", "onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);   
  59.                 Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();   
  60.                 
  61.                 return true;   
  62.                 }

  63.                 // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发   
  64.                 public void onLongPress(MotionEvent e) {
  65.                          Log.i("MyGesture", "onLongPress");   
  66.                      Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();   
  67.                 }

  68.                 // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发   
  69.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  70.                                 float velocityY) {
  71.                         Log.i("MyGesture", "onFling");   
  72.                 Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();   
  73.                         return true;
  74.                 }
  75.         };
  76.        

  77. }
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:52 |显示全部楼层
三、GestureDetector.OnDoubleTapListener---接口
1、构建
有两种方式设置双击监听:

方法一:新建一个类同时派生自OnGestureListener和OnDoubleTapListener:
  1. private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{
  2.         }
复制代码
方法二:使用GestureDetector::setOnDoubleTapListener();函数设置监听:
  1. //构建GestureDetector实例       
  2. mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener
  3. private class gestureListener implements GestureDetector.OnGestureListener{
  4.        
  5. }

  6. //设置双击监听器
  7. mGestureDetector.setOnDoubleTapListener(new doubleTapListener());
  8. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{
  9.        
  10. }
复制代码
注意:大家可以看到无论在方法一还是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我们说过GestureDetector 的构造函数,如下:
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
复制代码
可以看到,在构造函数中,除了后面要讲的SimpleOnGestureListener 以外的其它两个构造函数都必须是OnGestureListener的实例。所以要想使用OnDoubleTapListener的几个函数,就必须先实现OnGestureListener。
2、函数讲解:
首先看一下OnDoubleTapListener接口必须重写的三个函数:
  1. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{

  2.         public boolean onSingleTapConfirmed(MotionEvent e) {
  3.                 // TODO Auto-generated method stub
  4.                 return false;
  5.         }

  6.         public boolean onDoubleTap(MotionEvent e) {
  7.                 // TODO Auto-generated method stub
  8.                 return false;
  9.         }

  10.         public boolean onDoubleTapEvent(MotionEvent e) {
  11.                 // TODO Auto-generated method stub
  12.                 return false;
  13.         }
  14. }
复制代码
onSingleTapConfirmed(MotionEvent e):单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
关于onSingleTapConfirmed和onSingleTapUp的一点区别: OnGestureListener有这样的一个方法onSingleTapUp,和onSingleTapConfirmed容易混淆。二者的区别是:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。
onDoubleTap(MotionEvent e):双击事件

onDoubleTapEvent(MotionEvent e):双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件;下图是双击一下的Log输出:



两点总结:

1、从上图可以看出,在第二下点击时,先触发OnDoubleTap,然后再触发OnDown(第二次点击)

2、其次在触发OnDoubleTap以后,就开始触发onDoubleTapEvent了,onDoubleTapEvent后面的数字代表了当前的事件,0指ACTION_DOWN,1指ACTION_UP,2 指ACTION_MOVE
在上一个例子的基础上,我们再添加一个双击监听类,实现如下:
  1. public class MainActivity extends Activity implements OnTouchListener{

  2.         private GestureDetector mGestureDetector;   
  3.        

  4.         @Override
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.activity_main);
  8.                

  9.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener
  10.       mGestureDetector.setOnDoubleTapListener(new doubleTapListener());
  11.         
  12.       TextView tv = (TextView)findViewById(R.id.tv);
  13.       tv.setOnTouchListener(this);
  14.       tv.setFocusable(true);   
  15.       tv.setClickable(true);   
  16.       tv.setLongClickable(true);
  17.         }
  18.        
  19.        
  20.         /*
  21.      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
  22.      * 来分析是否有合适的callback函数来处理用户的手势
  23.      */  
  24.         public boolean onTouch(View v, MotionEvent event) {
  25.                 return mGestureDetector.onTouchEvent(event);   
  26.         }
  27.        
  28.         //OnGestureListener监听
  29.         private class gestureListener implements GestureDetector.OnGestureListener{

  30.                 public boolean onDown(MotionEvent e) {
  31.                         Log.i("MyGesture", "onDown");   
  32.                 Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();   
  33.                         return false;
  34.                 }

  35.                 public void onShowPress(MotionEvent e) {
  36.                         Log.i("MyGesture", "onShowPress");   
  37.                 Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();   
  38.                 }

  39.                 public boolean onSingleTapUp(MotionEvent e) {
  40.                         Log.i("MyGesture", "onSingleTapUp");   
  41.                 Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();   
  42.                 return true;   
  43.                 }

  44.                 public boolean onScroll(MotionEvent e1, MotionEvent e2,
  45.                                 float distanceX, float distanceY) {
  46.                         Log.i("MyGesture22", "onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);   
  47.                 Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();   
  48.                 
  49.                 return true;   
  50.                 }

  51.                 public void onLongPress(MotionEvent e) {
  52.                          Log.i("MyGesture", "onLongPress");   
  53.                      Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();   
  54.                 }

  55.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  56.                                 float velocityY) {
  57.                         Log.i("MyGesture", "onFling");   
  58.                 Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();   
  59.                         return true;
  60.                 }
  61.         };
  62.        
  63.         //OnDoubleTapListener监听
  64.         private class doubleTapListener implements GestureDetector.OnDoubleTapListener{

  65.                 public boolean onSingleTapConfirmed(MotionEvent e) {
  66.                         Log.i("MyGesture", "onSingleTapConfirmed");   
  67.                 Toast.makeText(MainActivity.this, "onSingleTapConfirmed", Toast.LENGTH_LONG).show();  
  68.                         return true;
  69.                 }

  70.                 public boolean onDoubleTap(MotionEvent e) {
  71.                         Log.i("MyGesture", "onDoubleTap");   
  72.                 Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();  
  73.                         return true;
  74.                 }

  75.                 public boolean onDoubleTapEvent(MotionEvent e) {
  76.                         Log.i("MyGesture", "onDoubleTapEvent");   
  77.                 Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_LONG).show();  
  78.                         return true;
  79.                 }
  80.         };
  81. }
复制代码
双击一下,部分截图如下:


双击所对应的触发事件顺序:


轻轻单击一下,对应的事件触发顺序为:

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:53 |显示全部楼层
四、GestureDetector.SimpleOnGestureListener---类
它与前两个不同的是:
1、这是一个类,在它基础上新建类的话,要用extends派生而不是用implements继承!
2、OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的,即使用不到也要重写出来一个空函数但在SimpleOnGestureListener类的实例或派生类中不必如此,可以根据情况,用到哪个函数就重写哪个函数,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。
下面利用SimpleOnGestureListener类来重新实现上面的几个效果,代码如下:
  1. public class MainActivity extends Activity implements OnTouchListener {

  2.         private GestureDetector mGestureDetector;   
  3.        
  4.         @Override
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.activity_main);
  8.                
  9.                 mGestureDetector = new GestureDetector(new simpleGestureListener());
  10.                
  11.                 TextView tv = (TextView)findViewById(R.id.tv);
  12.             tv.setOnTouchListener(this);
  13.             tv.setFocusable(true);   
  14.             tv.setClickable(true);   
  15.             tv.setLongClickable(true);
  16.         }
  17.        
  18.         public boolean onTouch(View v, MotionEvent event) {
  19.                 // TODO Auto-generated method stub
  20.                 return mGestureDetector.onTouchEvent(event);   
  21.         }

  22.         private class simpleGestureListener extends
  23.                         GestureDetector.SimpleOnGestureListener {
  24.                
  25.                 /*****OnGestureListener的函数*****/
  26.                 public boolean onDown(MotionEvent e) {
  27.                         Log.i("MyGesture", "onDown");
  28.                         Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT)
  29.                                         .show();
  30.                         return false;
  31.                 }

  32.                 public void onShowPress(MotionEvent e) {
  33.                         Log.i("MyGesture", "onShowPress");
  34.                         Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT)
  35.                                         .show();
  36.                 }

  37.                 public boolean onSingleTapUp(MotionEvent e) {
  38.                         Log.i("MyGesture", "onSingleTapUp");
  39.                         Toast.makeText(MainActivity.this, "onSingleTapUp",
  40.                                         Toast.LENGTH_SHORT).show();
  41.                         return true;
  42.                 }

  43.                 public boolean onScroll(MotionEvent e1, MotionEvent e2,
  44.                                 float distanceX, float distanceY) {
  45.                         Log.i("MyGesture", "onScroll:" + (e2.getX() - e1.getX()) + "   "
  46.                                         + distanceX);
  47.                         Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG)
  48.                                         .show();

  49.                         return true;
  50.                 }

  51.                 public void onLongPress(MotionEvent e) {
  52.                         Log.i("MyGesture", "onLongPress");
  53.                         Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG)
  54.                                         .show();
  55.                 }

  56.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  57.                                 float velocityY) {
  58.                         Log.i("MyGesture", "onFling");
  59.                         Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG)
  60.                                         .show();
  61.                         return true;
  62.                 }
  63.                
  64.                 /*****OnDoubleTapListener的函数*****/
  65.                 public boolean onSingleTapConfirmed(MotionEvent e) {
  66.                         Log.i("MyGesture", "onSingleTapConfirmed");
  67.                         Toast.makeText(MainActivity.this, "onSingleTapConfirmed",
  68.                                         Toast.LENGTH_LONG).show();
  69.                         return true;
  70.                 }

  71.                 public boolean onDoubleTap(MotionEvent e) {
  72.                         Log.i("MyGesture", "onDoubleTap");
  73.                         Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG)
  74.                                         .show();
  75.                         return true;
  76.                 }

  77.                 public boolean onDoubleTapEvent(MotionEvent e) {
  78.                         Log.i("MyGesture", "onDoubleTapEvent");
  79.                         Toast.makeText(MainActivity.this, "onDoubleTapEvent",
  80.                                         Toast.LENGTH_LONG).show();
  81.                         return true;
  82.                 }

  83.         }
  84. }
复制代码
到此,有关GestureDetector的所有基础知识都讲解完了,下面给出一个小应用——识别用户是向左滑还是向右滑!

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-09-21 09:54 |显示全部楼层
五、OnFling应用——识别向左滑还是向右滑
这部分就有点意思了,可以说是上面知识的一个小应用,我们利用OnFling函数来识别当前用户是在向左滑还是向右滑,从而打出日志。先看下OnFling的参数:
  1. boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
  2. 参数解释:   
  3. e1:第1个ACTION_DOWN MotionEvent   
  4. e2:最后一个ACTION_MOVE MotionEvent   
  5. velocityX:X轴上的移动速度,像素/秒   
  6. velocityY:Y轴上的移动速度,像素/秒   
复制代码
首先,先说一下实现的功能:当用户向左滑动距离超过100px,且滑动速度超过100 px/s时,即判断为向左滑动;向右同理.代码如下:
  1. public class MainActivity extends Activity implements OnTouchListener {

  2.         private GestureDetector mGestureDetector;   
  3.        
  4.         @Override
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.activity_main);
  8.                
  9.                 mGestureDetector = new GestureDetector(new simpleGestureListener());
  10.                
  11.                 TextView tv = (TextView)findViewById(R.id.tv);
  12.             tv.setOnTouchListener(this);
  13.             tv.setFocusable(true);   
  14.             tv.setClickable(true);   
  15.             tv.setLongClickable(true);
  16.         }
  17.        
  18.         public boolean onTouch(View v, MotionEvent event) {
  19.                 // TODO Auto-generated method stub
  20.                 return mGestureDetector.onTouchEvent(event);   
  21.         }

  22.         private class simpleGestureListener extends
  23.                         GestureDetector.SimpleOnGestureListener {
  24.                
  25.                 /*****OnGestureListener的函数*****/

  26.                 final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;  
  27.                
  28.                 // 触发条件 :   
  29.         // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒   
  30.       
  31.                 // 参数解释:   
  32.         // e1:第1个ACTION_DOWN MotionEvent   
  33.         // e2:最后一个ACTION_MOVE MotionEvent   
  34.         // velocityX:X轴上的移动速度,像素/秒   
  35.         // velocityY:Y轴上的移动速度,像素/秒   
  36.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  37.                                 float velocityY) {
  38.                        
  39.                 
  40.                 if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE  
  41.                         && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
  42.                     // Fling left   
  43.                     Log.i("MyGesture", "Fling left");  
  44.                     Toast.makeText(MainActivity.this, "Fling Left", Toast.LENGTH_SHORT).show();  
  45.                 } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE  
  46.                         && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
  47.                     // Fling right   
  48.                     Log.i("MyGesture", "Fling right");  
  49.                     Toast.makeText(MainActivity.this, "Fling Right", Toast.LENGTH_SHORT).show();  
  50.                 }  
  51.                         return true;
  52.                 }

  53.         }
  54. }
复制代码
这段代码难度不大,就不再细讲,看下效果:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP