The previous article focused on the TouchEvent distribution process for Android, with two important functions: Onintercepttouchevent and Ontouchevent, which can be re-installed to complete specific logic. The onintercepttouchevent is defined as ViewGroup, and the default return value is False, which means that touchevent is not intercepted. The definition of ontouchevent is in view, and super.ontouchevent is used when viewgroup to invoke Ontouchevent. ViewGroup call Ontouchevent returns false by default, indicating that the touch event is not consumed, and the view call Ontouchevent returns True by default, indicating that the touch event is consumed. Given that onintercepttouchevent and Ontoucheven are often used when writing the UI, here's an example to explain.
First create a class MyView, inherited from the view
[Java]View PlainCopy
- Public class MyView extends Button {
- private static final String TAG = MyView. Class.getname ();
- Public MyView (context context) {
- super (context);
- }
- Public MyView (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- @Override
- Public Boolean ontouchevent (Motionevent event) {
- LOG.D (TAG, "ontouchevent.");
- Logutil.logaction (event, TAG);
- return super.ontouchevent (event);
- }
- }
Create class Mylayout, inherit from ViewGroup
[Java]View PlainCopy
- Public class Mylayout extends framelayout{
- private static final String TAG = mylayout. Class.getname ();
- Public Mylayout (context context) {
- super (context);
- }
- Public mylayout (context context, AttributeSet AttributeSet) {
- Super (context, attributeset);
- }
- @Override
- Public Boolean onintercepttouchevent (motionevent ev) {
- LOG.D (TAG, "onintercepttouchevent");
- return super.onintercepttouchevent (EV);
- }
- @Override
- Public Boolean ontouchevent (Motionevent event) {
- LOG.D (TAG, "ontouchevent.");
- Logutil.logaction (event, TAG);
- return super.ontouchevent (event);
- }
- }
The Logutil.logaction () function is the type of action used to print the Motionevent, as follows:
[Java]View PlainCopy
- Public class Logutil {
- public static void Logaction (Motionevent event, final String tag) {
- int action = Event.getaction ();
- switch (action) {
- Case Motionevent.action_down:
- LOG.D (Tag, "Action down");
- Break ;
- Case Motionevent.action_cancel:
- LOG.D (Tag, "Action Cancel");
- Break ;
- Case MOTIONEVENT.ACTION_UP:
- LOG.D (Tag, "Action up");
- Break ;
- Case Motionevent.action_move:
- LOG.D (Tag, "Action move");
- Break ;
- Default:
- LOG.D (Tag, "Unknow action");
- }
- }
- }
The layout file Main.xml myview nested in mylayout with the following code:
[HTML]View PlainCopy
- <view android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- class= "com.example.AndroidTest.MyLayout" xmlns:android= "http://schemas.android.com/apk/res/ Android "
- android:id="@+id/view">
- <Com.example.AndroidTest.MyView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Hello" />
- </View>
The code for Mainactivity is as follows:
[Java]View PlainCopy
- Public class Mainactivity extends Activity {
- public static final String TAG = "Touchdemoactivity";
- @Override
- public void OnCreate (Bundle savedinstancestate)
- {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- }
- }
After the program starts, the following.
The running results of the program are discussed below.
1, the Mylayout onintercepttouchevent returns the ontouchevent of False,myview returns True
Scenario 1: When you click anywhere in the blue box, only mylayout receives the event, and the output log is as follows:
As you can see, the touch event is eventually received by the Mylayout Ontouchevent.
Case 2: Click the Black area in the red box, because Onintercepttouchevent () returns false, so myview can also receive the TouchEvent event, the output log is as follows:
As you can see, because MyView Ontouchevent returns true by default, the touch event is consumed and ontouchevent in Mylayout will not be called.
When our fingers press the black area, stay for a few seconds and then lift up, get the log like:
As you can see, the type of the first event is action down, the last action up, and the middle is the type of action move, which exactly matches the Android gesture definition described in the previous article.
2, Mylayout onintercepttouchevent return to False,myview Ontouchevent return False
Overwrite the ontouchevent code in MyView to return false
[Java]View PlainCopy
- @Override
- Public Boolean ontouchevent (Motionevent event) {
- LOG.D (TAG, "ontouchevent.");
- Logutil.logaction (event, TAG);
- return false;
- }
Since MyView does not consume the touch event, Mylayout's ontouchevent will be called and the print log is as follows:
As you can see, the type of touch is only action down.
3, Mylayout's onintercepttouchevent return True
Overwrite the onintercepttouchevent code in the Mylayout, so that it returns true
[Java]View PlainCopy
- @Override
- Public Boolean onintercepttouchevent (motionevent ev) {
- LOG.D (TAG, "onintercepttouchevent");
- return true;
- }
Because mylayout intercepts the touch event, the ontouchevent in MyView will not be called, log as follows:
The above description of Android Onintercepttouchevent and ontouchevent if there is something wrong, please correct me.
This article refers to the code from: Two minutes to thoroughly let you understand Android Onintercepttouchevent and ontouchevent (graphics)!, thanks to the author's selfless sharing.
Introduction to Android Touch System (II): A detailed example of the invocation process of onintercepttouchevent and ontouchevent