文档章节

测试安卓触摸事件的分发机制

lanfogz
 lanfogz
发布于 2015/06/23 17:52
字数 3637
阅读 128
收藏 8
点赞 2
评论 3




概要:


Activity|dispatchTouchEvent  ========================》^onTouchEvent -- PhotoWindow.FragmentLayout

ViewGroup|dispatchTouchEvent  = onInterceptTouchEvent 》|onTouchEvent

View|dispatchTouchEvent  ========================》|onTouchEvent


其中,

角色:Activity、ViewGroup、View,其中ViewGroup继承View

方法:dispatch分发、onTouchEvent处理、onIntercept拦截/消费,最后一个为ViewGroup独有

事件:TouchEvent,包括一次DOWN、多次MOVE、一次UP,组成onClick,onLongClick,onScroll等事件

如图,

触摸事件总是先被派发,然后被处理,中间如果存在ViewGroup时,可以被拦截进而处理;

在向下传递不被处理的情况下,事件冒泡上传直到结束;

对于触摸监听器,直接影响View的事件派发

了解触摸事件的分发机制是自定义控件的基础。



测试:


1、均默认,且未指定监听器

06-23 15:58:39.551: I/MyActivity(11132): -->dispatchTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): -->dispatchTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): -->onInterceptTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:onInterceptTouchEvent(DOWN)

06-23 15:58:39.551: I/MyTextView(11132): -->dispatchTouchEvent(DOWN)

06-23 15:58:39.551: I/MyTextView(11132): -->onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyTextView(11132): <--false:onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyTextView(11132): <--false:dispatchTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): -->onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:dispatchTouchEvent(DOWN)

06-23 15:58:39.551: I/MyActivity(11132): -->onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyActivity(11132): <--false:onTouchEvent(DOWN)

06-23 15:58:39.551: I/MyActivity(11132): <--false:dispatchTouchEvent(DOWN)

06-23 15:58:39.641: I/MyActivity(11132): -->dispatchTouchEvent(MOVE)

06-23 15:58:39.641: I/MyActivity(11132): -->onTouchEvent(MOVE)

06-23 15:58:39.641: I/MyActivity(11132): <--false:onTouchEvent(MOVE)

06-23 15:58:39.641: I/MyActivity(11132): <--false:dispatchTouchEvent(MOVE)

06-23 15:58:39.931: I/MyActivity(11132): -->dispatchTouchEvent(UP)

06-23 15:58:39.931: I/MyActivity(11132): -->onTouchEvent(UP)

06-23 15:58:39.931: I/MyActivity(11132): <--false:onTouchEvent(UP)

06-23 15:58:39.931: I/MyActivity(11132): <--false:dispatchTouchEvent(UP)

-- 事件最终冒泡至MyActivity


2、MyActivity未派发DOWN事件

06-23 16:02:20.321: I/MyActivity(11673): -->dispatchTouchEvent(DOWN)

06-23 16:02:20.321: I/MyActivity(11673): <--true:dispatchTouchEvent(DOWN)

06-23 16:02:20.361: I/MyActivity(11673): -->dispatchTouchEvent(MOVE)

06-23 16:02:20.361: I/MyActivity(11673): -->onTouchEvent(MOVE)

06-23 16:02:20.361: I/MyActivity(11673): <--false:onTouchEvent(MOVE)

06-23 16:02:20.361: I/MyActivity(11673): <--false:dispatchTouchEvent(MOVE)

06-23 16:02:20.371: I/MyActivity(11673): -->dispatchTouchEvent(UP)

06-23 16:02:20.371: I/MyActivity(11673): -->onTouchEvent(UP)

06-23 16:02:20.371: I/MyActivity(11673): <--false:onTouchEvent(UP)

06-23 16:02:20.371: I/MyActivity(11673): <--false:dispatchTouchEvent(UP)


-- 无人得到DOWN事件,后续事件由MyActivity得到


3、MyLinearLayout未派发DOWN事件

06-23 16:05:35.411: I/MyActivity(12122): -->dispatchTouchEvent(DOWN)

06-23 16:05:35.411: I/MyLinearLayout(12122): -->dispatchTouchEvent(DOWN)

06-23 16:05:35.411: I/MyLinearLayout(12122): <--true:dispatchTouchEvent(DOWN)

06-23 16:05:35.411: I/MyActivity(12122): <--true:dispatchTouchEvent(DOWN)

06-23 16:05:35.481: I/MyActivity(12122): -->dispatchTouchEvent(MOVE)

