当前位置: 代码迷 >> Android >> 从源码解释Android事件散发机制
  详细解决方案

从源码解释Android事件散发机制

热度:19   发布时间:2016-04-28 00:10:40.0
从源码解释Android事件分发机制

在ViewRootImpl的setView方法中,用户的触摸按键消息是体现在窗体上的,而windowManagerService则是管理这些窗口,它一旦接收到用户对窗体的一些触摸按键消息,会进行相应的动作,这种动作是需要体现在具体的view上面,在Android中,一个具体的界面是由一个Activity呈现的,而Activity中则包含了一个window,此window中又包含了一个phoneWindow,这个phoneWindow才是真正意义上的窗口,它把一个框架布局进行了一定的包装,并提供了具体的窗口操作接口,phoneWindow中包含了一个DecorView,这个view才是包含整个Activity的ui,它将被attach到Activity主窗口中。所以说用户触摸按键的消息是由windowManagerService捕捉到然后交给phoneWindow中的DecorView进行相应的处理,而连接两者的桥梁则是一个ViewRoot类,ViewRoot类由windowManagerService创建,其内部有一个W类,这个W类是一个binder,负责WindowManagerService的ipc调用,W接收到windowManagerService发送过来的消息后,把消息传递给ViewRoot,进而传递给ActivityThread解析做出处理,

1、在ViewRootImpl.java类的setView方法中:

<span style="font-size:14px;"><span style="white-space:pre">	</span>res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,                            getHostVisibility(), mDisplay.getDisplayId(),mAttachInfo.mContentInsets, mInputChannel);</span>
在这里把mWindow传递给了window,这个mWindow就是W类的一个实例
  2、当具体的触摸按键消息发生后,会由ViewRootImpl类中WindowInputEventReceiver这个类的onInputEvent方法负责接收,其实也就是个回调。
<span style="font-size:14px;"><span style="white-space:pre">	</span>public void onInputEvent(InputEvent event) {       <span style="white-space:pre">		</span> enqueueInputEvent(event, this, 0, true); <span style="white-space:pre">	</span>}</span>
   
<span style="font-size:14px;">void enqueueInputEvent(InputEvent event,            InputEventReceiver receiver, int flags, boolean processImmediately) {        QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);        // Always enqueue the input event in order, regardless of its time stamp.        // We do this because the application or the IME may inject key events        // in response to touch events and we want to ensure that the injected keys        // are processed in the order they were received and we cannot trust that        // the time stamp of injected events are monotonic.        QueuedInputEvent last = mFirstPendingInputEvent;        if (last == null) {            mFirstPendingInputEvent = q;        } else {            while (last.mNext != null) {                last = last.mNext;            }            last.mNext = q;        }        if (processImmediately) {			// 立即处理事件            doProcessInputEvents();        } else {        // 将事件放到队列的最后            scheduleProcessInputEvents();        }    }</span>

 在enqueueInputEvent方法中,把event加入到队列的最后面,如果processImmediately为true,则直接调用doProcessInputEvents方法,否则scheduleProcessInputEvents被调用,这两个方法最后都调用了deliverInputEvent方法,用于分发输入事件。在这个方法中判断如果是按键事件,比如说back、home等,就会调用deliverKeyEvent分发事件,一些一般的移动事件调用deliverGenericMotionEvent方法。
