Each ViewGroup subclass in the Android system has the following methods that are closely related to the next three and touchevent processing:
1) Public boolean dispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent
2) Public boolean onintercepttouchevent (motionevent ev) This method is used to intercept touchevent
3) Public boolean ontouchevent (motionevent ev) This method is used to process touchevent
Note: Not all of the view's subclasses, many of the tutorials are talking about all of the view's subclasses, only the control that can add view to it needs to be distributed, such as TextView itself is the smallest view, so no longer to its sub-view distribution, it also has no sub-view, So it has no dispatch and intercept, only touchevent.
Description: White is the outermost layer and it fills the entire screen;
Red is the middle area, which belongs to a layer of white;
Black is the central area and must be a layer in the red.
Note: They are essentially: linearlayout, not relativelayout or other layouts.
1. Handle touch events from the central area
The layout file is as follows:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- android:orientation= "Vertical" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_out"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- Android:background= "#fff"
- android:gravity= "Center" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_mid"
- Android:layout_width= "300px"
- android:layout_height= "400px"
- Android:background= "#f00"
- android:gravity= "Center" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_center"
- Android:layout_width= "150px"
- android:layout_height= "150px"
- Android:background= "#000"
- android:gravity= "Center"
- Android:clickable= "true" >
- </com.kris.touch.widget.TouchView>
- </com.kris.touch.widget.TouchView>
- </com.kris.touch.widget.TouchView>
- </LinearLayout>
Copy Code
Note: android:clickable= "true"
Next we look at the printed log:
The combination is the above log, we can look at the Action_down event processing flow:
Description
First touch event occurs (Action_down), the event is distributed by the system calling the activity's Dispatchtouchevent method. This event is passed to the out Dispatchtouchevent processing based on the coordinates of the touch event, and out calls Onintercepttouchevent to determine whether the event is handled by itself or continues to be distributed to the child view. Because out does not handle the touch event, the event is passed to the immediate child view of the out (that is, middle), based on the event's coordinates.
Middle and center in the event processing process ibid. But because the center component is clickable that it can handle touch events, the Onintercepttouchevent method in the center passes events to the center's own ontouchevent method processing. At this point, this touch event has been processed without continuing delivery.
2. Do not specify who will handle the touch event
The layout file is as follows:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- android:orientation= "Vertical" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_out"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- Android:background= "#fff"
- android:gravity= "Center" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_mid"
- Android:layout_width= "300px"
- android:layout_height= "400px"
- Android:background= "#f00"
- android:gravity= "Center" >
- <com.kris.touch.widget.touchview
- Android:id= "@+id/view_center"
- Android:layout_width= "150px"
- android:layout_height= "150px"
- Android:background= "#000"
- android:gravity= "Center" >
- </com.kris.touch.widget.TouchView>
- </com.kris.touch.widget.TouchView>
- </com.kris.touch.widget.TouchView>
- </LinearLayout>
Copy Code
Note: Just less than the last layout android:clickable= "true"
Next we'll look at the printed log
The combination is the above log, we can look at the Action_down event processing flow:
Description
The event processing flow is roughly the same as the difference is that in this state, all components do not handle the event, and the event is not "consumed" by the center's Ontouchevent method, the event is passed back to the activity at a layer level, and if the activity does not handle this event, This event is equivalent to disappearing (no effect).
For subsequent move, up events, the up event is handled by the activity's dispatchtouchevent directly to its own Ontouchevent method because the first down event has been determined by the activity to handle the event.
Please see the final attachment for the code.
Summarize:
1) TouchEvent, the return value is true, it means that the event is consumed, the return value is false, it is not consumed, will continue to pass, this is the most basic.
2) Two ways of event delivery:
Tunnel mode: Passes from the root element down to the most inner child element or in an intermediate element because a condition stops passing.
Bubbling: passes outward from the most inner child element to the root element or to an element in the middle because a condition stops passing. Android's distribution logic for touch event is that the view is distributed from the upper layer to the lower layer (dispatchtouchevent function) similar to tunneling, then the lower level first begins processing the event (first Montouchlistener, Ontouchevent) and returns the processing status (Boolean value), if true, the upper layer is no longer processed. Similar to bubbling mode
So the problem arises, if you try to reach the top of the touch event (only by returning false to the upper layer), then the various sub-view below will not be able to handle the subsequent events. And sometimes we need to handle touch events on the lower and upper levels.
For example, Viewflipper used to detect gestures, in the interior we put a few images, a bit like the gallery effect, that is, the left and right slide to switch the picture, but sometimes we hope that the image can be enlarged zoom out! There is a need for a touch event in the Viewflipper, and a touch event in the image (you can drag the picture when the picture is the size of the screen, rather than toggling the picture around).
Android Touch Event delivery mechanism parsing