06-23 16:05:35.481: I/MyLinearLayout(12122): -->dispatchTouchEvent(MOVE)

06-23 16:05:35.481: I/MyLinearLayout(12122): -->onTouchEvent(MOVE)

06-23 16:05:35.481: I/MyLinearLayout(12122): <--false:onTouchEvent(MOVE)

06-23 16:05:35.481: I/MyLinearLayout(12122): <--false:dispatchTouchEvent(MOVE)

06-23 16:05:35.481: I/MyActivity(12122): -->onTouchEvent(MOVE)

06-23 16:05:35.481: I/MyActivity(12122): <--false:onTouchEvent(MOVE)

06-23 16:05:35.481: I/MyActivity(12122): <--false:dispatchTouchEvent(MOVE)

06-23 16:05:35.501: I/MyActivity(12122): -->dispatchTouchEvent(UP)

06-23 16:05:35.501: I/MyLinearLayout(12122): -->dispatchTouchEvent(UP)

06-23 16:05:35.501: I/MyLinearLayout(12122): -->onTouchEvent(UP)

06-23 16:05:35.501: I/MyLinearLayout(12122): <--false:onTouchEvent(UP)

06-23 16:05:35.501: I/MyLinearLayout(12122): <--false:dispatchTouchEvent(UP)

06-23 16:05:35.501: I/MyActivity(12122): -->onTouchEvent(UP)

06-23 16:05:35.501: I/MyActivity(12122): <--false:onTouchEvent(UP)

06-23 16:05:35.501: I/MyActivity(12122): <--false:dispatchTouchEvent(UP)

-- 无人得到DOWN事件,后续事件由MyLinearLayout、MyActivity得到


4、MyLinearLayout未派发任何事件

06-23 16:13:04.011: I/MyActivity(12958): -->dispatchTouchEvent(DOWN)

06-23 16:13:04.011: I/MyLinearLayout(12958): -->dispatchTouchEvent(DOWN)

06-23 16:13:04.011: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(DOWN)

06-23 16:13:04.011: I/MyActivity(12958): <--true:dispatchTouchEvent(DOWN)

06-23 16:13:04.071: I/MyActivity(12958): -->dispatchTouchEvent(MOVE)

06-23 16:13:04.071: I/MyLinearLayout(12958): -->dispatchTouchEvent(MOVE)

06-23 16:13:04.071: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(MOVE)

06-23 16:13:04.071: I/MyActivity(12958): <--true:dispatchTouchEvent(MOVE)

06-23 16:13:04.151: I/MyActivity(12958): -->dispatchTouchEvent(UP)

06-23 16:13:04.151: I/MyLinearLayout(12958): -->dispatchTouchEvent(UP)

06-23 16:13:04.151: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(UP)

06-23 16:13:04.151: I/MyActivity(12958): <--true:dispatchTouchEvent(UP)


-- 无人得到任何事件

-- 综合1、2、3、4的测试结果,dispatchTouchEvent用于进行事件的派发,返回true时标识该类型事件已完成派发,返回false时意味着冒泡到上级继续处理


4、MyLinearLayout拦截DOWN事件

06-23 16:21:15.211: I/MyActivity(13579): -->dispatchTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): -->dispatchTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): -->onInterceptTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): <--true:onInterceptTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): -->onTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): <--false:onTouchEvent(DOWN)

06-23 16:21:15.211: I/MyLinearLayout(13579): <--false:dispatchTouchEvent(DOWN)

06-23 16:21:15.211: I/MyActivity(13579): -->onTouchEvent(DOWN)

06-23 16:21:15.211: I/MyActivity(13579): <--false:onTouchEvent(DOWN)

06-23 16:21:15.211: I/MyActivity(13579): <--false:dispatchTouchEvent(DOWN)

06-23 16:21:15.261: I/MyActivity(13579): -->dispatchTouchEvent(MOVE)

06-23 16:21:15.261: I/MyActivity(13579): -->onTouchEvent(MOVE)

06-23 16:21:15.261: I/MyActivity(13579): <--false:onTouchEvent(MOVE)

06-23 16:21:15.261: I/MyActivity(13579): <--false:dispatchTouchEvent(MOVE)

06-23 16:21:15.291: I/MyActivity(13579): -->dispatchTouchEvent(UP)

06-23 16:21:15.291: I/MyActivity(13579): -->onTouchEvent(UP)

06-23 16:21:15.291: I/MyActivity(13579): <--false:onTouchEvent(UP)

06-23 16:21:15.291: I/MyActivity(13579): <--false:dispatchTouchEvent(UP)


