Touch gesture distribution in Android

Source: Internet
Author: User

In touch distribution in Android, you can often see from ACTION_down->action_move->action_up, When we do not understand how it is distributed, it feels confused, so it is necessary to understand its overall distribution process, so it is necessary to keep a good record.

Event distribution functions

public void dispatchtouchevent (Motionevent ev).

public void onintercepttouchevent (Motionevent ev).

public void ontouchevent (Motionevent ev).

Overall touch distribution We need to understand its distribution order from these three distribution functions, where the onintercepttouchevent function is only unique in ViewGroup. We determine whether we need to continue distributing the change-over event based on the result of the return in each of the distributed functions.

Write a log where we need to test the touch distribution to study its results.

First we define two ViewGroup respectively extends LinearLayout and write log in dispatchtouchevent,onintercepttouchevent,ontouchevent respectively, as follows < Because it is a simple log write, it is only an example:

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 "); return super.dispatchtouchevent (EV);}
And then we went on to customize a Touchbutton extends button as well as rewrite the dispathtouchevent and Ontouchevent methods, and then write the two methods in the activity, we mainly through the log to analyze the distribution gesture, The approximate interface layout is:


1. In the activity parentlayout Touchbutton in the case of distribution by default:


You can see that the default is to press Activity-parentlayout-childlayout-touchbutton to distribute, and return true in Touchbutton. So we can see that the gesture distribution is to distribute the gesture from the activity, which, by default, is consumed in the Touchbutton, and in the Dispathtouchevent, it returns true anywhere in the move up. Then the gesture will not be distributed down directly within the control consumed.

2. When True is returned in the Acton_down in Onintercepttouchevent in Parentlayout:


If it is intercepted in the onintercepttouchevent that causes the event to continue to pass and cannot receive the move and up events that continue, the final time is still back to activity consumption.

3. When we return true in Action_move in Onintercepttouchevent:


Will distribute the cancle event to the Child and button and then pass the event to Ontouchevent and the child and button cannot receive the move and up events, in which case we most often need to In many cases, the distribution gesture is intercepted to move to achieve the sliding effect of the custom viewgroup.

Finally say two words:

We distribute the gesture according to Dispathtouchevent, and if the return is true in this method then the next gesture can only be consumed in this view or activity, whereas the last gesture will be consumed in the Touchbutton.

Returns true in Onintercepttouchevent's move then the upper view can only receive the Action_cancle event without any other events, eventually consumed in this viewgroup.


Touch gesture distribution in Android

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.