Android_Touch事件的分发和消费机制

原创 2015年07月10日 21:26:21

我也是初学android的菜鸟,今天看书自己研究学习了android得Touch事件分发和消费机制,稍微做了一下总结,与大家一起分享,假如有理解错误的地方,欢迎大家提醒。

那么接下来让我们就开始进入正题。 在Android 中与 Touch 事件相关的方法包括以下三个方法:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)、onTouchEvent(MotionEvent ev);能够响应这些方法的控件包括:Activity、ViewGroup、View。下面是它们与Touch事件的对应表:

Touch 事件相关方法   方法功能  
  ViewGroup   
       View   
     Activity
  public boolean dispatchTouchEvent(MotionEvent ev) 事件分发 
 Yes  Yes  Yes
  public boolean onInterceptTouchEvent(MotionEvent ev)  
事件拦截 
 Yes  Yes  No
  public boolean onTouchEvent(MotionEvent ev) 事件响应 
 Yes  Yes  Yes

从上面这张表中我们可以看出 ViewGroup 和 View 对与 Touch 事件相关的三个方法的响应关系。Activity 根本就没有onInterceptTouchEvent(MotionEvent ev)方法,所以它不会对 onInterceptTouchEvent(MotionEvent ev) 也就是事件拦截进行响应。至于,Activity为什么没有拦截事件,这个也比较容易理解,因为Activity是作为最外层的一个容器,它进行拦截根本就没有意义。另外需要注意的是 View 对 dispatchTouchEvent(MotionEvent ev) (分发)和 onInterceptTouchEvent(MotionEvent ev) (拦截)的响应的前提是可以向该 View 中添加子 View,如果当前的 View 已经是一个最小的单元 View(比如 TextView,Button等),那么就无法向这个最小 View 中添加子 View,也就无法向子 View 进行事件的分发和拦截,所以它没有 dispatchTouchEvent(MotionEvent ev) 和 onInterceptTouchEvent(MotionEvent ev),只有 onTouchEvent(MotionEvent ev)。


下面是对这三个事件的分析:
(一)、事件分发:public boolean dispatchTouchEvent(MotionEvent ev)
Touch 事件发生时 Activity 的 dispatchTouchEvent(MotionEvent ev) 方法会以隧道方式(从根元素依次往下传递直到最内层子元素或在中间某一元素中由于某一条件停止传递)将事件传递给最外层 View 的 dispatchTouchEvent(MotionEvent ev) 方法,并由该 View 的 dispatchTouchEvent(MotionEvent ev) 方法对事件进行分发。


(二)、事件拦截:public boolean onInterceptTouchEvent(MotionEvent ev)
在外层 View 的 dispatchTouchEvent(MotionEvent ev) 方法返回系统默认的 super.dispatchTouchEvent(ev) 情况下,事件会自动的分发给当前 View 的 onInterceptTouchEvent 方法。onInterceptTouchEvent 的事件拦截逻辑如下:
  1. •如果 onInterceptTouchEvent 返回 true,则表示将事件进行拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理; 
  2. •如果 onInterceptTouchEvent 返回 false,则表示将事件放行,当前 View 上的事件会被传递到子 View 上,再由子 View 的 dispatchTouchEvent 来开始这个事件的分发; 
  3. •如果 onInterceptTouchEvent 返回 super.onInterceptTouchEvent(ev),事件默认不会被拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理。 

(三)、事件响应:public boolean onTouchEvent(MotionEvent ev)
在 dispatchTouchEvent 返回 super.dispatchTouchEvent(ev) 并且 onInterceptTouchEvent 返回 true 或返回 super.onInterceptTouchEvent(ev) 的情况下 onTouchEvent 会被调用。onTouchEvent 的事件响应逻辑如下:
  1. •如果事件传递到当前 View 的 onTouchEvent 方法,而该方法返回了 false,那么这个事件会从当前 View 向上传递,并且都是由上层 View 的 onTouchEvent 来接收,如果传递到上面的 onTouchEvent 也返回 false,这个事件就会“消失”,而且接收不到下一次事件。 
  2. •如果返回了 true 则会接收并消费该事件。 
  3. •如果返回 super.onTouchEvent(ev) 默认处理事件的逻辑和返回 false 时相同。


因为事件包括的动作很多,假如都考虑在内情况比较复杂,下面我们只从按下和抬起这两种状态来进行分析。下面是我的布局,因为xml和Activity中的代码都比较简单,在这就不贴出来了。让大家看一下我的log输出,


整个的是一个Activity,红色和黄色的是ViewGroup (都是继承LinearLayout)蓝色是一个view(继承了Button) 从图中可以看出它们是嵌套关系。

下面我们就开始做一些分析:

一、当所有的touchEvent都默认返回:

1、只点击父布局:

07-30 17:00:46.633: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN

07-30 17:00:46.633: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN

07-30 17:00:46.633: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN

07-30 17:00:46.644: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN

07-30 17:00:46.644: MainActivity: ==onTouchEvent --> ACTION_DOWN