-- DOWN事件由MyLinearLayout、MyActivity得到,后续事件由MyActivity得到

-- 综合之前的测试结果,onInterceptTouchEvent仅在ViewGroup进行派发时才会执行调用


5、MyLinearLayout拦截并消费DOWN事件

06-23 16:25:07.241: I/MyActivity(14077): -->dispatchTouchEvent(DOWN)

06-23 16:25:07.241: I/MyLinearLayout(14077): -->dispatchTouchEvent(DOWN)

06-23 16:25:07.251: I/MyLinearLayout(14077): -->onInterceptTouchEvent(DOWN)

06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:onInterceptTouchEvent(DOWN)

06-23 16:25:07.251: I/MyLinearLayout(14077): -->onTouchEvent(DOWN)

06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:onTouchEvent(DOWN)

06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:dispatchTouchEvent(DOWN)

06-23 16:25:07.251: I/MyActivity(14077): <--true:dispatchTouchEvent(DOWN)

06-23 16:25:07.261: I/MyActivity(14077): -->dispatchTouchEvent(MOVE)

06-23 16:25:07.261: I/MyLinearLayout(14077): -->dispatchTouchEvent(MOVE)

06-23 16:25:07.261: I/MyLinearLayout(14077): -->onTouchEvent(MOVE)

06-23 16:25:07.261: I/MyLinearLayout(14077): <--false:onTouchEvent(MOVE)

06-23 16:25:07.261: I/MyLinearLayout(14077): <--false:dispatchTouchEvent(MOVE)

06-23 16:25:07.261: I/MyActivity(14077): -->onTouchEvent(MOVE)

06-23 16:25:07.261: I/MyActivity(14077): <--false:onTouchEvent(MOVE)

06-23 16:25:07.261: I/MyActivity(14077): <--false:dispatchTouchEvent(MOVE)

06-23 16:25:07.271: I/MyActivity(14077): -->dispatchTouchEvent(UP)

06-23 16:25:07.271: I/MyLinearLayout(14077): -->dispatchTouchEvent(UP)

06-23 16:25:07.271: I/MyLinearLayout(14077): -->onTouchEvent(UP)

06-23 16:25:07.271: I/MyLinearLayout(14077): <--false:onTouchEvent(UP)

06-23 16:25:07.271: I/MyLinearLayout(14077): <--false:dispatchTouchEvent(UP)

06-23 16:25:07.271: I/MyActivity(14077): -->onTouchEvent(UP)

06-23 16:25:07.271: I/MyActivity(14077): <--false:onTouchEvent(UP)

06-23 16:25:07.271: I/MyActivity(14077): <--false:dispatchTouchEvent(UP)


-- down事件由MyActivity得到并处理,后续事件由MyLinearLayout、MyActivity得到


5、MyLinearLayout拦截DOWN事件、处理所有事件

06-23 16:29:19.331: I/MyActivity(14591): -->dispatchTouchEvent(DOWN)

06-23 16:29:19.331: I/MyLinearLayout(14591): -->dispatchTouchEvent(DOWN)

06-23 16:29:19.331: I/MyLinearLayout(14591): -->onInterceptTouchEvent(DOWN)

06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:onInterceptTouchEvent(DOWN)

06-23 16:29:19.341: I/MyLinearLayout(14591): -->onTouchEvent(DOWN)

06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:onTouchEvent(DOWN)

06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(DOWN)

06-23 16:29:19.341: I/MyActivity(14591): <--true:dispatchTouchEvent(DOWN)

06-23 16:29:19.421: I/MyActivity(14591): -->dispatchTouchEvent(MOVE)

06-23 16:29:19.421: I/MyLinearLayout(14591): -->dispatchTouchEvent(MOVE)

06-23 16:29:19.421: I/MyLinearLayout(14591): -->onTouchEvent(MOVE)

06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:onTouchEvent(MOVE)

06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(MOVE)

06-23 16:29:19.421: I/MyActivity(14591): <--true:dispatchTouchEvent(MOVE)

06-23 16:29:19.421: I/MyActivity(14591): -->dispatchTouchEvent(UP)

06-23 16:29:19.421: I/MyLinearLayout(14591): -->dispatchTouchEvent(UP)

06-23 16:29:19.421: I/MyLinearLayout(14591): -->onTouchEvent(UP)

06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:onTouchEvent(UP)

06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(UP)

06-23 16:29:19.421: I/MyActivity(14591): <--true:dispatchTouchEvent(UP)


