触摸消息事件从Activity的dispatchTouchEvent()开始,首先如果是down事件就会调用onUserinteraction。然后调用window.superDispatchTouch(),此window是PhoneWindow,而superDispatchTouch调用的就是DecorView的superDispatchTouch。而DecorView有时继承了FrameLayout,所以事件是从ViewGroup的dispatchTouchEvent()
Activity.class
/**
* Called to process touch screen events. You can override this to
* intercept all touch screen events before they are dispatched to the
* window. Be sure to call this implementation for touch screen events
* that should be handled normally.
*
* @param ev The touch screen event.
*
* @return boolean Return true if this event was consumed.
*/
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
ViewGroup.class
dispatchTouchEvent:主要分发事件
/**
* ViewGroup.dispatchTouchEvent
*
* @param ev
* @return
*/
@Override
public boolean dispatchTouchEvent(MotionEvent 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