The touch event handling mechanism of Android is more complex, especially after multi-touch and event interception are considered.
Android's Touch event processing is divided into 3 levels: Activity layer, ViewGroup layer, view layer.
Let's start with a few basic rules for touch event handling.
1. If the Action_down event is not handled at a certain level, then the layer will no longer receive subsequent touch events until the next Action_down event.
Description: A. A hierarchy that does not handle an event refers to it and its child view does not handle the event.
B. This rule does not apply to the activity layer (it is the top level) and they receive every touch event.
C. There would be no impact if such incidents were not dealt with action_move.
2. If the Action_down event occurs within a view, subsequent events such as ACTION_MOVE,ACTION_UP and Action_cancel will be sent to the view, even if the event is already out of bounds.
3. The first pressed finger triggers the Action_down event, then the pressed finger triggers the Action_pointer_down event, the middle-up finger triggers the action_pointer_up event, and the last-up finger triggers action_ Up event (even if it is not the finger that triggered the Action_down event).
The 4.pointer ID can be used to track your finger, from the moment it is pressed, the pointer ID takes effect until the moment it gets up, which remains the same.
5. If a Action_down event is intercepted by the parent view, No child view will receive any more touch events (this meets the 1th requirement).
6. If a non-Action_down event is intercepted by the parent view, the child view that last processed the Action_down event receives a Action_cancel event and no more touch events are received. Even if the parent view no longer intercepts subsequent touch events.
7. If the parent view decides to handle the touch event or the child view does not handle the touch event, the parent view handles the touch event as normal view does, otherwise it does not handle the touch event at all (it is only responsible for distributing).
8. If the parent view intercepts the event in Onintercepttouchevent, no more touch events are received in the Onintercepttouchevent, and the event is handed over to itself (as normal view is handled).
The following hierarchies describe some of the details.
1.Activity Layer:
public boolean dispatchtouchevent (motionevent ev) { if (ev.getaction () = = Motionevent.action_down) { Onuserinteraction (); } if (GetWindow (). superdispatchtouchevent (EV)) {//Here to the view layer to process return true; } return ontouchevent (EV); If the view layer is not processed, it is processed here}
2.View Layer:
public boolean dispatchtouchevent (Motionevent event) { //omitted some details listenerinfo li = mlistenerinfo; if (Li! = null && Li.montouchlistener! = null && (mviewflags & enabled_mask) = = ENABLED && ; Li.mOnTouchListener.onTouch (this, event)) { return true; } if (Ontouchevent (event)) { return true; } return false;}
View Ontouch method Code is more, the main logic is divided into two steps: First the event to touchdelegate processing (if any), if the touchdelegate do not handle and then self-processing, self-processing is mainly responsible for the view state of the transformation (such as pressed state), Long press event, click event Detection and trigger, etc.
3.ViewGroup layer (more complex):
The overall logic for the ViewGroup layer to handle touch events is to detect if interception is required, send sub-view processing without interception, and if the sub-view is not processed and processed on its own, the logic of self-processing is the same as view.
The logic of interception is that all events from down to up are treated as a set of events, and if intercepted from down, the subsequent other events within the group are completely left to themselves, no more interception logic is required, and if intercepted from the middle, the cancel event is sent to the child view first. Subsequent other events within the group are completely left to themselves and do not need to go into the interception logic anymore.
The logic of distribution is that in the case of a Action_down event, a child view is searched for processing, called Target, and if the target is not found, it is handled by the target, if target is found.
From the code perspective, Dispatchtouchevent is responsible for distributing logic, Ontouchevent is responsible for the real processing logic, generally should be overloaded ontouchevent, only special cases need to overload dispatchtouchevent.
Android's Touch event handling mechanism