Android Touch gesture distribution and androidtouch gesture

Source: Internet
Author: User

Android Touch gesture distribution and androidtouch gesture

In the Touch distribution in android, we often see ACTION_DOWN-> ACTION_MOVE-> ACTION_UP. When we don't know how it is distributed, we always feel confused, therefore, it is necessary to understand its overall distribution process, so it is necessary to keep a good record.

Event Distribution Function

Public void dispatchTouchEvent (MotionEvent ev ).

Public void onInterceptTouchEvent (MotionEvent ev ).

Public void onTouchEvent (MotionEvent ev ).

We need to understand the distribution sequence of the entire Touch distribution from these three distribution functions. The onInterceptTouchEvent function is only a function exclusive to ViewGroup. Based on the return results in each distributed function, we can determine whether to continue to trigger and change the Touch event.

Write logs where we need to test the Touch distribution to study the results.

First, we define two viewgroups, extends LinearLayout, and write logs in dispatchTouchEvent, onInterceptTouchEvent, and onTouchEvent respectively. The following is an example for writing logs in a simple way:

public boolean dispatchTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.i(TAG, "--- dispatchTouchEvent -> action_down");break;case MotionEvent.ACTION_MOVE:Log.i(TAG, "--- dispatchTouchEvent -> action_move");break;case MotionEvent.ACTION_UP:Log.i(TAG, "--- dispatchTouchEvent -> action_up");break;case MotionEvent.ACTION_CANCEL:Log.i(TAG, "--- dispatchTouchEvent -> action_cancle");break;}return super.dispatchTouchEvent(ev);}
Then, we continue to customize the TouchButton extends Button and overwrite the dispathTouchEvent and onTouchEvent methods above, and then write these two methods in the Activity. We mainly analyze the distribution gesture through log, the general interface layout is:


1. In the event that all activities ParentLayout touchbuttons are distributed by default:


We can see that by default, the distribution is based on the Activity-ParentLayout-ChildLayout-TouchButton and return true in the TouchButton. Therefore, we can see that the gesture distribution is from the activity to the delivery gesture, by default, it is successfully consumed in the TouchButton. In the dispathTouchEvent, if true is returned at any place in the down move up, the gesture will not be distributed and will be consumed directly in the control.

2. When true is returned in ACTON_DOWN of onInterceptTouchEvent in ParentLayout:


If the onInterceptTouchEvent is intercepted, and the event cannot be passed on and can not receive the move and up events, the final time will return to activity consumption.

3. If we return true in ACTION_MOVE of onInterceptTouchEvent:


The cancle events are distributed to the child and button, and the events are passed to the ontouchevent. The child and button cannot receive the move and up events. In this case, we need, in many cases, the distribution gesture is intercepted to move to achieve the sliding effect of the custom ViewGroup.

The last two sentences are as follows:

We distribute gestures Based on dispathTouchEvent. If this method returns true, the following gestures can only be consumed in the current View or Activity, otherwise, the final gesture will be consumed in the TouchButton.

If true is returned in the onInterceptTouchEvent move, the upper-layer View can only receive the ACTION_CANCLE event, but will not be subject to any other event, which is eventually consumed in this ViewGroup.


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.