当前位置: 移动技术网 > IT编程>移动开发>Android > Android事件分发机制

Android事件分发机制

2019年09月11日  | 移动技术网IT编程  | 我要评论

冯晓强,2013小品搞笑大全,美术教案

1、概述

本次分享有一个非常重要的概念:view,虽然说view不属于四大组件,但是它的作用堪比四大组件,甚至比receiver和provider的重要性都要大。在android开发中,activity承担这可视化的功能,同时android系统提供了很多基础控件,常见的有button、textview、checkbox等。很多时候仅仅使用系统提供的控件是不能满足需求的,因此我们就需要能够根据需求进行新控件的定义,而控件的自定义就需要对android的view体系有深入的理解,只有这样才能写出完美的自定义控件。同时android手机属于移动设备,移动设备的一个特点就是用户可以直接通过屏幕来进行一系列操作,一个典型的场景就是屏幕的滑动,用户可以通过滑动来切换到不同的界面。很多情况下我们的应用都需要支持滑动操作,当处于不同层级的view都可以响应用户的滑动操作时,就会带来一个问题,那就是滑动冲突。如何解决滑动冲突呢?这对于初学者来说的确是个头疼的问题,其实解决滑动冲突本不难,它需要读者对view的事件分发机制有一定的了解,在这个基础上,我们就可以利于这个特性从而得出滑动冲突的解决方法。

——摘自android开发艺术探索

2、事件分发概述

事件指的是什么呢?就是指用户触摸屏幕产生的touch事件;在android中它被封装成motionevent

3、常用motionevent分类

  • action_down:按下view(其他所有事件的开始)。
  • action_up:抬起view(与down对应)。
  • action_move:滑动view。
  • action_cancel:结束事件(非人为原因)。

其余事件:action_maskaction_outsideaction_pointer_down
action_pointer_upaction_hover_moveaction_scroll
action_hover_enteraction_hover_exitaction_button_press
action_button_releaseaction_pointer_index_mask
action_pointer_index_shift;有兴趣的可以下来自己了解。

3、事件产生顺序

4、android事件扭转流程

模拟:

经理分派任务,下属处理这个任务的过程。

  • view层次示例

  • view内部事件处理流程图:

5、事件分发流程及其分析

流程图及注释

流程分析

  • 流程1:在activity#dispatchtouchevent返回false/true
    当前流程是在activity#dispatchtouchevent拦截并消费事件,不再往下传递

    03-26 18:54:58.178 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0

  • 流程2:在viewgroup#dispatchtouchevent返回true:
    当前流程是viewgroup#dispatchtouchevent拦截并消费事件,事件不再往下传递

    03-26 19:08:23.268 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:08:23.268 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0

  • 流程3:在viewgroup#dispatchtouchevent返回false:
    当前流程是停止往当前viewgroup及其子view事件,并将当前事件交由父类ontouchevent处理。

    03-26 19:09:15.238 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:09:15.238 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:09:15.238 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 流程4:在viewgroup#onintercepttouchevent返回true:

    03-26 19:07:02.918 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:07:02.918 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:07:02.918 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:07:02.918 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:07:02.918 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 在viewgroup#onintercepttouchevent返回false
    当前流程为系统默认流程即:viewgroup不拦截事件,事件将往下一级传递

    03-26 19:06:11.008 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:06:11.018 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 流程5:在view#dispatchtouchevent返回true:
    当前流程是viewgroup#dispatchtouchevent拦截并消费事件,事件不再往下传递

    03-26 19:10:41.048 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:10:41.048 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:10:41.048 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:10:41.048 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0

  • 流程6:在view#dispatchtouchevent返回false:

    03-26 19:11:36.458 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:11:36.458 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:11:36.458 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:11:36.458 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:11:36.458 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:11:36.458 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 流程7:在view#ontouchevent返回true:

    03-26 19:12:51.688 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:12:51.688 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:12:51.688 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:12:51.688 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:12:51.688 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0

  • 在view#ontouchevent返回false:
    当前流程为系统默认流程

    03-26 19:13:58.188 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:13:58.188 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 流程8:在viewgroup#ontouchevent返回true:

    03-26 19:15:14.608 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:15:14.608 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:15:14.608 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:15:14.608 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:15:14.608 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:15:14.608 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0

  • 在viewgroup#ontouchevent返回false:
    当前流程为系统默认流程

    03-26 19:16:48.928 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:16:48.928 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 流程9:在activity#ontouchevent返回true:

    03-26 19:18:49.648 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:18:49.648 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

  • 在activity#ontouchevent返回false:
    当前流程为系统默认流程

    03-26 19:20:07.738 com.android.api23 i/com.android.api23.mainactivity: dispatchtouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.myviewgroup: dispatchtouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.myviewgroup: onintercepttouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.myview: dispatchtouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.myview: ontouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.myviewgroup: ontouchevent#event:0
    03-26 19:20:07.738 com.android.api23 i/com.android.api23.mainactivity: ontouchevent#event:0

androidapi23:android事件分发机制测试demo

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网