当前位置: 移动技术网 > 移动技术>移动开发>Android > Android GestureDetector用户手势检测实例讲解

Android GestureDetector用户手势检测实例讲解

2019年07月24日  | 移动技术网移动技术  | 我要评论
一、概述 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等。 一般情况下,我们知道view类有个view.ontouchli

一、概述

当用户触摸屏幕的时候,会产生许多手势,例如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,程序员可以在外部继承这个类,重写里面的手势处理方法。

下面我们先看ongesturelistener接口;

二、gesturedetector.ongesturelistener---接口

1、基本讲解
如果我们写一个类并implements ongesturelistener,会提示有几个必须重写的函数,加上之后是这个样子的:

private class gesturelistener implements gesturedetector.ongesturelistener{ 
 
 public boolean ondown(motionevent e) { 
  // todo auto-generated method stub 
  return false; 
 } 
 
 public void onshowpress(motionevent e) { 
  // todo auto-generated method stub 
   
 } 
 
 public boolean onsingletapup(motionevent e) { 
  // todo auto-generated method stub 
  return false; 
 } 
 
 public boolean onscroll(motionevent e1, motionevent e2, 
   float distancex, float distancey) { 
  // todo auto-generated method stub 
  return false; 
 } 
 
 public void onlongpress(motionevent e) { 
  // todo auto-generated method stub 
   
 } 
 
 public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
   float velocityy) { 
  // todo auto-generated method stub 
  return false; 
 } 
  
} 

可见,这里总共重写了六个函数,这些函数都在什么情况下才会触发呢,下面讲一下:

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监听函数:
可以使用构造实例:

gesturedetector.ongesturelistener listener = new gesturedetector.ongesturelistener(){ 
   
 };

也可以构造类:

private class gesturelistener implements gesturedetector.ongesturelistener{ 
 
} 

2.创建gesturedetector实例mgesturedetector:

构造函数有下面三个,根据需要选择:

gesturedetector gesturedetector=new gesturedetector(gesturedetector.ongesturelistener listener); 
gesturedetector gesturedetector=new gesturedetector(context context,gesturedetector.ongesturelistener listener); 
gesturedetector gesturedetector=new gesturedetector(context context,gesturedetector.simpleongesturelistener listener); 

3、ontouch(view v, motionevent event)中拦截:

public boolean ontouch(view v, motionevent event) { 
 return mgesturedetector.ontouchevent(event);  
} 

4.控件绑定

textview tv = (textview)findviewbyid(r.id.tv); 
tv.setontouchlistener(this); 

现在进入实例阶段:
首先,在主布局页面添加一个textview,并将其放大到整屏,方便在其上的手势识别,代码为:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.example.gesturedetectorinterface.mainactivity" > 
 
 <textview 
  android:id="@+id/tv" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_margin="50dip" 
  android:background="#ff00ff" 
  android:text="@string/hello_world" /> 
 
</relativelayout> 

然后在java代码中,依据上面的三步走原则,写出代码,并在所有的手势下添加上toast提示并写上log

public class mainactivity extends activity implements ontouchlistener{ 
 
 private gesturedetector mgesturedetector;  
  
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
   
  mgesturedetector = new gesturedetector(new gesturelistener()); //使用派生自ongesturelistener 
   
