A bad pen is better than a good memory. Note your own use to impress

1. Event definition: Click event Touch. An event is generated when the user touches the View ViewGroup

2. Event encapsulation: Information about the event is encapsulated as a MotionEvent.

ACTION_DOWN MotionEvent.ACTION_UP MotionEvent.ACTION_MOVE MotionEvent.ACTION_CANCEL

/** * Constant for {@link #getActionMasked}: A pressed gesture has started, the * motion contains the initial starting location. * <p> * This is also a good time to check the button state to distinguish * secondary and tertiary button clicks and handle them appropriately. * Use {@link #getButtonState} to retrieve the button state. * </p> */ public static final int ACTION_DOWN = 0; /** * Constant for {@link #getActionMasked}: A pressed gesture has finished, the * motion contains the final release location as well as any intermediate * points since the last down or move event.  */ public static final int ACTION_UP = 1; /** * Constant for {@link #getActionMasked}: A change has happened during a * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). * The motion contains the most recent point, as well as any intermediate * points since the last down or move event. */ public static final int ACTION_MOVE = 2; /** * Constant for {@link #getActionMasked}: The current gesture has been aborted. * You will not receive any more points in it. You should treat this as * an up event, but not perform any action that you normally would. */ public static final int ACTION_CANCEL = 3;Copy the code

There are many moves in the middle of the event from down to up, so a lot of real-time location calculation needs to be carried out in the move

Cancel You should treat this as an up event

Sending a MotionEvent to the view and processing it is the essence of event distribution

The order of the objects in which events are passed is: Activity –> ViewGroup –> View

The methods involved in event distribution are:

 /**
 * Pass the touch screen motion event down to the target view, or this
 * view if it is the target.
 *
 * @param event The motion event to be dispatched.
 * @return True if the event was handled by the view, false otherwise.
 */
public boolean dispatchTouchEvent(MotionEvent event) {
    xxx
}



 /**
 * Implement this method to handle touch screen motion events.
 * <p>
 * If this method is used to detect click actions, it is recommended that
 * the actions be performed by implementing and calling
 * {@link #performClick()}. This will ensure consistent system behavior,
 * including:
 * <ul>
 * <li>obeying click sound preferences
 * <li>dispatching OnClickListener calls
 * <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when
 * accessibility features are enabled
 * </ul>
 *
 * @param event The motion event.
 * @return True if the event was handled, false otherwise.
 */
public boolean onTouchEvent(MotionEvent event) {
    xxxxx
}
Copy the code

These two methods are in the View class

/**
 * A helper class to handle touches on the heads-up views.
 */
public class HeadsUpTouchHelper implements Gefingerpoken {
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        xxxx
    }
}
Copy the code

This is just a little bit of detail behind the general concept of event distribution