Android Touch Message delivery mechanism

Source: Internet
Author: User

The job of the former is to add a touch effect (a view) to the Desktop (launcher launcher, which is actually an activity), and this special effect is available every time you touch it, no matter what you do on the desktop, it will show the effects! have been groping to do not know how to start, and then slowly after the experiment to know that there is an Android touch message delivery mechanism. It's really slow to find out for yourself, but it would be more effective if you knew the mechanism earlier.

Each touch of the user (onclick,onlongclick,onscroll,etc.) is composed of a action_down+n action_move+1 action_up, the user touch must first have a action_down response, The end of the user's touch will inevitably be a action_up. (Of course, if intercepted on the way, it may not have!) So how does view distribute messages and intercept messages?

The 1.View and its subclasses will have two methods:

public boolean dispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent

public boolean ontouchevent (motionevent ev) This method is used to process touchevent

2. The special view subclass ViewGroup also has a method:

public boolean onintercepttouchevent (motionevent ev) This method is used to intercept touchevent

3. Distribution

Dispatchtouchevent receives a touch, it passes the message to the outermost view and then distributes it to the view of the child layer

4. Interception:

onintercepttouchevent Intercept returns true means to intercept the message and not to Child view is passed (the child view here is not an inheritance relationship, but an inclusive relationship). Returning false indicates that the message is not intercepted and can continue to the next level of view delivery message, the child view will be able to dispatchtouchevent received a touch message redistribution message

5. Message processing:

The ontouchevent handles the event, intercepts the message, or the last view that receives the message calls this method to handle the event, and if it returns true, it is correctly received and processed. Returning false means that it is not processed and will be passed to the parent view ( where the parent view is not an inheritance relationship, but an inclusive relationship )

6. Examples and manuscripts


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.