07-30 17:00:46.827: MainActivity: ==dispatchTouchEvent --> ACTION_UP

07-30 17:00:46.827: MainActivity: ==onTouchEvent --> ACTION_UP   

2、点击父布局中的子布局:
07-30 17:04:00.599: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.614: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.865: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 17:04:00.865: MainActivity: ==onTouchEvent --> ACTION_UP
3、点击子布局中的Button:
07-30 19:12:25.479: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 19:12:25.614: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:12:25.694: MyButton: ==onTouchEvent --> ACTION_UP
07-30 19:12:25.724: MainActivity: ==onClick -->
二、当LinearLayoutFather中的onInterceptTouchEvent返回false:???为什么跟默认返回一致?
1、只点击父布局:
07-30 19:46:42.435: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:46:42.435: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:46:42.626: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:46:42.626: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 19:47:55.379: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.379: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.394: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.394: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.527: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:47:55.527: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 19:52:53.554: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.554: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.554: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 19:52:53.774: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.841: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:52:53.841: MyButton: ==onTouchEvent --> ACTION_UP
07-30 19:52:53.903: MainActivity: ==onClick --> 




三、当LinearLayoutFather中的onInterceptTouchEvent返回true:
1、只点击父布局:
07-30 19:57:52.385: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:57:52.394: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:57:52.485: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:57:52.485: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 19:59:26.684: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.837: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:59:26.837: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 19:59:26.684: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.837: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:59:26.837: MainActivity: ==onTouchEvent --> ACTION_UP






四、当LinearLayoutChilds中的onInterceptTouchEvent返回false:
1、只点击父布局:
07-30 20:08:25.894: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:08:26.094: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:08:26.094: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:09:22.347: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.347: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.354: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:09:22.354: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.441: MainActivity: ==dispatchTouchEvent --> ACTION_MOVE
07-30 20:09:22.447: MainActivity: ==onTouchEvent --> ACTION_MOVE
07-30 20:09:22.457: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:09:22.457: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:10:08.385: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.385: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:10:08.404: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:10:08.594: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.654: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:10:08.654: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:10:08.717: MainActivity: ==onClick --> 




五、当LinearLayoutChilds中的onInterceptTouchEvent返回true:
1、只点击父布局:
07-30 20:12:47.143: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:12:47.143: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:12:47.143: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:12:47.153: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:12:47.153: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:12:47.314: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:12:47.314: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:13:25.582: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.584: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.584: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.617: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.763: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:13:25.763: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:13:58.674: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.685: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.918: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:13:58.918: MainActivity: ==onTouchEvent --> ACTION_UP






六、当LinearLayoutFather中的onTouchEvent返回false:
1、只点击父布局:
07-30 20:23:45.933: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:23:45.944: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:23:46.068: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:23:46.068: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:26:08.985: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:08.985: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.114: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:09.114: MainActivity: ==onTouchEvent --> ACTION_UP




3、点击子布局中的Button:
07-30 20:26:58.767: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.774: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.774: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:58.933: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.944: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:26:58.944: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:26:58.955: MainActivity: ==onClick --> 




七、当LinearLayoutFather中的onTouchEvent返回true:
1、只点击父布局:
07-30 20:17:54.564: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:17:54.564: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:17:54.574: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:17:54.574: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:17:54.756: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:17:54.756: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:17:54.756: LinearLayoutFather: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:18:52.710: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.710: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:18:52.724: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:18:52.828: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:18:52.834: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:18:52.834: LinearLayoutFather: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:20:26.579: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.579: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:20:26.824: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:20:26.894: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:20:26.975: MainActivity: ==onClick --> 




八、当MyButton中的onTouchEvent返回false:
1、点击子布局中的Button:
07-30 20:35:24.218: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.514: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:35:24.514: MainActivity: ==onTouchEvent --> ACTION_UP




九、当MyButton中的onTouchEvent返回true:
1、点击子布局中的Button:
07-30 20:34:12.458: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.458: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.458: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:34:12.627: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:34:12.634: MyButton: ==onTouchEvent --> ACTION_UP


好了我们暂时就分析到这里,希望我的文章可以帮助你进一步理解Android中的Touch事件的分发和消费机制
版权声明:本文为博主原创文章,未经博主允许不得转载。

Android Touch 事件的分发和消费机制

这里先对几个函数讲解下。 Ø  public boolean dispatchTouchEvent (MotionEventev)     这个方法分发TouchEvent Ø  publ...

Android 编程下 Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)...

Android 编程下 Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)...

Android编程——Touch 事件的分发和消费机制

介绍Android 中与 Touch 事件相关的方法包括: dispatchTouchEvent(MotionEvent ev) onInterceptTouchEvent(MotionEvent...

Android--Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)、...

Android 编程下 Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev...

Android 编程下 Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev...

Android中Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)...
  • fngy123
  • fngy123
  • 2015年10月07日 16:28
  • 328

Android 编程下 Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android_Touch事件的分发和消费机制
举报原因:
原因补充:

(最多只允许输入30个字)