Detailed process of Android processing touch

Source: Internet
Author: User

Most of the interactions between users and applications are completed through touch events. Therefore, it is necessary to have an understanding of the internal logic of event processing. Only in this way can events be written and used freely. Start now.

Statement:

1. The full name of WMS in this article is: windowmanagerservier
2. The specific view mentioned in this Article. If there is no special description, all the views, such as textview and button, represent non-viewgroup classes.

Android's processing logic for touch events is as follows: Upload events from top to bottom, and then process the time logic from bottom to top. We can also understand the fact that when you touch the screen, first, upload the event at the first layer, and then upload the event to a specific view, such as textview and button. Then, process the event. Upload data by layer. However

The return value of onintercepttouchevent (motionevent eV) is different. The processing logic of different values is returned for detailed analysis.

A touch event is certainly triggered by the following actions:

Action_down-> action_move... -> action_move-> action_up, there must be a down and up event. According to the actual situation, it must also be in the move action, because the human finger is in the process of clicking, it will certainly move in a certain situation, but it just doesn't feel it, but the program knows, but the program understands your move and does not process it.

First, let's talk about the general process of the event: the event receiving layer (bottom layer: hardware and software, generally do not need to know) ----> Windows Management System windowmangerservicer (WMS) -----> because all windows are created by him, all WMS know who is in the current activity window, and WMS hand over the event to the current activity window --------> get the event in the current activity window, call the dispatchtouchevent of the viewroot class and give the root view ---> root view of the current activity window the dispatchtouchevent event to the specific view. The following is a detailed discussion.

1. For a specific view (non-viewgroup), such as textview, this situation does not exist at all, because each window must have a rootview and viewgroup.

2. For viewgroup, such as linearlayout.

  • If the event is down, start the following logic

  • (-- Recursive start point-) First, call the dispatchtouchevent method of viegroup. If it is a down event, clear the object that was last processed for this event (cache for processing move and other events) mmotiontarget = NULL;

  • Call the onintercepttouchevent method. This method only exists in the viewgroup class and does not have a specific view. The function of this method is to determine whether to intercept the message. If true is returned, the message transmission ends, call the ontouchevent method of the view object. If the returned value is false, it indicates that the view has no consumption event. Continue.

  • Because a touch event is a window coordinate value, you need to convert the coordinate value to the view's own coordinate system.

  • After the conversion, use the for loop traversal. All the child views of the view read the coordinate system of the Child view, that is, the size occupied by the Child view. It is a rect object, which is located at the top, bottom, right, and right, after obtaining this value, determine whether the clicked coordinates are included in the current subview Based on the converted coordinates. If not, start the next subview directly.

  • If the coordinates are included in the sub-view, the dispatchtouchevent of the sub-view is called. If the sub-view is still of the viewgroup type, recursive calls start from the position marked above (-- Recursive start point. For a specific view, call the dispatchtouchevent of the specific view. This method is relatively simple. First, determine whether to set the value through settoucheventlistener. If yes, call the ontouch method, if the method returns true, it will be returned directly. The ontouchevent method of the view is not called. If false is returned, the ontouchevent method of the view is called. The method is returned as the response of dispatchtouchevent.

  • After the dispatchtouchevent processing of a specific view is complete, if the returned value of the dispatchtouchevent of the child view is true, the parent view of the view will save the view object to mmotiontarget and end to this down event, if the returned value is false, continue the For Loop (I can launch the for loop at this time, because it feels useless, do you want to worry about overwriting the view) and start the next sub-View

  • (-- Recursive end point-) after for (because the process is synchronous, no other events are sent during execution), determine whether mmotiontarget is empty, if it is null, the target sub-view is not found. Therefore, the super of the current view (which must be the viewgroup object and the view object of the loop body) is called. the dispatchtouchevent method is a view-based class. The implementation is the same as the view processing logic. First, determine whether to set the value through settoucheventlistener. If yes, call the ontouch method, if the method returns true, it will be returned directly. The ontouchevent method of the view is not called. If false is returned, the ontouchevent method of the view is called. And treat this method
    The return of the dispatchtouchevent is returned to the for loop of the dispatchtouchevent of the viewgroup (the end of a recursive call). This indicates that if all the child views do not consume the event, the view consumes the event, regardless of whether the onintercepttouchevent returns true or false. This event is also ended.

  • At this point, a down event is processed and WMS is notified after the event is processed. At this time, WMS starts to dispatch the next event.

  • If the event is a move or up event, first determine whether the mmotiontarget obtained by the down processing logic is null, that is, whether the child view of the received event is found in the down processing. If it is blank, the target sub-view is not found. Therefore, the super of the current view (which must be a viewgroup object and the view object of the loop body) is called. the dispatchtouchevent method is a view-based class. The implementation is the same as the view processing logic. First, determine whether to set the value through settoucheventlistener. If yes, call the ontouch method, if the method returns true, it will be returned directly. The ontouchevent method of the view is not called. If false is returned, the ontouchevent method of the view is called. And treat this method
    The return of the dispatchtouchevent is returned to the for loop of the dispatchtouchevent of the viewgroup (the end of a recursive call). This indicates that if all the child views do not consume the event, the view consumes the event, regardless of whether the onintercepttouchevent returns true or false. End this event at the same time
  • If it is not blank, the direct parent class of mmotiontarget only takes the dispatchtouchevent event. However, Grandpa and elders of mmotiontarget will also take the dispatchtouchevent and onintercepttouchevent events, which are not clear yet, thank you for your understanding. This move and up event will continue to be processed by the sub-view. We can think of this logic because the same event should be processed by the same view, rather than a down event, it is a view processing, and a move or up event is a view processing. Therefore, if it is not empty, the dispatchtouchevent of the mmotiontarget will be processed directly.

  • At the same time, we know that ontoucheventlistener can be registered in the activity, so when will it be executed? The execution time is when the view that consumes the event is not found in the view, it is handed to the acitivity for processing.

At this point, the analysis of the event processing is over, and it is time to get off work. We will start to work next week. PM has given the task !!, You have time to learn the source code !!!!

Summary:

  • If the onintercepttouchevent of the parent view returns true, the Child view will never receive the touch event, and The onclick event of the Child view will not be processed, because the onclick event is called according to the condition in the ontouch event of the view, if the ontouchevent method of the view is re-viewed, the super. ontouchevent. the onclick event will not be processed.
  • If ontoucheventlistener is set for a view and onclicklistener is set, and the ontouch method of ontouchlistener returns true, The onclick event does not go away because the ontouchevent method is not called at this time, when the system calls the onclick event to capture the up event in the ontouchevent,

Http://www.eoeandroid.com/thread-200133-1-1.html

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.