-- 所有事件由MyLinearLayout得到并处理

-- 综合之前的测试结果,事件被消费后任何人不再得到该事件

6、MyTextView消费DOWN事件


06-23 16:44:42.351: I/MyActivity(15674): -->dispatchTouchEvent(DOWN)

06-23 16:44:42.351: I/MyLinearLayout(15674): -->dispatchTouchEvent(DOWN)

06-23 16:44:42.351: I/MyLinearLayout(15674): -->onInterceptTouchEvent(DOWN)

06-23 16:44:42.361: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(DOWN)

06-23 16:44:42.361: I/MyTextView(15674): -->dispatchTouchEvent(DOWN)

06-23 16:44:42.361: I/MyTextView(15674): -->onTouchEvent(DOWN)

06-23 16:44:42.361: I/MyTextView(15674): <--true:onTouchEvent(DOWN)

06-23 16:44:42.361: I/MyTextView(15674): <--true:dispatchTouchEvent(DOWN)

06-23 16:44:42.361: I/MyLinearLayout(15674): <--true:dispatchTouchEvent(DOWN)

06-23 16:44:42.361: I/MyActivity(15674): <--true:dispatchTouchEvent(DOWN)

06-23 16:44:42.401: I/MyActivity(15674): -->dispatchTouchEvent(MOVE)

06-23 16:44:42.401: I/MyLinearLayout(15674): -->dispatchTouchEvent(MOVE)

06-23 16:44:42.401: I/MyLinearLayout(15674): -->onInterceptTouchEvent(MOVE)

06-23 16:44:42.401: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(MOVE)

06-23 16:44:42.401: I/MyTextView(15674): -->dispatchTouchEvent(MOVE)

06-23 16:44:42.401: I/MyTextView(15674): -->onTouchEvent(MOVE)

06-23 16:44:42.401: I/MyTextView(15674): <--false:onTouchEvent(MOVE)

06-23 16:44:42.401: I/MyTextView(15674): <--false:dispatchTouchEvent(MOVE)

06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:dispatchTouchEvent(MOVE)

06-23 16:44:42.411: I/MyActivity(15674): -->onTouchEvent(MOVE)

06-23 16:44:42.411: I/MyActivity(15674): <--false:onTouchEvent(MOVE)

06-23 16:44:42.411: I/MyActivity(15674): <--false:dispatchTouchEvent(MOVE)

06-23 16:44:42.411: I/MyActivity(15674): -->dispatchTouchEvent(UP)

06-23 16:44:42.411: I/MyLinearLayout(15674): -->dispatchTouchEvent(UP)

06-23 16:44:42.411: I/MyLinearLayout(15674): -->onInterceptTouchEvent(UP)

06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(UP)

06-23 16:44:42.411: I/MyTextView(15674): -->dispatchTouchEvent(UP)

06-23 16:44:42.411: I/MyTextView(15674): -->onTouchEvent(UP)

06-23 16:44:42.411: I/MyTextView(15674): <--false:onTouchEvent(UP)

06-23 16:44:42.411: I/MyTextView(15674): <--false:dispatchTouchEvent(UP)

06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:dispatchTouchEvent(UP)

06-23 16:44:42.411: I/MyActivity(15674): -->onTouchEvent(UP)

06-23 16:44:42.411: I/MyActivity(15674): <--false:onTouchEvent(UP)

06-23 16:44:42.411: I/MyActivity(15674): <--false:dispatchTouchEvent(UP)


-- down事件由MyTextView得到并消费,后续事件被MyTextView、MyActivity得到

7、MyTextView消费DOWN事件、MyLinearLayout拦截MOVE事件

06-23 16:50:38.431: I/MyActivity(16115): -->dispatchTouchEvent(DOWN)

06-23 16:50:38.431: I/MyLinearLayout(16115): -->dispatchTouchEvent(DOWN)

06-23 16:50:38.431: I/MyLinearLayout(16115): -->onInterceptTouchEvent(DOWN)

06-23 16:50:38.431: I/MyLinearLayout(16115): <--false:onInterceptTouchEvent(DOWN)

06-23 16:50:38.431: I/MyTextView(16115): -->dispatchTouchEvent(DOWN)

06-23 16:50:38.431: I/MyTextView(16115): -->onTouchEvent(DOWN)

06-23 16:50:38.431: I/MyTextView(16115): <--true:onTouchEvent(DOWN)

06-23 16:50:38.431: I/MyTextView(16115): <--true:dispatchTouchEvent(DOWN)

