// 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. finalbooleancanceled= resetCancelNextUpFlag(this) || actionMasked == MotionEvent.ACTION_CANCEL;
// Update list of touch targets for pointer down, if needed. finalbooleansplit= (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; TouchTargetnewTouchTarget=null; booleanalreadyDispatchedToNewTouchTarget=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. ViewchildWithAccessibilityFocus= ev.isTargetAccessibilityFocus() ? findChildWithAccessibilityFocus() : null;
if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { finalintactionIndex= ev.getActionIndex(); // always 0 for down finalintidBitsToAssign= 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);
finalintchildrenCount= mChildrenCount; if (newTouchTarget == null && childrenCount != 0) { finalfloatx= ev.getX(actionIndex); finalfloaty= ev.getY(actionIndex); // Find a child that can receive the event. // Scan children from front to back. //根据子控件在Z方向的大小,对子控件进行排序,上面的子控件应该先处理触摸事件 final ArrayList<View> preorderedList = buildOrderedChildList(); finalbooleancustomOrder= preorderedList == null && isChildrenDrawingOrderEnabled(); final View[] children = mChildren; for (inti= childrenCount - 1; i >= 0; i--) { //遍历所有的子控件
// 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; }
//canViewReceivePointerEvents检测子控件是否能接收触摸事件 //不可见的,正在执行动画的View不能接收触摸事件 //isTransformedTouchPointInView检测触摸点是否在子控件中 if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { //不满足条件的,跳过该子控件 ev.setTargetAccessibilityFocus(false); continue; }
newTouchTarget = getTouchTarget(child); if (newTouchTarget != null) { //查看child是否加入到mFirstTouchTarget链表中 // 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);
//dispatchTransformedTouchEvent,对事件进行一些转换,比如坐标转换成相对子控件边界的坐标 //并将转换后的事件分发给该子控件,该函数后面还会重点解析 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 //如果childIndex是指向排序后的列表,则需要找到在mChildren中的原始索引 for (intj=0; j < childrenCount; j++) { if (children[childIndex] == mChildren[j]) { mLastTouchDownIndex = j; break; } } } else { mLastTouchDownIndex = childIndex; } mLastTouchDownX = ev.getX(); mLastTouchDownY = ev.getY(); //将该子控件添加到mFirstTouchTarget链表的头部 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. // 将事件分发给mFirstTouchTarget保存的事件处理View if (mFirstTouchTarget == null) { // No touch targets so treat this as an ordinary view. // 没有子控件接收该事件,则分发给自己,即调用自己的onTouchEvent 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. TouchTargetpredecessor=null; TouchTargettarget= mFirstTouchTarget; while (target != null) { //遍历TouchTarget对象,将事件分发给所有需要处理该事件的所有控件 finalTouchTargetnext= target.next; if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { //如果在之前的处理过程中已经发给了target中保存的View,则直接设置handle为true,表示已经处理过了该事件 handled = true; } else { finalbooleancancelChild= 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; } } ... } ... return handled; }
if ((viewFlags & ENABLED_MASK) == DISABLED) { if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) { //ACTION_UP触发修改PFLAG_PRESSED标志位, //同时更换drawableStateList设置的不同状态下的drawable资源 setPressed(false); } // A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. // 即使DISABLED状态下的控件,只要满足: // CLICKABLE(可点击), // LONG_CLICKABLE(可长按), // CONTEXT_CLICKABLE(手写笔、鼠标相关,基本不用考虑) // 仍然消耗MotionEvent事件,只是不做响应 return (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE); }
if (mTouchDelegate != null) { //委托的触摸代理,将本控件一个区域映射到另一个控件的某个区域 //如果点击了委托的区域则将MotionEvent交给委托的View控件来处理 //详细的内容可以自行阅读TouchDelegate源码 if (mTouchDelegate.onTouchEvent(event)) { returntrue; } }
if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) { switch (action) { case MotionEvent.ACTION_UP: booleanprepressed= (mPrivateFlags & PFLAG_PREPRESSED) != 0; if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) { // take focus if we don't have it already and we should in // touch mode. // 如果没有获得焦点,那么点击后让其获取焦点 booleanfocusTaken=false; if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { focusTaken = requestFocus(); }
if (prepressed) { // The button is being released before we actually // showed it as pressed. Make it show the pressed // state now (before scheduling the click) to ensure // the user sees it. setPressed(true, x, y); }
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) { // This is a tap, so remove the longpress check removeLongPressCallback();
// Only perform take click actions if we were in the pressed state if (!focusTaken) { // Use a Runnable and post this rather than calling // performClick directly. This lets other visual state // of the view update before click actions start. if (mPerformClick == null) { mPerformClick = newPerformClick(); } if (!post(mPerformClick)) { //performClick中将会执行已经设置了onClickListener.onClick方法 performClick(); } } }
if (mUnsetPressedState == null) { mUnsetPressedState = newUnsetPressedState(); }
if (prepressed) { postDelayed(mUnsetPressedState, ViewConfiguration.getPressedStateDuration()); } elseif (!post(mUnsetPressedState)) { // If the post failed, unpress right now mUnsetPressedState.run(); }
case MotionEvent.ACTION_DOWN: mHasPerformedLongPress = false;
if (performButtonActionOnTouchDown(event)) { break; }
// Walk up the hierarchy to determine if we're inside a scrolling container. booleanisInScrollingContainer= isInScrollingContainer();
// For views inside a scrolling container, delay the pressed feedback for // a short period in case this is a scroll. if (isInScrollingContainer) { mPrivateFlags |= PFLAG_PREPRESSED; if (mPendingCheckForTap == null) { mPendingCheckForTap = newCheckForTap(); //CheckForTap的run方法中会调用checkForLongClick } mPendingCheckForTap.x = event.getX(); mPendingCheckForTap.y = event.getY(); postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); } else { // Not inside a scrolling container, so show the feedback right away setPressed(true, x, y); //该方法中会执行一个延时的LongClick checkForLongClick(0); } break; case MotionEvent.ACTION_CANCEL: ... break;