TouchEvent的处理流程就是TouchEvent在View树中的传递的过程:
这个过程分为2步:
第一步,ACTION_DOWN在View树中寻找处理TouchEvent的View;
第二步,剩余的ACTION_XXX在View树传递给目标View;

第一步,ACTION_DOWN在View树中寻找处理TouchEvent的View

递归方式完成

  • 1.ACTION_DOWN从顶向下传递,找到Touch到的最底层的View;
  • 2.从底向上,查找可以处理TouchEvent的View,并记录从View到root的路径;

其中涉及到ViewGroup.dispatchTouchEvent,onInterceptTouchEvent
以及View.dispatchTouchEvent, View.onTouchEvent
递归的方式调用View/ViewGroup的dispatchTouchEvent,寻找最底层的View

ViewGroup.java

    @Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onTouchEvent(ev, 1);}// If the event targets the accessibility focused view and this is it, start// normal event dispatch. Maybe a descendant is what will handle the click.if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {ev.setTargetAccessibilityFocus(false);}boolean handled = false;if (onFilterTouchEventForSecurity(ev)) {final int action = ev.getAction();final int actionMasked = action & MotionEvent.ACTION_MASK;// Handle an initial down.if (actionMasked == MotionEvent.ACTION_DOWN) {// Throw away all previous state when starting a new touch gesture.// The framework may have dropped the up or cancel event for the previous gesture// due to an app switch, ANR, or some other state change.cancelAndClearTouchTargets(ev);resetTouchState();}// Check for interception.final boolean intercepted;if (actionMasked == MotionEvent.ACTION_DOWN|| mFirstTouchTarget != null) {final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;if (!disallowIntercept) {intercepted = onInterceptTouchEvent(ev);ev.setAction(action); // restore action in case it was changed} else {intercepted = false;}} else {// There are no touch targets and this action is not an initial down// so this view group continues to intercept touches.intercepted = true;}// If intercepted, start normal event dispatch. Also if there is already// a view that is handling the gesture, do normal event dispatch.if (intercepted || mFirstTouchTarget != null) {ev.setTargetAccessibilityFocus(false);}// Check for cancelation.final boolean canceled = resetCancelNextUpFlag(this)|| actionMasked == MotionEvent.ACTION_CANCEL;// Update list of touch targets for pointer down, if needed.final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;TouchTarget newTouchTarget = null;boolean alreadyDispatchedToNewTouchTarget = false;if (!canceled && !intercepted) {// If the event is targeting accessiiblity focus we give it to the// view that has accessibility focus and if it does not handle it// we clear the flag and dispatch the event to all children as usual.// We are looking up the accessibility focused host to avoid keeping// state since these events are very rare.View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()? findChildWithAccessibilityFocus() : null;if (actionMasked == MotionEvent.ACTION_DOWN|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {final int actionIndex = ev.getActionIndex(); // always 0 for downfinal int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex): TouchTarget.ALL_POINTER_IDS;// Clean up earlier touch targets for this pointer id in case they// have become out of sync.removePointersFromTouchTargets(idBitsToAssign);final int childrenCount = mChildrenCount;if (newTouchTarget == null && childrenCount != 0) {final float x = ev.getX(actionIndex);final float y = ev.getY(actionIndex);// Find a child that can receive the event.// Scan children from front to back.final ArrayList<View> preorderedList = buildTouchDispatchChildList();final boolean customOrder = preorderedList == null&& isChildrenDrawingOrderEnabled();final View[] children = mChildren;for (int i = childrenCount - 1; i >= 0; i--) {final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);// If there is a view that has accessibility focus we want it// to get the event first and if not handled we will perform a// normal dispatch. We may do a double iteration but this is// safer given the timeframe.if (childWithAccessibilityFocus != null) {if (childWithAccessibilityFocus != child) {continue;}childWithAccessibilityFocus = null;i = childrenCount - 1;}if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);continue;}newTouchTarget = getTouchTarget(child);if (newTouchTarget != null) {// Child is already receiving touch within its bounds.// Give it the new pointer in addition to the ones it is handling.newTouchTarget.pointerIdBits |= idBitsToAssign;break;}resetCancelNextUpFlag(child);if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {// Child wants to receive touch within its bounds.mLastTouchDownTime = ev.getDownTime();if (preorderedList != null) {// childIndex points into presorted list, find original indexfor (int j = 0; j < childrenCount; j++) {if (children[childIndex] == mChildren[j]) {mLastTouchDownIndex = j;break;}}} else {mLastTouchDownIndex = childIndex;}mLastTouchDownX = ev.getX();mLastTouchDownY = ev.getY();newTouchTarget = addTouchTarget(child, idBitsToAssign);alreadyDispatchedToNewTouchTarget = true;break;}// The accessibility focus didn't handle the event, so clear// the flag and do a normal dispatch to all children.ev.setTargetAccessibilityFocus(false);}if (preorderedList != null) preorderedList.clear();}if (newTouchTarget == null && mFirstTouchTarget != null) {// Did not find a child to receive the event.// Assign the pointer to the least recently added target.newTouchTarget = mFirstTouchTarget;while (newTouchTarget.next != null) {newTouchTarget = newTouchTarget.next;}newTouchTarget.pointerIdBits |= idBitsToAssign;}}}// Dispatch to touch targets.if (mFirstTouchTarget == null) {// No touch targets so treat this as an ordinary view.handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);} else {// Dispatch to touch targets, excluding the new touch target if we already// dispatched to it.  Cancel touch targets if necessary.TouchTarget predecessor = null;TouchTarget target = mFirstTouchTarget;while (target != null) {final TouchTarget next = target.next;if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {handled = true;} else {final boolean cancelChild = resetCancelNextUpFlag(target.child)|| intercepted;if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {handled = true;}if (cancelChild) {if (predecessor == null) {mFirstTouchTarget = next;} else {predecessor.next = next;}target.recycle();target = next;continue;}}predecessor = target;target = next;}}// Update list of touch targets for pointer up or cancel, if needed.if (canceled|| actionMasked == MotionEvent.ACTION_UP|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {resetTouchState();} else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {final int actionIndex = ev.getActionIndex();final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);removePointersFromTouchTargets(idBitsToRemove);}}if (!handled && mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);}return handled;}
     private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {final boolean handled;// Canceling motions is a special case.  We don't need to perform any transformations// or filtering.  The important part is the action, not the contents.final int oldAction = event.getAction();if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {event.setAction(MotionEvent.ACTION_CANCEL);if (child == null) {handled = super.dispatchTouchEvent(event);} else {handled = child.dispatchTouchEvent(event);}event.setAction(oldAction);return handled;}// Calculate the number of pointers to deliver.final int oldPointerIdBits = event.getPointerIdBits();final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;// If for some reason we ended up in an inconsistent state where it looks like we// might produce a motion event with no pointers in it, then drop the event.if (newPointerIdBits == 0) {return false;}// If the number of pointers is the same and we don't need to perform any fancy// irreversible transformations, then we can reuse the motion event for this// dispatch as long as we are careful to revert any changes we make.// Otherwise we need to make a copy.final MotionEvent transformedEvent;if (newPointerIdBits == oldPointerIdBits) {if (child == null || child.hasIdentityMatrix()) {if (child == null) {handled = super.dispatchTouchEvent(event);} else {final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;event.offsetLocation(offsetX, offsetY);handled = child.dispatchTouchEvent(event);event.offsetLocation(-offsetX, -offsetY);}return handled;}transformedEvent = MotionEvent.obtain(event);} else {transformedEvent = event.split(newPointerIdBits);}// Perform any necessary transformations and dispatch.if (child == null) {handled = super.dispatchTouchEvent(transformedEvent);} else {final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;transformedEvent.offsetLocation(offsetX, offsetY);if (! child.hasIdentityMatrix()) {transformedEvent.transform(child.getInverseMatrix());}handled = child.dispatchTouchEvent(transformedEvent);}// Done.transformedEvent.recycle();return handled;}
            if (actionMasked == MotionEvent.ACTION_DOWN|| mFirstTouchTarget != null) {final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;if (!disallowIntercept) {intercepted = onInterceptTouchEvent(ev);ev.setAction(action); // restore action in case it was changed} else {intercepted = false;}} else {// There are no touch targets and this action is not an initial down// so this view group continues to intercept touches.intercepted = true;}

从这段代码看出,在ViewGroup.dispatchTouchEvent中调用onInterceptTouchEvent
调用的条件是: 1.ACTION_DOWN

        2.mFirstTouchTarget != null

mFirstTouchTarget != null

ViewGroup.dispatchTouchEvent:

if (!canceled && !intercepted) {if (actionMasked == MotionEvent.ACTION_DOWN|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {if (newTouchTarget == null && childrenCount != 0) {... ...for (int i = childrenCount - 1; i >= 0; i--) {... ...if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {... ...newTouchTarget = addTouchTarget(child, idBitsToAssign);... ...}... ...}}}
}

1.传递ACTION_DOWN
child.dispatchTouchEvent()
调用子View的dispatchTouchEvent方法
2.把dispatchTouchEvent返回值为true的子View串成一个链表
如果该链表为空,说明该View是最底层的View;
3.找到最底层的View,再从该View向上查找处理TouchEvent的目标View
super.dispatchTouchEvent()
这整个过程
View.java

    public boolean dispatchTouchEvent(MotionEvent event) {// If the event should be handled by accessibility focus first.if (event.isTargetAccessibilityFocus()) {// We don't have focus or no virtual descendant has it, do not handle the event.if (!isAccessibilityFocusedViewOrHost()) {return false;}// We have focus and got the event, then use normal event dispatch.event.setTargetAccessibilityFocus(false);}boolean result = false;if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onTouchEvent(event, 0);}final int actionMasked = event.getActionMasked();if (actionMasked == MotionEvent.ACTION_DOWN) {// Defensive cleanup for new gesturestopNestedScroll();}if (onFilterTouchEventForSecurity(event)) {if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {result = true;}//noinspection SimplifiableIfStatementListenerInfo li = mListenerInfo;if (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED&& li.mOnTouchListener.onTouch(this, event)) {result = true;}if (!result && onTouchEvent(event)) {result = true;}}if (!result && mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);}// Clean up after nested scrolls if this is the end of a gesture;// also cancel it if we tried an ACTION_DOWN but we didn't want the rest// of the gesture.if (actionMasked == MotionEvent.ACTION_UP ||actionMasked == MotionEvent.ACTION_CANCEL ||(actionMasked == MotionEvent.ACTION_DOWN && !result)) {stopNestedScroll();}return result;}

View.dispatchTouchEvent调用了mOnTouchListener.onTouch和onTouchEvent;
当onTouchEvent返回true,代表该View处理TouchEvent

            if (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED&& li.mOnTouchListener.onTouch(this, event)) {result = true;}if (!result && onTouchEvent(event)) {result = true;}

第二步,剩余的ACTION_XXX在View树传递给目标View;

    if (mFirstTouchTarget == null) {// No touch targets so treat this as an ordinary view.handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);} else {TouchTarget predecessor = null;TouchTarget target = mFirstTouchTarget;while (target != null) {final TouchTarget next = target.next;if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {handled = true;} else {final boolean cancelChild = resetCancelNextUpFlag(target.child)|| intercepted;if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {handled = true;}if (cancelChild) {if (predecessor == null) {mFirstTouchTarget = next;} else {predecessor.next = next;}target.recycle();target = next;continue;}}predecessor = target;target = next;}

遍历子View的链表传递ACTION_XXX

Android系统--TouchEvent的处理流程相关推荐

  1. android启动流程之lk,Android系统之LK启动流程分析(一)

    1.前言 LK是Little Kernel的缩写,在Qualcomm平台的Android系统中普遍采用LK作为bootloader,它是一个开源项目,LK是整个系统的引导部分,所以不是独立存在的,但是 ...

  2. Android系统完整的启动流程

    Android系统完整的启动过程,从系统层次角度可分为 Linux 系统层.Android 系统服务层.Zygote进程模型三个阶段:从开机到启动 Home Launcher 完成具体的任务细节可分为 ...

  3. Android系统字体加载流程

    一.背景 视觉同学提了一个需求,要求手机中显示的字体可以支持medium字体,经过分析,android原生的字体库中并没有中文的medium字体,如果使用bold,显示又太粗,为满足需求,需要分析an ...

  4. android系统自动休眠代码流程,一种基于Android系统下的可控休眠方法及系统与流程...

    本发明涉及一种手机应用,尤其涉及一种基于android系统下的可控休眠方法及系统. 背景技术: android设备的休眠指的是当长时间不用手机时手机会自动进入一个省电模式,调低或者关闭一部分电压的输出 ...

  5. 全志平台Android系统WIFI架构工作流程分析

    1. 前言 记录个人最近对Android wifi的了解和使用感受, 并为想入门.学习Android wifi的人员提供一定的参考 2. Android系统中的WIFI架构 wifi在Android中 ...

  6. android系统自动休眠代码流程,Android P 电源管理(4)待机流程

    电视遥控器,短端走待机(str待机)流程, 长按是关机,走关机流程,记录下待机流程. 参考博客 待机流程 Android P引入自动待机功能,只有存在WakeLock,wakeup_count就不会为 ...

  7. android系统相机实时数据采集流程,Android 摄像头采集与数据处理

    android Camera2使用 前言:由于有关camera2使用和对数据处理的比较少所以笔者也有着乐于助人心所以有了后面的内容.咋们废话不多说先把流程和目的说下.首先是获取到相关摄像头id.然后打 ...

  8. android系统相机实时数据采集流程,Android摄像头获取实时数据+Demo

    前言 首先需要用到的是一个叫Camera类的,更新的一个叫Camera2的类. Camera2是从 SDK 5.0(API Level 21)开始才被引入的.里面的方法有些还是比原来的方便许多的,但是 ...

  9. android swap 大小,android系统开swap简易流程

    写此博文,只因记性不好,备用.以华为U8220为例,其他机型不保证一定可行,请三思后再动手. 总的来说,总共2步,与PC类似:分区.装系统 要开swap,需要格机,重新对SD卡进行分区(如果还有好的方 ...

最新文章

  1. asp.net实现C#代码加亮显示
  2. 毕业季:理想很丰满,现实也可以很丰满!
  3. 运维人员处理云服务器故障的方法总结
  4. (0108)iOS开发之Xcode11: 删除默认Main.storyBoard、自定义根控制器
  5. 利用jQuery和CSS实现环形进度条
  6. 《Javascript高级程序设计》读书笔记之对象创建
  7. 矿泉水功能突破口-丰收节交易会·李喜贵:遵义谋定水产业
  8. 微博拟全球发售1100万股 发售价不超388港元
  9. 5月20日发!索尼Xperia 1国行版官宣:骁龙855+4K“带鱼”屏
  10. js如何操作或是更改sass里的变量
  11. 推荐一个vs自带工具分析代码的复杂度
  12. FastDFS入门一篇就够
  13. HTTP和HTTPS的工作原理及区别
  14. 关于html5毕业论文设计任务书,毕业论文设计任务书(精选多篇)
  15. python爬虫--看看虎牙女主播中谁最“顶”
  16. 大数据征信是个人信用风险管理的必然趋势
  17. 专业技能热门配方大全
  18. Sketch for UX Design Sketch UX设计教程 Lynda课程中文字幕
  19. 阿里企业邮箱-PC加载outlook插件
  20. 数字图像处理——第九章(形态学图像处理)

热门文章

  1. javascript中有关this的使用
  2. MySQL 用户与授权管理详解
  3. ORACLE11g 前期安装环境配置
  4. 摘自一个读者读后rework的感受
  5. Field types
  6. 搭建redis给mysql做缓存
  7. 【转】Windows 64bit下Python环境配置与集成IDE PyCharm
  8. C# 值类型与引用类型
  9. Java面试宝典——————Java基础部分(三)
  10. 1.3-date命令