06-23 16:50:38.431: I/MyLinearLayout(16115): <--true:dispatchTouchEvent(DOWN)

06-23 16:50:38.431: I/MyActivity(16115): <--true:dispatchTouchEvent(DOWN)

06-23 16:50:38.461: I/MyActivity(16115): -->dispatchTouchEvent(MOVE)

06-23 16:50:38.461: I/MyLinearLayout(16115): -->dispatchTouchEvent(MOVE)

06-23 16:50:38.461: I/MyLinearLayout(16115): -->onInterceptTouchEvent(MOVE)

06-23 16:50:38.461: I/MyLinearLayout(16115): <--true:onInterceptTouchEvent(MOVE)

06-23 16:50:38.461: I/MyTextView(16115): -->dispatchTouchEvent(CANCEL)

06-23 16:50:38.461: I/MyTextView(16115): -->onTouchEvent(CANCEL)

06-23 16:50:38.461: I/MyTextView(16115): <--false:onTouchEvent(CANCEL)

06-23 16:50:38.461: I/MyTextView(16115): <--false:dispatchTouchEvent(CANCEL)

06-23 16:50:38.471: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(MOVE)

06-23 16:50:38.471: I/MyActivity(16115): -->onTouchEvent(MOVE)

06-23 16:50:38.471: I/MyActivity(16115): <--false:onTouchEvent(MOVE)

06-23 16:50:38.511: I/MyActivity(16115): -->dispatchTouchEvent(MOVE)

06-23 16:50:38.511: I/MyLinearLayout(16115): -->dispatchTouchEvent(MOVE)

06-23 16:50:38.511: I/MyLinearLayout(16115): -->onTouchEvent(MOVE)

06-23 16:50:38.511: I/MyLinearLayout(16115): <--false:onTouchEvent(MOVE)

06-23 16:50:38.511: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(MOVE)

06-23 16:50:38.511: I/MyActivity(16115): -->onTouchEvent(MOVE)

06-23 16:50:38.521: I/MyActivity(16115): <--false:onTouchEvent(MOVE)

06-23 16:50:38.521: I/MyActivity(16115): <--false:dispatchTouchEvent(MOVE)

06-23 16:50:38.521: I/MyActivity(16115): -->dispatchTouchEvent(UP)

06-23 16:50:38.521: I/MyLinearLayout(16115): -->dispatchTouchEvent(UP)

06-23 16:50:38.521: I/MyLinearLayout(16115): -->onTouchEvent(UP)

06-23 16:50:38.521: I/MyLinearLayout(16115): <--false:onTouchEvent(UP)

06-23 16:50:38.521: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(UP)

06-23 16:50:38.521: I/MyActivity(16115): -->onTouchEvent(UP)

06-23 16:50:38.521: I/MyActivity(16115): <--false:onTouchEvent(UP)

06-23 16:50:38.521: I/MyActivity(16115): <--false:dispatchTouchEvent(UP)


-- down事件由MyTextView得到并消费,之后拦截MOVE事件时MyTextView得到一次CANCEL事件,后续事件由MyLinearLayout、MyActivity得到

-- 注意:MyLinearLayout拦截第一次MOVE之后,onInterceptTouchEvent方法未再被调用

8、MyTextView消费DOWN事件、MyLinearLayout拦截UP事件


-- down事件由MyTextView得到并消费,move事件被MyTextView、MyActivity得到,之后拦截UP时MyTextView得到一次CANCEL事件,MyActivity得到UP事件


9、MyTextView消费DOWN事件、MyLinearLayout拦截且消费MOVE事件


--  down事件由MyTextView得到并消费,之后拦截MOVE事件时MyTextView得到一次CANCEL事件,后续MOVE事件由MyLinearLayout得到,UP事件由MyLinearLayout、MyActivity得到


10、MyTextView指定Touch监听器,且消费DOWN事件


-- 同6一致

11、MyLinearLayout指定Touch监听器,拦截并消费DOWN事件

 

 -- 同5一致

 

 12、MyTextView指定click监听器

 

06-23 17:18:14.621: I/MyActivity(19859): -->dispatchTouchEvent(DOWN)

06-23 17:18:14.621: I/MyLinearLayout(19859): -->dispatchTouchEvent(DOWN)

06-23 17:18:14.621: I/MyLinearLayout(19859): -->onInterceptTouchEvent(DOWN)

06-23 17:18:14.621: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(DOWN)

06-23 17:18:14.621: I/MyTextView(19859): -->dispatchTouchEvent(DOWN)

