Android开发总结笔记 ViewGroup的事件分发机制 3-10
2016-01-05 10:30
916 查看
参考:/article/1503767.html
事实上,当点击某个组件。会先出发它所在的ViewGroup的dispatchTouchEvent方法,然后才分发到View的
所以,来看一下ViewGroup的dispatchTouchEvent方法
[/code]
分步解析
第一步:15-21行,对ACTION_DOWN进行处理
在这里会对ACTION_DOWN进行一些初始化的操作
[/code]
第二步:23-43行,
这段代码中决定了intercepted的值,而这个值是用来标记ViewGroup是否拦截Touch事件的传递的。
[/code]
一开始判断是否新起一轮Touch事件,不是的话直接拦截
disallowIntercept这个值默认是false,
可以通过ViewGroup提供的public方法requestDisallowInterceptTouchEvent方法来设置
onInterceptTouchEvent方法默认是false,可以通过重写返回true
第三步:43-45,检查cancel
[/code]
通过标记和action检查cancel
第五步:47-函数结束,事件分发
在54行可以看到一个split变量,默认是true,作用是是否把事件分发给多个子View
在ViewGroup中提供了一个public方法设置setMotionEventSplittingEnabled
50行的
[/code]
事件如果不是ACTION_CANCEL并且标志位intercepted为false则会进入
到这里。。思路大概也差不多了。详细的,还是看开头那个文章把。。。
事实上,当点击某个组件。会先出发它所在的ViewGroup的dispatchTouchEvent方法,然后才分发到View的
所以,来看一下ViewGroup的dispatchTouchEvent方法
public 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 down
final 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 = buildOrderedChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder
? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null)
? children[childIndex] : preorderedList.get(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 index
for (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;
}
[/code]
分步解析
第一步:15-21行,对ACTION_DOWN进行处理
在这里会对ACTION_DOWN进行一些初始化的操作
cancelAndClearTouchTargets(ev); //里面有一个重要的操作,将mFirstTouchTarget设为null
resetTouchState();// 重置Touch状态标识
[/code]
第二步:23-43行,
这段代码中决定了intercepted的值,而这个值是用来标记ViewGroup是否拦截Touch事件的传递的。
/ 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;
}
[/code]
一开始判断是否新起一轮Touch事件,不是的话直接拦截
disallowIntercept这个值默认是false,
可以通过ViewGroup提供的public方法requestDisallowInterceptTouchEvent方法来设置
onInterceptTouchEvent方法默认是false,可以通过重写返回true
第三步:43-45,检查cancel
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
[/code]
通过标记和action检查cancel
第五步:47-函数结束,事件分发
在54行可以看到一个split变量,默认是true,作用是是否把事件分发给多个子View
在ViewGroup中提供了一个public方法设置setMotionEventSplittingEnabled
50行的
if (!canceled && !intercepted)
[/code]
事件如果不是ACTION_CANCEL并且标志位intercepted为false则会进入
到这里。。思路大概也差不多了。详细的,还是看开头那个文章把。。。
相关文章推荐
- Android驱动编译mmm命令的小tip
- Android中selector背景选择器
- Android Studio之导入外部so库
- Android View之组合控件实例(一)
- Android4.4 RIL的AT命令增加流程
- Android继承Parcelable接口传递复杂数据类型
- android studio 快捷键
- Android 引导界面制作
- ANDROID SHAPE画圆形背景_ANDROID实现角标布局
- Android生成圆形图片
- Android 给空白包签名并上传审核
- android
- Android自定义控件(一)
- Android动画之补间动画(Tween Animation)实例详解
- android屏幕适配
- Android线程的创建与销毁
- android 通过UncaughtExceptionHandler处理和上传错误日志
- android中的广播接收实现总结 and Android 广播大全
- Android退出应用最优雅的方式(改进版)
- Android夜间模式最佳实践