Android各种事件的总结

来源:互联网 发布:淘宝贷款利息高不高 编辑:程序博客网 时间:2024/06/13 18:47

  我们经常使用的监听有:

tv.setOnClickListener(null);           tv..setOnLongClickListener(null);            tv..setOnScrollChangeListener(null);              tv..setOnTouchListener(null);

这些监听都是由触摸(Touch)而触发的事件:onClick, onScroll,onFling等等,都是由许多个Touch组成的;每一次手势交互都会依照以下顺序执行:

1. 接触接触屏一刹那,触发一个MotionEvent事件。

2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象。

3. 通过GestureDetector(手势识别器)转发次MotionEvent对象至OnGestureListener。

4. OnGestureListener获得该对象,听根据该对象封装的的信息,做出合适的反馈。


MotionEvent: 这个类用于封装手势、触摸笔、轨迹球等等的动作事件。其内部封装了两个重要的属性X和Y,这两个属性分别用于记录横轴和纵轴的坐标。


GestureDetector: 识别各种手势,(Gesture:手势 Detector:识别)

OnGestureListener: 这是一个手势交互的监听接口,其中提供了多个抽象方法,并根据GestureDetector的手势识别结果调用相对应的方法。


一、一般情况:

       一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)。onTouch(View v, MotionEvent event)方法可以处理简单的down、up、move三种简单事件;



二、通过通过GestureDetector(手势识别器)处理一些复杂的手势:

   首先我们先了解下GestureDetector类:

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

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

下面我们先看OnGestureListener接口;

2、GestureDetector.OnGestureListener---接口

2.1、基本讲解

如果我们写一个类并implements OnGestureListener,会提示有几个必须重写的函数,加上之后是这个样子的:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.2、实例

要使用GestureDetector,有三步要走:

1、创建OnGestureListener监听函数:
可以使用构造实例:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener(){  
  2.           
  3.     };  
也可以构造类:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private class gestureListener implements GestureDetector.OnGestureListener{  
  2.   
  3. }  
2、创建GestureDetector实例mGestureDetector:

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  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)中拦截:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public boolean onTouch(View v, MotionEvent event) {  
  2.     return mGestureDetector.onTouchEvent(event);     
  3. }  

4、控件绑定

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. TextView tv = (TextView)findViewById(R.id.tv);  
  2. tv.setOnTouchListener(this);
其中最重要的一步是将touch事件捕获的Event事件传递给GestureDetector,也就是上面的第三步;

3、GestureDetector.OnDoubleTapListener---接口

3.1、构建

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

方法一:新建一个类同时派生自OnGestureListener和OnDoubleTapListener:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{  
  2.     }  
方法二:使用GestureDetector::setOnDoubleTapListener();函数设置监听:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //构建GestureDetector实例     
  2. mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  3. private class gestureListener implements GestureDetector.OnGestureListener{  
  4.       
  5. }  
  6.   
  7. //设置双击监听器  
  8. mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
  9. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  10.       
  11. }  
注意:大家可以看到无论在方法一还是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我们说过GestureDetector 的构造函数,如下:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  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。

3.2、函数讲解:

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  2.   
  3.     public boolean onSingleTapConfirmed(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public boolean onDoubleTap(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.         return false;  
  11.     }  
  12.   
  13.     public boolean onDoubleTapEvent(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17. }  
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


4、GestureDetector.SimpleOnGestureListener---类

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



关于一些手势,经过摸索得出一些经验如下:

  • 按下(onDown): 刚刚手指接触到触摸屏的那一刹那,就是触的那一下。

  • 抛掷(onFling): 手指在触摸屏上迅速移动,并松开的动作。

  • 长按(onLongPress): 手指按在持续一段时间,并且没有松开。

  • 滚动(onScroll): 手指在触摸屏上滑动。

  • 按住(onShowPress): 手指按在触摸屏上,它的时间范围在按下起效,在长按之前。

  • 抬起(onSingleTapUp):手指离开触摸屏的那一刹那。

除了这些定义之外,鄙人也总结了一点算是经验的经验吧,在这里和大家分享一下。

  • 任何手势动作都会先执行一次按下(onDown)动作。

  • 长按(onLongPress)动作前一定会执行一次按住(onShowPress)动作。

  • 按住(onShowPress)动作和按下(onDown)动作之后都会执行一次抬起(onSingleTapUp)动作。

  • 长按(onLongPress)、滚动(onScroll)和抛掷(onFling)动作之后都不会执行抬起(onSingleTapUp)动作。


 5.最后关于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轴上的移动速度,像素/秒   

大神的博客:http://blog.csdn.net/harvic880925/article/details/39520901   


后面还有关于事件分发的机制;


0 0
原创粉丝点击