Introduction to Android Touch System (II): A detailed example of the invocation process of onintercepttouchevent and ontouchevent

Source: Internet
Author: User

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 
  1. Public class MyView extends Button {
  2. private static final String TAG = MyView.   Class.getname ();
  3. Public MyView (context context) {
  4. super (context);
  5. }
  6. Public MyView (context context, AttributeSet attrs) {
  7. Super (context, attrs);
  8. }
  9. @Override
  10. Public Boolean ontouchevent (Motionevent event) {
  11. LOG.D (TAG, "ontouchevent.");
  12. Logutil.logaction (event, TAG);
  13. return super.ontouchevent (event);
  14. }
  15. }


Create class Mylayout, inherit from ViewGroup

[Java]View PlainCopy 
  1. Public class Mylayout extends framelayout{
  2. private static final String TAG = mylayout.   Class.getname ();
  3. Public Mylayout (context context) {
  4. super (context);
  5. }
  6. Public mylayout (context context, AttributeSet AttributeSet) {
  7. Super (context, attributeset);
  8. }
  9. @Override
  10. Public Boolean onintercepttouchevent (motionevent ev) {
  11. LOG.D (TAG, "onintercepttouchevent");
  12. return super.onintercepttouchevent (EV);
  13. }
  14. @Override
  15. Public Boolean ontouchevent (Motionevent event) {
  16. LOG.D (TAG, "ontouchevent.");
  17. Logutil.logaction (event, TAG);
  18. return super.ontouchevent (event);
  19. }
  20. }

The Logutil.logaction () function is the type of action used to print the Motionevent, as follows:

[Java]View PlainCopy 
  1. Public class Logutil {
  2. public static void Logaction (Motionevent event, final String tag) {
  3. int action = Event.getaction ();
  4. switch (action) {
  5. Case Motionevent.action_down:
  6. LOG.D (Tag, "Action down");
  7. Break ;
  8. Case Motionevent.action_cancel:
  9. LOG.D (Tag, "Action Cancel");
  10. Break ;
  11. Case MOTIONEVENT.ACTION_UP:
  12. LOG.D (Tag, "Action up");
  13. Break ;
  14. Case Motionevent.action_move:
  15. LOG.D (Tag, "Action move");
  16. Break ;
  17. Default:
  18. LOG.D (Tag, "Unknow action");
  19. }
  20. }
  21. }


The layout file Main.xml myview nested in mylayout with the following code:

[HTML]View PlainCopy 
  1. <view android:layout_width="fill_parent"
  2. android:layout_height="fill_parent"
  3. class= "com.example.AndroidTest.MyLayout" xmlns:android= "http://schemas.android.com/apk/res/ Android "
  4. android:id="@+id/view">
  5. <Com.example.AndroidTest.MyView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:text="Hello" />
  9. </View>

The code for Mainactivity is as follows:

[Java]View PlainCopy 
  1. Public class Mainactivity extends Activity {
  2. public static final String TAG = "Touchdemoactivity";
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate)
  5. {
  6. super.oncreate (savedinstancestate);
  7. Setcontentview (R.layout.main);
  8. }
  9. }


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 
    1. @Override
    2. Public Boolean ontouchevent (Motionevent event) {
    3. LOG.D (TAG, "ontouchevent.");
    4. Logutil.logaction (event, TAG);
    5. return false;
    6. }


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 
    1. @Override
    2. Public Boolean onintercepttouchevent (motionevent ev) {
    3. LOG.D (TAG, "onintercepttouchevent");
    4. return true;
    5. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.