06-23 17:18:14.621: I/MyTextView(19859): -->onTouchEvent(DOWN)

06-23 17:18:14.621: I/MyTextView(19859): <--true:onTouchEvent(DOWN)

06-23 17:18:14.621: I/MyTextView(19859): <--true:dispatchTouchEvent(DOWN)

06-23 17:18:14.621: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(DOWN)

06-23 17:18:14.621: I/MyActivity(19859): <--true:dispatchTouchEvent(DOWN)

06-23 17:18:14.661: I/MyActivity(19859): -->dispatchTouchEvent(MOVE)

06-23 17:18:14.661: I/MyLinearLayout(19859): -->dispatchTouchEvent(MOVE)

06-23 17:18:14.671: I/MyLinearLayout(19859): -->onInterceptTouchEvent(MOVE)

06-23 17:18:14.671: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(MOVE)

06-23 17:18:14.671: I/MyTextView(19859): -->dispatchTouchEvent(MOVE)

06-23 17:18:14.671: I/MyTextView(19859): -->onTouchEvent(MOVE)

06-23 17:18:14.671: I/MyTextView(19859): <--true:onTouchEvent(MOVE)

06-23 17:18:14.671: I/MyTextView(19859): <--true:dispatchTouchEvent(MOVE)

06-23 17:18:14.671: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(MOVE)

06-23 17:18:14.671: I/MyActivity(19859): <--true:dispatchTouchEvent(MOVE)

06-23 17:18:14.701: I/MyActivity(19859): -->dispatchTouchEvent(UP)

06-23 17:18:14.701: I/MyLinearLayout(19859): -->dispatchTouchEvent(UP)

06-23 17:18:14.701: I/MyLinearLayout(19859): -->onInterceptTouchEvent(UP)

06-23 17:18:14.701: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(UP)

06-23 17:18:14.701: I/MyTextView(19859): -->dispatchTouchEvent(UP)

06-23 17:18:14.701: I/MyTextView(19859): -->onTouchEvent(UP)

06-23 17:18:14.701: I/MyTextView(19859): <--true:onTouchEvent(UP)

06-23 17:18:14.701: I/MyTextView(19859): <--true:dispatchTouchEvent(UP)

06-23 17:18:14.701: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(UP)

06-23 17:18:14.701: I/MyActivity(19859): <--true:dispatchTouchEvent(UP)

06-23 17:18:14.711: I/MyTextView(19859): --onClick()

 

 -- 所有事件也由MyTextView得到并消费

 

 

 13、MyLinearLayout、MyTextView同时指定click监听器

 

 -- 同上

 

 14、MyLinearLayout、MyTextView同时指定click监听器,MyLinearLayout指定属性android:descendantFocusability="blocksDescendants"

 

 -- 同上

 

 15、MyLinearLayout、MyTextView同时指定click监听器,MyLinearLayout拦截DOWN事件

 

06-23 17:29:54.591: I/MyActivity(22144): -->dispatchTouchEvent(DOWN)

06-23 17:29:54.591: I/MyLinearLayout(22144): -->dispatchTouchEvent(DOWN)

06-23 17:29:54.591: I/MyLinearLayout(22144): -->onInterceptTouchEvent(DOWN)

06-23 17:29:54.591: I/MyLinearLayout(22144): <--true:onInterceptTouchEvent(DOWN)

06-23 17:29:54.601: I/MyLinearLayout(22144): -->onTouchEvent(DOWN)

06-23 17:29:54.601: I/MyLinearLayout(22144): <--true:onTouchEvent(DOWN)

06-23 17:29:54.601: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(DOWN)

06-23 17:29:54.601: I/MyActivity(22144): <--true:dispatchTouchEvent(DOWN)

06-23 17:29:54.631: I/MyActivity(22144): -->dispatchTouchEvent(MOVE)

06-23 17:29:54.631: I/MyLinearLayout(22144): -->dispatchTouchEvent(MOVE)

06-23 17:29:54.631: I/MyLinearLayout(22144): -->onTouchEvent(MOVE)

06-23 17:29:54.631: I/MyLinearLayout(22144): <--true:onTouchEvent(MOVE)

06-23 17:29:54.631: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(MOVE)

06-23 17:29:54.631: I/MyActivity(22144): <--true:dispatchTouchEvent(MOVE)

06-23 17:29:54.681: I/MyActivity(22144): -->dispatchTouchEvent(UP)

06-23 17:29:54.681: I/MyLinearLayout(22144): -->dispatchTouchEvent(UP)