  textview tv = (textview)findviewbyid(r.id.tv); 
  tv.setontouchlistener(this); 
  tv.setfocusable(true);  
  tv.setclickable(true);  
  tv.setlongclickable(true); 
 } 
  
  
 /* 
  * 在ontouch()方法中,我们调用gesturedetector的ontouchevent()方法,将捕捉到的motionevent交给gesturedetector 
  * 来分析是否有合适的callback函数来处理用户的手势 
  */ 
 public boolean ontouch(view v, motionevent event) { 
  return mgesturedetector.ontouchevent(event);  
 } 
  
 private class gesturelistener implements gesturedetector.ongesturelistener{ 
 
  // 用户轻触触摸屏,由1个motionevent action_down触发  
  public boolean ondown(motionevent e) { 
   log.i("mygesture", "ondown");  
   toast.maketext(mainactivity.this, "ondown", toast.length_short).show();  
   return false; 
  } 
 
  /* 
   * 用户轻触触摸屏,尚未松开或拖动,由一个1个motionevent action_down触发 
   * 注意和ondown()的区别,强调的是没有松开或者拖动的状态 
   * 
   * 而ondown也是由一个motioneventaction_down触发的,但是他没有任何限制, 
   * 也就是说当用户点击的时候,首先motioneventaction_down,ondown就会执行, 
   * 如果在按下的瞬间没有松开或者是拖动的时候onshowpress就会执行,如果是按下的时间超过瞬间 
   * (这块我也不太清楚瞬间的时间差是多少,一般情况下都会执行onshowpress),拖动了,就不执行onshowpress。 
   */ 
  public void onshowpress(motionevent e) { 
   log.i("mygesture", "onshowpress");  
   toast.maketext(mainactivity.this, "onshowpress", toast.length_short).show();  
  } 
 
  // 用户(轻触触摸屏后)松开,由一个1个motionevent action_up触发  
  ///轻击一下屏幕,立刻抬起来,才会有这个触发 
  //从名子也可以看出,一次单独的轻击抬起操作,当然,如果除了down以外还有其它操作,那就不再算是single操作了,所以这个事件 就不再响应 
  public boolean onsingletapup(motionevent e) { 
   log.i("mygesture", "onsingletapup");  
   toast.maketext(mainactivity.this, "onsingletapup", toast.length_short).show();  
   return true;  
  } 
 
  // 用户按下触摸屏,并拖动,由1个motionevent action_down, 多个action_move触发  
  public boolean onscroll(motionevent e1, motionevent e2, 
    float distancex, float distancey) { 
   log.i("mygesture22", "onscroll:"+(e2.getx()-e1.getx()) +" "+distancex);  
   toast.maketext(mainactivity.this, "onscroll", toast.length_long).show();  
    
   return true;  
  } 
 
  // 用户长按触摸屏,由多个motionevent action_down触发  
  public void onlongpress(motionevent e) { 
    log.i("mygesture", "onlongpress");  
    toast.maketext(mainactivity.this, "onlongpress", toast.length_long).show();  
  } 
 
  // 用户按下触摸屏、快速移动后松开,由1个motionevent action_down, 多个action_move, 1个action_up触发  
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
    float velocityy) { 
   log.i("mygesture", "onfling");  
   toast.maketext(mainactivity.this, "onfling", toast.length_long).show();  
   return true; 
  } 
 }; 
  
 
} 

源码在博客底部给出。

三、gesturedetector.ondoubletaplistener---接口

1、构建

有两种方式设置双击监听:

方法一:新建一个类同时派生自ongesturelistener和ondoubletaplistener:

private class gesturelistener implements gesturedetector.ongesturelistener,gesturedetector.ondoubletaplistener{ 
 } 


方法二:使用gesturedetector::setondoubletaplistener();函数设置监听:

//构建gesturedetector实例  
mgesturedetector = new gesturedetector(new gesturelistener()); //使用派生自ongesturelistener 
private class gesturelistener implements gesturedetector.ongesturelistener{ 
  
} 
 
//设置双击监听器 
mgesturedetector.setondoubletaplistener(new doubletaplistener()); 
private class doubletaplistener implements gesturedetector.ondoubletaplistener{ 
  
} 

注意:大家可以看到无论在方法一还是在方法二中,都需要派生自gesturedetector.ongesturelistener,前面我们说过gesturedetector 的构造函数,如下:
gesturedetector gesturedetector=new gesturedetector(gesturedetector.ongesturelistener listener); 
gesturedetector gesturedetector=new gesturedetector(context context,gesturedetector.ongesturelistener listener); 
gesturedetector gesturedetector=new gesturedetector(context context,gesturedetector.simpleongesturelistener listener); 

可以看到,在构造函数中,除了后面要讲的simpleongesturelistener 以外的其它两个构造函数都必须是ongesturelistener的实例。所以要想使用ondoubletaplistener的几个函数,就必须先实现ongesturelistener。

2、函数讲解

首先看一下ondoubletaplistener接口必须重写的三个函数:

private class doubletaplistener implements gesturedetector.ondoubletaplistener{ 
 
 public boolean onsingletapconfirmed(motionevent e) { 
  // todo auto-generated method stub 
  return false; 
 } 
 
 public boolean ondoubletap(motionevent e) { 
  // todo auto-generated method stub 
  return false; 
 } 
 
 public boolean ondoubletapevent(motionevent e) { 
  // todo auto-generated method stub 
  return false; 
 } 
} 

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
在上一个例子的基础上,我们再添加一个双击监听类,实现如下:

public class mainactivity extends activity implements ontouchlistener{ 
 
 private gesturedetector mgesturedetector;  
  
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
   
 
  mgesturedetector = new gesturedetector(new gesturelistener()); //使用派生自ongesturelistener 
  mgesturedetector.setondoubletaplistener(new doubletaplistener()); 
   
