编程语言
首页 > 编程语言> > 【View系列】View事件分发源码探索,安卓常见面试题知乎

【View系列】View事件分发源码探索,安卓常见面试题知乎

作者:互联网

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

//actionDown是一系列事件的开头 需要重置所有状态

if (actionMasked == MotionEvent.ACTION_DOWN) {

cancelAndClearTouchTargets(ev);

resetTouchState();

}

final boolean intercepted;

//如果是down事件 或者mFirstTouchTarget不为空

if (actionMasked == MotionEvent.ACTION_DOWN

|| mFirstTouchTarget != null) {

//如果子类并没有调用parent.requestDisallowInterceptTouchEvent(true) 那么这个值为false

final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;

//如果子类调用了requestDisallowInterceptTouch(true) 则这个if判断条件不成立 反之则会进去

if (!disallowIntercept) {

//进入viewGroup的onInterceptTouchEvent()方法 默认返回false

intercepted = onInterceptTouchEvent(ev);

ev.setAction(action); // restore action in case it was changed

} else {

intercepted = false;

}

} else {

//如果没有子view消费了事件 并且事件不是down事件 intercepted 为true

intercepted = true;

}

//如果没有cancel而且viewGroup本身也没拦截事件 则进入这个代码块

if (!canceled && !intercepted) {

//如果事件是down事件 则进入代码块

if (actionMasked == MotionEvent.ACTION_DOWN

|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)

|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {

final int childrenCount = mChildrenCount;

if (newTouchTarget == null && childrenCount != 0) {

final View[] children = mChildren;

//倒序遍历child查看child 是否消费了事件

for (int i = childrenCount - 1; i >= 0; i–) {

//child.canReceivePointerEvents() 标识child是否能接收触摸事件 判断条件是可见并且没有执行动画

//!isTransformedTouchPointInView(x, y, child, null) 用来判断触摸事件的x,y 是否在child的矩形区域内

//两个判断有一个条件不满足就会遍历下一个child

if (!child.canReceivePointerEvents()|| !isTransformedTouchPointInView(x, y, child, null)) {

continue;

}

//调用子view的dispatchTouchEvent 在view的dispatchTouchEvent里面调用了onTouchEvent()

//如果子view消费了事件 则dispatchTransformedTouchEvent()返回true 进入代码块执行逻辑

if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {

//赋值newTouchTarget

//addTouchTarget()方法会将mFirstTouchTarget赋值 方便下面的事件系列直接拿到 //mFirstTouchTarget.child并调用其dispatchTouchEvent方法

newTouchTarget = addTouchTarget(child, idBitsToAssign);

//将其设置为true 下面代码会用到

alreadyDispatchedToNewTouchTarget = true;

break;

}

}

}

}

if (mFirstTouchTarget == null) {

// mFirstTouchTarget == null 代表没有子view消费事件

// 如果没有子view消费事件那么以后则直接调用dispatchTransformedTouchEvent方法 结果实际上是直接调用自己的onTouchEvent()

// 注意 第三个参数出的是null 会直接调用到super.dispatchTouchEvent()即view的dispatchTouchEvent()

//view的dispatchTouchEvent会调用onTouchEvent

handled = dispatchTransformedTouchEvent(ev, canceled, null,

TouchTarget.ALL_POINTER_IDS);

} else {

//如果mFirstTouchTarget不为null(代表有子view消费了事件) 则走进这个代码块

TouchTarget predecessor = null;

TouchTarget target = mFirstTouchTarget;

while (target != null) {

final TouchTarget next = target.next;

//down事件的时候如果有子view消费事件了 alreadyDispatchedToNewTouchTarget值会为true 并且

//target和newTouchTarget地址值是一样的

if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {

handled = true;

} else { //其他事件(除了down)都会走这个代码块

//如果viewGroup拦截了事件 则会导致cancelChild为true 此时传递子view的event.action 是ACTION_CANCEL

final boolean cancelChild = resetCancelNextUpFlag(target.child)

|| intercepted;

//继续调用子view的dispatchTouchEvent

//注意第三个参数这时候传的是target.child 并不是null 内部会target.child.dispatchTouchEvent()

if (dispatchTransformedTouchEvent(ev, cancelChild,

target.child, target.pointerIdBits)) {

handled = true;

}

//如果取消child事件

//那么会导致mFirstTouchTarget = target.next

//通常单指事件cancelChild 直接会导致mFirstTouchTarget为null

if (cancelChild) {

if (predecessor == null) {

mFirstTouchTarget = next;

} else {

predecessor.next = next;

}

//回收target

target.recycle();

target = next;

continue;

}

}

predecessor = target;

target = next;

}

}

return handled;

}

public boolean onInterceptTouchEvent(MotionEvent ev) {

return false;

}

private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,

View child, int desiredPointerIdBits) {

if (child == null) {

//调用view的dispatchTouchEvent

handled = super.dispatchTouchEvent(transformedEvent);

else {

//调用子view的dispatchTouchEvent

handled = child.dispatchTouchEvent(transformedEvent);

}

return handled;

}

//View.java

//View 的dispatchTouchEvent

public boolean dispatchTouchEvent(MotionEvent event) {

//首先查看是否设置有onTouchListener 如果有并且返回了true 则不会执行onTouchEvent

//下面的代码说明onTouchListener优先级高于onTouchEvent

ListenerInfo 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;

}

return result;

}

public boolean onTouchEvent(MotionEvent event) {

//标识view是否可点击 设置onClickListener也会导致clickable为true

final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE

|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)

|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;

if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {

switch(action) {

case MotionEvent.ACTION_UP:

//回调了OnClickListener的onClick方法

performClickInternal();

break;

}

//如果view可点击 直接就返回true了

return true;

}

//如果view不可点击 返回false

return false;

}

复制代码

通过上面的源码分析,我们可以总结一下基础流程:


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

如果不拦截 倒序遍历children,让每个子view 调用自己的dispatchTouchEvent方法**

if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {handled = true; }

然后返回handled;

if (dispatchTransformedTouchEvent(ev, cancelChild, target.child, target.pointerIdBits)) {handled = true;}

还是间接调用了child的dispatchTouchEvent方法

标签:面试题,null,target,dispatchTouchEvent,源码,child,View,true,view
来源: https://blog.csdn.net/flunsna/article/details/121889277