06-23 17:29:54.691: I/MyLinearLayout(22144): -->onTouchEvent(UP)

06-23 17:29:54.691: I/MyLinearLayout(22144): <--true:onTouchEvent(UP)

06-23 17:29:54.691: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(UP)

06-23 17:29:54.691: I/MyActivity(22144): <--true:dispatchTouchEvent(UP)

06-23 17:29:54.691: I/MyLinearLayout(22144): --onClick()


 -- 所有事件由MyLinearLayout得到并消费

 

 

 总结:

 

 1、dispatchTouchEvent总是首先执行,直接return之后返回到父类,其中当前控件和子控件不会得到任何事件

 2、View和ViewGroup(通过拦截)得到消费down事件后,后续事件也会被该控件得到

 3、onTouchEvent的返回值决定事件是否会被上级、上上级得到(均为false时,事件由底层View和Activity得到)

4、ViewGroup拦截事件之后,该类onInterceptTouch不会再被调用,而其中拦截MOVE或UP,子类会得到一个CANCEL事件

5、TouchListener同onTouchEvent的功能一致

6、ClickListener会消费所有的事件,前提是该控件得到或拦截到down事件

 7、即便在同一个控件内部,事件优先被TouchListener得到,其次是onTouchEvent,最后是ClickListener得到

 

 

 

 

 

 名词:

 

 分发dispatch分发是指dispatchTouchEvent返回true

 拦截intercept拦截是指onInterceptTouchEvent返回true

 消费  touch消费是指onTouchEvent、onTouch返回true

 

 参考:

 

http://hunankeda110.iteye.com/blog/1944311

Android触摸事件分发机制

 


© 著作权归作者所有

共有 人打赏支持
lanfogz
粉丝 2
博文 18
码字总数 29273
作品 0
南京
程序员
加载中

评论(3)

lanfogz
lanfogz

// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}
      ...
case MotionEvent.ACTION_DOWN:
mHasPerformedLongPress = false;
break;
}
return true;
}
return false;
}
lanfogz
lanfogz

3/
public boolean onTouchEvent(MotionEvent event) {
final int viewFlags = mViewFlags;
  ...
  if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
      ...
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
      ...
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();

// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick dir
lanfogz
lanfogz
《View控件中setOnClickListener对onTouchEvent的影响》
android.view.View

1/
public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
getListenerInfo().mOnClickListener = l;
}

2/
public void setClickable(boolean clickable) {
setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
}
安卓自定义View进阶-事件分发机制原理

安卓自定义View进阶-事件分发机制原理 之前讲解了很多与View绘图相关的知识,你可以在 安卓自定义View教程目录 中查看到这些文章,如果你理解了这些文章,那么至少2D绘图部分不是难题了,大部...

猴亮屏 ⋅ 05/22 ⋅ 0

Android6.0触摸事件分发机制解读

本篇博文是Android触摸事件分发机制系列博文的第一篇,带领大家从全局掌握Android触摸事件分发机制。特别声明的是,本源码解读是基于最新的Android6.0版本。 (一)Android6.0源码解读之Vie...

mynameishuangshuai ⋅ 2016/10/24 ⋅ 0

Design Patterns in Android:责任链模式

前言 非常抱歉,本系列博客长达半年没更新了,今日偶得灵感,更新一波《设计模式Android篇:责任链模式》。点击此处查看《Design Patterns in Android》系列其他文章。 本文原创作者MichaelX。...

MichaelX ⋅ 05/14 ⋅ 0

一步步探索学习Android Touch事件分发传递机制(一)

Android TouchEvent 简介 1.探究对象简介 Android的屏幕触摸事件在Android官方API中由类MotionEvent来描述,不同的触摸事件对应不同的事件类型。 如:ACTIONDOWN;ACTIONUP;ACTIONMOVE;ACTION...

单人旅途alan ⋅ 2017/11/16 ⋅ 0

一步步探索学习Android Touch事件分发传递机制(三)

前言 该系列文章分三篇: 一步步探索学习Android Touch事件分发传递机制(一) 通过写demo打Log,以ACTIONDOWN事件为例,完整了解整个Android Touch事件分发传递机制。 一步步探索学习Andro...

单人旅途alan ⋅ 2017/12/04 ⋅ 0

Android6.0源码解读之Activity点击事件分发机制

本篇博文是Android点击事件分发机制系列博文的第四篇,主要是从解读Activity类的源码入手,根据源码理清Activity点击事件分发原理,并掌握Activity点击事件分法机制。特别声明的是,本源码解...