  textview tv = (textview)findviewbyid(r.id.tv); 
  tv.setontouchlistener(this); 
  tv.setfocusable(true);  
  tv.setclickable(true);  
  tv.setlongclickable(true); 
 } 
  
  
 /* 
  * 在ontouch()方法中,我们调用gesturedetector的ontouchevent()方法,将捕捉到的motionevent交给gesturedetector 
  * 来分析是否有合适的callback函数来处理用户的手势 
  */ 
 public boolean ontouch(view v, motionevent event) { 
  return mgesturedetector.ontouchevent(event);  
 } 
  
 //ongesturelistener监听 
 private class gesturelistener implements gesturedetector.ongesturelistener{ 
 
  public boolean ondown(motionevent e) { 
   log.i("mygesture", "ondown");  
   toast.maketext(mainactivity.this, "ondown", toast.length_short).show();  
   return false; 
  } 
 
  public void onshowpress(motionevent e) { 
   log.i("mygesture", "onshowpress");  
   toast.maketext(mainactivity.this, "onshowpress", toast.length_short).show();  
  } 
 
  public boolean onsingletapup(motionevent e) { 
   log.i("mygesture", "onsingletapup");  
   toast.maketext(mainactivity.this, "onsingletapup", toast.length_short).show();  
   return true;  
  } 
 
  public boolean onscroll(motionevent e1, motionevent e2, 
    float distancex, float distancey) { 
   log.i("mygesture22", "onscroll:"+(e2.getx()-e1.getx()) +" "+distancex);  
   toast.maketext(mainactivity.this, "onscroll", toast.length_long).show();  
    
   return true;  
  } 
 
  public void onlongpress(motionevent e) { 
    log.i("mygesture", "onlongpress");  
    toast.maketext(mainactivity.this, "onlongpress", toast.length_long).show();  
  } 
 
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
    float velocityy) { 
   log.i("mygesture", "onfling");  
   toast.maketext(mainactivity.this, "onfling", toast.length_long).show();  
   return true; 
  } 
 }; 
  
 //ondoubletaplistener监听 
 private class doubletaplistener implements gesturedetector.ondoubletaplistener{ 
 
  public boolean onsingletapconfirmed(motionevent e) { 
   log.i("mygesture", "onsingletapconfirmed");  
   toast.maketext(mainactivity.this, "onsingletapconfirmed", toast.length_long).show(); 
   return true; 
  } 
 
  public boolean ondoubletap(motionevent e) { 
   log.i("mygesture", "ondoubletap");  
   toast.maketext(mainactivity.this, "ondoubletap", toast.length_long).show(); 
   return true; 
  } 
 
  public boolean ondoubletapevent(motionevent e) { 
   log.i("mygesture", "ondoubletapevent");  
   toast.maketext(mainactivity.this, "ondoubletapevent", toast.length_long).show(); 
   return true; 
  } 
 }; 
} 

双击一下,部分截图如下:


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


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


源码在博客底部给出。

四、gesturedetector.simpleongesturelistener---类

它与前两个不同的是:
1、这是一个类,在它基础上新建类的话,要用extends派生而不是用implements继承!
2、ongesturelistener和ondoubletaplistener接口里的函数都是强制必须重写的,即使用不到也要重写出来一个空函数但在simpleongesturelistener类的实例或派生类中不必如此,可以根据情况,用到哪个函数就重写哪个函数,因为simpleongesturelistener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。
下面利用simpleongesturelistener类来重新实现上面的几个效果,代码如下:

public class mainactivity extends activity implements ontouchlistener { 
 
 private gesturedetector mgesturedetector;  
  
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
   
  mgesturedetector = new gesturedetector(new simplegesturelistener()); 
   