<span style="font-size:14px;"> private void deliverInputEvent(QueuedInputEvent q) {        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent");        try {            if (q.mEvent instanceof KeyEvent) {		// 如果是按键事件,也就是back、home等按键                deliverKeyEvent(q);            } else {            	// touch事件                final int source = q.mEvent.getSource();                if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {                    deliverPointerEvent(q);                } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {                    deliverTrackballEvent(q);                } else {                    deliverGenericMotionEvent(q);                }            }        } finally {            Trace.traceEnd(Trace.TRACE_TAG_VIEW);        }    }</span>
在deliverPointerEvent方法中,如果view对象不存在,或者没有被添加,则这个event不会被处理,直接finishInputEvent。接着判断action是否是MotionEvent.ACTION_DOWN,如果是,则表示触摸方式改变了,需要告诉windowManager在本地进行处理,因为每一次的按下操作都代表了一个新的event事件的到来。然后记录触摸的位置,这个位置就代表了应该是哪一个view来接收这个事件,然后直接调用mView.dispatchPointerEvent(event)来分发这个事件,如果这个事件被分发下去了,则结束事件。
<span style="font-size:14px;">  private void deliverPointerEvent(QueuedInputEvent q) {        final MotionEvent event = (MotionEvent)q.mEvent;        final boolean isTouchEvent = event.isTouchEvent();        if (mInputEventConsistencyVerifier != null) {            if (isTouchEvent) {                mInputEventConsistencyVerifier.onTouchEvent(event, 0);            } else {                mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);            }        }        // If there is no view, then the event will not be handled.        if (mView == null || !mAdded) {			// view对象为空,或者没有被添加,这个事件就不会被处理            finishInputEvent(q, false);            return;        }        // Translate the pointer event for compatibility, if needed.        if (mTranslator != null) {            mTranslator.translateEventInScreenToAppWindow(event);        }        // Enter touch mode on down or scroll.        final int action = event.getAction();        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) {			// 如果是MotionEvent.ACTION_DOWN			// 如果是如果触摸方式改变,告诉wm,在本地进行处理			// 每一次的按下操作就是一个触摸事件的改变            ensureTouchMode(true);        }		         // Offset the scroll position.        if (mCurScrollY != 0) {            event.offsetLocation(0, mCurScrollY);        }        if (MEASURE_LATENCY) {            lt.sample("A Dispatching PointerEvents", System.nanoTime() - event.getEventTimeNano());        }        // Remember the touch position for possible drag-initiation.        // 有可能拖拽开始,记录触摸的位置        if (isTouchEvent) {            mLastTouchPoint.x = event.getRawX();            mLastTouchPoint.y = event.getRawY();        }        // Dispatch touch to view hierarchy.        // 给view的层级上view分发事件        //  DecorView继承FrameLayout也就间接继承了ViewGroup,View        // DoverView---->Activity-->PhoneWindow--->DocerView---->ViewGroup        boolean handled = mView.dispatchPointerEvent(event);        if (MEASURE_LATENCY) {            lt.sample("B Dispatched PointerEvents ", System.nanoTime() - event.getEventTimeNano());        }        if (handled) {			// 结束事件            finishInputEvent(q, true);            return;        }        // Pointer event was unhandled.        // 暗示事件已经被处理        finishInputEvent(q, false);    </span>
在DecorView中没有dispatchPointerEvent方法,所以调用的是View.java的dispatchPointerEvent方法中判断具体的是哪一类的事件,然后又调用了自身的dispatchTouchEvent。
<span style="font-size:14px;">public final boolean dispatchPointerEvent(MotionEvent event) {        if (event.isTouchEvent()) {			// 如果是触摸事件            return dispatchTouchEvent(event);        } else {        	// 如果是一般的移动事件            return dispatchGenericMotionEvent(event);        }    }</span>

在DecorView.java的dispatchTouchEvent方法中,如果Activity不为空,并且没有被销毁,则调用Activity的dispatchTouchEvent方法,否则调用父类View的dispatchTouchEvent方法。
<span style="white-space:pre">	</span>@Override        public boolean dispatchTouchEvent(MotionEvent ev) {        	// callback就是Activity本身            final Callback cb = getCallback();			// 如果Activity不为空,并且没有被销毁,则调用Activity的dispatchTouchEvent			// 否则调用父类的dispatchTouchEvent            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)                    : super.dispatchTouchEvent(ev);        }
在Activity的dispatchTouchEvent方法中,调用的是PhoneWindow的superDispatchTouchEvent的方法去分发事件,如果到最后Activity中的所有的view都不去处理这个事件时,就有Activity的OnTouchEvent来处理。
public boolean  dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {			// 在dispatch之前做一些操作,其实什么也没做            onUserInteraction();        }		// 调用PhoneWindow中的superDispatchTouchEvent		// PhoneWindow中superDispatchTouchEvent 直接调用了mDecor的superDispatchTouchEvent		// mDecore的superDispatchTouchEvent方法中直接调用super.dispatchOnTouchEvent		// 也就是开始进入了viewGroup中的dispatchOnTouchEvent方法        if (getWindow().superDispatchTouchEvent(ev)) {            return true;        }		//  当Activity中所有的View都不处理Event的时候,就用由Activity的onTouchEvent()来处理		// 通知window关闭这个touch事件		return onTouchEvent(ev);    }

 public boolean onTouchEvent(MotionEvent event) {        if (mWindow.shouldCloseOnTouch(this, event)) {            finish();            return true;        }                return false;    }
PhoneWindow的superDispatchTouchEvent中,调用了DecorView的superDispatchTouchEvent方法,进而调用了super.dispatchTouchEvent(event)方法,也就是进入了viewGroup中开始事件的分发。
 @Override    public boolean superDispatchTouchEvent(MotionEvent event) {        return mDecor.superDispatchTouchEvent(event);    }

 public boolean superDispatchTouchEvent(MotionEvent event) {            return super.dispatchTouchEvent(event);        }
ViewGroup的dispatchTouchEvent中,处理如下,如果action是MotionEvent.ACTION_DOWN,则需要重置触摸的状态。继而判断是否拦截此事件,然后遍历所有的孩子以便找到一个可以接收此事件的孩子,如果child不存在TouchTarget中,则把事件分发给子view,这个寻找是根据view的区域来寻找的。如果childView没有消费掉此事件,则自己处理的事件,如果自己也没有处理,回溯至父view处理,否则viewGroup把事件一级一级的递归传递,如果child是一个viewGroup,则重复上述的步骤,如果是view,直接调用dispatchTouchEvent方法。
 public boolean dispatchTouchEvent(MotionEvent ev) {        if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);        }        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.                // ACTION_DOWN意味着touch事件的改变,所以需要把之前的TouchTargets和TouchState都clear掉,mFirstTouchTarget = null                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) {					// 拦截事件,默认返回false,表示不拦截,如果拦截了就不继续往下面执行了                    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;            }            // 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 (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 (childrenCount != 0) {                        // Find a child that can receive the event.                        // Scan children from front to back.                        // 遍历所有的孩子,以便找到一个可以接收这个事件的孩子                        // 某个区域内的孩子                        final View[] children = mChildren;                        final float x = ev.getX(actionIndex);                        final float y = ev.getY(actionIndex);                        final boolean customOrder = isChildrenDrawingOrderEnabled();                        for (int i = childrenCount - 1; i >= 0; i--) {                            final int childIndex = customOrder ?                                    getChildDrawingOrder(childrenCount, i) : i;                            final View child = children[childIndex];                            if (!canViewReceivePointerEvents(child)                                    || !isTransformedTouchPointInView(x, y, child, null)) {                                continue;                            }							// 判断child是否在TouchTarget中                            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);							// child不存在TouchTarget中,则调用dispatchTransformedTouchEvent							// 把event分发给子view,这里并不是做处理,应该就是找到touch区域                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {                                // Child wants to receive touch within its bounds.                                mLastTouchDownTime = ev.getDownTime();                                mLastTouchDownIndex = childIndex;                                mLastTouchDownX = ev.getX();                                mLastTouchDownY = ev.getY();                                newTouchTarget = addTouchTarget(child, idBitsToAssign);                                alreadyDispatchedToNewTouchTarget = true;                                break;                            }                        }                    }                    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) {				// mFirstTouchTarget 为空,表示childview没有将此事件消费掉,则自己处理这个event				// 如果viewGroup自己也没有处理,则回溯到父view进行处理                // 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;						// viewGroup把事件递归传递,如果child是一个gourp,则重复上述步骤						// 如果是view,则直接调用dispatchTouchEvent方法                        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;    }
在View.java中的 dispatchTouchEvent方法中,如果已经注册了listener监听器并且是enable的,并且监听器的onTouch返回true,则onTouchEvent不会被调用。
public boolean dispatchTouchEvent(MotionEvent event) {        if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onTouchEvent(event, 0);        }        if (onFilterTouchEventForSecurity(event)) {            //noinspection SimplifiableIfStatement            ListenerInfo li = mListenerInfo;            if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED                    && li.mOnTouchListener.onTouch(this, event)) {                // 要想执行onTouchEvent方法上述三个条件只要一个不满足就可以了                //                 return true;            }			            if (onTouchEvent(event)) {                return true;            }			// 如果所有的View都不处理TouchEvent,最后由Activity来处理        }        if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);        }        return false;    }

如果Activity中所有的view都不处理这个事件,则由Activity自己处理
public boolean onTouchEvent(MotionEvent event) {        if (mWindow.shouldCloseOnTouch(this, event)) {            finish();            return true;        }                return false;    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案