mynameishuangshuai ⋅ 2016/10/23 ⋅ 0

Android触摸事件(下)——事件的分发

已经记不清有多久了,貌似自从接触Android开发开始,Android的事件分发机制一直伴随着我们。网上各种大神的各种分析,看完了可能还是会晕晕乎乎的。没办法,谁让咱是菜鸡呢(对!我菜鸡我还有...

nick_young ⋅ 2017/12/20 ⋅ 0

Android 高级UI4 事件传递机制

Android 高级UI 目录 1.View的事件分发 流程 dispatchTouchEvent(): onTouchListener--->onTouch方法 onTouchEvent onClickListener--->onClick方法 ListenerInfo dispatchTouchEvent onTouc......

香沙小熊 ⋅ 06/06 ⋅ 0

Android中view的dispatchTouchEvent方法源码分析

最近因为项目以及解bug的需要,阅读了下view的dispatchTouchEvent这个方法的源码。 安卓的事件传递机制就不用多说了,最重要的就是dispatchTouchEvent()、onInterceptTouchEvent()和onTouch...

Corrine_Shao ⋅ 05/04 ⋅ 0

【漫画技术】揭秘Android事件分发机制

类型 描述 用时 选题 silencezwm 0.1小时 写作时间 2017年11月16日 2.5小时 审稿 silencezwm 0.5小时 校对上线 silencezwm 0.2小时 Tips:4个环节,共计约3.3小时的精心打磨完成上线。 你好,...

silencezwm ⋅ 2017/11/17 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

骰子游戏代码开源地址

因为阿里云现在服务器已经停用了,所以上面的配置已经失效。 服务端开源地址:https://gitee.com/goalya/chat4.git 客户端开源地址:https://gitee.com/goalya/client4.git 具体运行界面请参考...

算法之名 ⋅ 36分钟前 ⋅ 0

设计模式--装饰者模式

装饰者模式 定义 动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。 通用类图 意图 动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比...

gaob2001 ⋅ 今天 ⋅ 0

JavaScript零基础入门——(八)JavaScript的数组

JavaScript零基础入门——(八)JavaScript的数组 欢迎大家回到我们的JavaScript零基础入门,上一节课我们讲了有关JavaScript正则表达式的相关知识点,便于大家更好的对字符串进行处理。这一...

JandenMa ⋅ 今天 ⋅ 0

sbt网络问题解决方案

转自:http://dblab.xmu.edu.cn/blog/maven-network-problem/ cd ~/.sbt/launchers/0.13.9unzip -q ./sbt-launch.jar 修改 vi sbt/sbt.boot.properties 增加一个oschina库地址: [reposit......

狐狸老侠 ⋅ 今天 ⋅ 0

大数据,必须掌握的10项顶级安全技术

我们看到越来越多的数据泄漏事故、勒索软件和其他类型的网络攻击,这使得安全成为一个热门话题。 去年,企业IT面临的威胁仍然处于非常高的水平,每天都会看到媒体报道大量数据泄漏事故和攻击...

p柯西 ⋅ 今天 ⋅ 0

Linux下安装配置Hadoop2.7.6

前提 安装jdk 下载 wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.7.6/hadoop-2.7.6.tar.gz 解压 配置 vim /etc/profile # 配置java环境变量 export JAVA_HOME=/opt/jdk1......

晨猫 ⋅ 今天 ⋅ 0

crontab工具介绍

crontab crontab 是一个用于设置周期性被执行的任务工具。 周期性执行的任务列表称为Cron Table crontab(选项)(参数) -e:编辑该用户的计时器设置; -l:列出该用户的计时器设置; -r:删除该...

Linux学习笔记 ⋅ 今天 ⋅ 0

深入Java多线程——Java内存模型深入(2)

5. final域的内存语义 5.1 final域的重排序规则 1.对于final域,编译器和处理器要遵守两个重排序规则: (1)在构造函数内对一个final域的写入,与随后把这个被构造对象的引用赋值给一个引用...

江左煤郎 ⋅ 今天 ⋅ 0

面试-正向代理和反向代理

面试-正向代理和反向代理 Nginx 是一个高性能的反向代理服务器,但同时也支持正向代理方式的配置。

秋日芒草 ⋅ 今天 ⋅ 0

Spring 依赖注入(DI)

1、Setter方法注入: 通过设置方法注入依赖。这种方法既简单又常用。 类中定义set()方法: public class HelloWorldOutput{ HelloWorld helloWorld; public void setHelloWorld...

霍淇滨 ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部