  textview tv = (textview)findviewbyid(r.id.tv); 
  tv.setontouchlistener(this); 
  tv.setfocusable(true);  
  tv.setclickable(true);  
  tv.setlongclickable(true); 
 } 
  
 public boolean ontouch(view v, motionevent event) { 
  // todo auto-generated method stub 
  return mgesturedetector.ontouchevent(event);  
 } 
 
 private class simplegesturelistener extends 
   gesturedetector.simpleongesturelistener { 
   
  /*****ongesturelistener的函数*****/ 
  public boolean ondown(motionevent e) { 
   log.i("mygesture", "ondown"); 
   toast.maketext(mainactivity.this, "ondown", toast.length_short) 
     .show(); 
   return false; 
  } 
 
  public void onshowpress(motionevent e) { 
   log.i("mygesture", "onshowpress"); 
   toast.maketext(mainactivity.this, "onshowpress", toast.length_short) 
     .show(); 
  } 
 
  public boolean onsingletapup(motionevent e) { 
   log.i("mygesture", "onsingletapup"); 
   toast.maketext(mainactivity.this, "onsingletapup", 
     toast.length_short).show(); 
   return true; 
  } 
 
  public boolean onscroll(motionevent e1, motionevent e2, 
    float distancex, float distancey) { 
   log.i("mygesture", "onscroll:" + (e2.getx() - e1.getx()) + " " 
     + distancex); 
   toast.maketext(mainactivity.this, "onscroll", toast.length_long) 
     .show(); 
 
   return true; 
  } 
 
  public void onlongpress(motionevent e) { 
   log.i("mygesture", "onlongpress"); 
   toast.maketext(mainactivity.this, "onlongpress", toast.length_long) 
     .show(); 
  } 
 
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
    float velocityy) { 
   log.i("mygesture", "onfling"); 
   toast.maketext(mainactivity.this, "onfling", toast.length_long) 
     .show(); 
   return true; 
  } 
   
  /*****ondoubletaplistener的函数*****/ 
  public boolean onsingletapconfirmed(motionevent e) { 
   log.i("mygesture", "onsingletapconfirmed"); 
   toast.maketext(mainactivity.this, "onsingletapconfirmed", 
     toast.length_long).show(); 
   return true; 
  } 
 
  public boolean ondoubletap(motionevent e) { 
   log.i("mygesture", "ondoubletap"); 
   toast.maketext(mainactivity.this, "ondoubletap", toast.length_long) 
     .show(); 
   return true; 
  } 
 
  public boolean ondoubletapevent(motionevent e) { 
   log.i("mygesture", "ondoubletapevent"); 
   toast.maketext(mainactivity.this, "ondoubletapevent", 
     toast.length_long).show(); 
   return true; 
  } 
 
 } 
} 

到此,有关gesturedetector的所有基础知识都讲解完了,下面给出一个小应用——识别用户是向左滑还是向右滑!

源码在博客底部给出。

五、onfling应用——识别向左滑还是向右滑

这部分就有点意思了,可以说是上面知识的一个小应用,我们利用onfling函数来识别当前用户是在向左滑还是向右滑,从而打出日志。先看下onfling的参数:

boolean onfling(motionevent e1, motionevent e2, float velocityx,float velocityy) 

参数解释:    
e1:第1个action_down motionevent    
e2:最后一个action_move motionevent    
velocityx:x轴上的移动速度,像素/秒    
velocityy:y轴上的移动速度,像素/秒    
首先,先说一下实现的功能:当用户向左滑动距离超过100px,且滑动速度超过100 px/s时,即判断为向左滑动;向右同理.代码如下:

public class mainactivity extends activity implements ontouchlistener { 
 
 private gesturedetector mgesturedetector;  
  
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
   
  mgesturedetector = new gesturedetector(new simplegesturelistener()); 
   
  textview tv = (textview)findviewbyid(r.id.tv); 
  tv.setontouchlistener(this); 
  tv.setfocusable(true);  
  tv.setclickable(true);  
  tv.setlongclickable(true); 
 } 
  
 public boolean ontouch(view v, motionevent event) { 
  // todo auto-generated method stub 
  return mgesturedetector.ontouchevent(event);  
 } 
 
 private class simplegesturelistener extends 
   gesturedetector.simpleongesturelistener { 
   
  /*****ongesturelistener的函数*****/ 
 
  final int fling_min_distance = 100, fling_min_velocity = 200; 
   
  // 触发条件 :  
  // x轴的坐标位移大于fling_min_distance,且移动速度大于fling_min_velocity个像素/秒  
   
  // 参数解释:  
  // e1:第1个action_down motionevent  
  // e2:最后一个action_move motionevent  
  // velocityx:x轴上的移动速度,像素/秒  
  // velocityy:y轴上的移动速度,像素/秒  
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
    float velocityy) { 
    
    
   if (e1.getx() - e2.getx() > fling_min_distance 
     && math.abs(velocityx) > fling_min_velocity) { 
    // fling left  
    log.i("mygesture", "fling left"); 
    toast.maketext(mainactivity.this, "fling left", toast.length_short).show(); 
   } else if (e2.getx() - e1.getx() > fling_min_distance 
     && math.abs(velocityx) > fling_min_velocity) { 
    // fling right  
    log.i("mygesture", "fling right"); 
    toast.maketext(mainactivity.this, "fling right", toast.length_short).show(); 
   } 
   return true; 
  } 
 
 } 
} 

这段代码难度不大,就不再细讲,看下效果:

源码在博客底部给出。

源码地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网