标签:承担 3.2 分析 功能 point 就是 png 用户 ges
1、概述
本次分享有一个非常重要的概念:
View
,虽然说View
不属于四大组件,但是它的作用堪比四大组件,甚至比Receiver和Provider的重要性都要大。在Android开发中,Activity承担这可视化的功能,同时Android系统提供了很多基础控件,常见的有Button、TextView、CheckBox等。很多时候仅仅使用系统提供的控件是不能满足需求的,因此我们就需要能够根据需求进行新控件的定义,而控件的自定义就需要对Android的View体系有深入的理解,只有这样才能写出完美的自定义控件。同时Android手机属于移动设备,移动设备的一个特点就是用户可以直接通过屏幕来进行一系列操作,一个典型的场景就是屏幕的滑动,用户可以通过滑动来切换到不同的界面。很多情况下我们的应用都需要支持滑动操作,当处于不同层级的View都可以响应用户的滑动操作时,就会带来一个问题,那就是滑动冲突。如何解决滑动冲突呢?这对于初学者来说的确是个头疼的问题,其实解决滑动冲突本不难,它需要读者对View的事件分发机制有一定的了解,在这个基础上,我们就可以利于这个特性从而得出滑动冲突的解决方法。
——摘自Android开发艺术探索
事件指的是什么呢?就是指用户触摸屏幕产生的
Touch
事件;在Android中它被封装成MotionEvent
。
MotionEvent
分类其余事件:
ACTION_MASK
、ACTION_OUTSIDE
、ACTION_POINTER_DOWN
、
ACTION_POINTER_UP
、ACTION_HOVER_MOVE
、ACTION_SCROLL
、
ACTION_HOVER_ENTER
、ACTION_HOVER_EXIT
、ACTION_BUTTON_PRESS
、
ACTION_BUTTON_RELEASE
、ACTION_POINTER_INDEX_MASK
、
ACTION_POINTER_INDEX_SHIFT
;有兴趣的可以下来自己了解。3、事件产生顺序
经理分派任务,下属处理这个任务的过程。
View层次示例
View内部事件处理流程图:
流程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
标签:承担 3.2 分析 功能 point 就是 png 用户 ges
原文地址:https://www.cnblogs.com/hejing-michael/p/android-shi-jian-fen-fa-ji-zhi.html