Android Development event processing (Xiang tan), android

Source: Internet
Author: User

Android Development event processing (Xiang tan), android

When you click the screen, the first event you get is the outermost ViewRoot-> ViewGroup .... In this way, the ViewRoot directly ignores it. Let's take a look at the ViewGroup. We know that the outermost layer of the layout file is wrapped in five la S, the five la s are subclasses of ViewGroup. Therefore, ViewGroup (ViewRoot is directly ignored here) is the first method to trigger the event after ViewGroup obtains the event. dospatchTouchEvent (ev) what have you done in this method to distribute events? We came to the source code:

@ Overridepublic boolean dispatchTouchEvent (MotionEvent ev ){... omit // record the coordinates of the currently clicked touch event, and get a rectangle used to search for the final int action = ev. getAction (); final float xf = ev. getX (); final float yf = ev. getY (); final float scrolledXFloat = xf + mScrollX; final float scrolledYFloat = yf + mScrollY; final Rect frame = mTempRect; // if (action = MotionEvent. ACTION_DOWN ){... omitted // No event interception processing if ( DisallowIntercept |! OnInterceptTouchEvent (ev) {// re-determine that the event is DOWN (just to reassure yourself) ev. setAction (MotionEvent. ACTION_DOWN);... omitted}

Undoubtedly, the click event is an ACTION_DOWN event at the beginning. Of course, Google engineers still trigger the first method (dispatchTouchEvent () twice to determine whether the click event is ACTION_DOWN, therefore, once an event occurs, the dispatchTouchEvent () method is executed. In this method, the first thing is to record the coordinates of the currently clicked touch events, at the same time, a rectangle is obtained to search for the range of coordinates of the locked touch event, and then judge whether the event is DOWN.

// Whether the event is triggered. if (action = MotionEvent. ACTION_DOWN) {... omitted // No event blocking is performed. if (disallowIntercept |! OnInterceptTouchEvent (ev) {// call the onInterceptTouchEvent () method // re-determine that the event is DOWN (just to reassure yourself) ev. setAction (MotionEvent. ACTION_DOWN );... omitted }}

If it is an ACTION_DOWN event, the onInterceptTouchEvent (ev) interrupt event method is called in the current dispatchTouchEvent () method.

public boolean onInterceptTouchEvent(MotionEvent ev) {    return false;}

If the onInterceptTouchEvent (ev) method returns false (false by default), it means that no interruption event is performed in the current ViewGroup class, then the onTouchEvent () of the current class () the event handling method will not be executed (but will be handed over to the subview for resolution ).

The onInterceptTouchEvent (ev) method returns false (to the sub-View for resolution, then to the sub-View for resolution, all the sub-views will be obtained): if the condition is met, continue to execute the following code, record the coordinates of the currently clicked touch event, and obtain all the child views in the ViewGroup class, store them in the array, and obtain the total number of child views.

// We know that we want to dispatch events and find the children who can handle the events for (int I = count-1; I> = 0; I --) {final View child = children [I]; if (child. mViewFlags & VISIBILITY_MASK) = VISIBLE | child. getAnimation ()! = Null) {child. getHitRect (frame); if (frame. contains (scrolledXInt, scrolledYInt) {final float xc = scrolledXFloat-child. mLeft; final float yc = scrolledYFloat-child. mTop; ev. setLocation (xc, yc); child. mPrivateFlags & = ~ CANCEL_NEXT_UP_EVENT; if (child. dispatchTouchEvent (ev) {// event processing, now we have the target mMotionTarget = child; return true ;}}}}

Loop traversal stores all the sub-View arrays in the current ViewGroup. loop through the value of variable I to the sub-View of the corresponding badge, first determine whether the Sub-View can be clicked.

If a subview can be clicked, it indicates that it may be clicked. If a subview cannot be clicked, the DOWN event will not happen. Therefore, since it is possible to be clicked, you have to use the sub-View to obtain the rectangle (search for the range of coordinates of the locked touch event). If the rectangle contains coordinates of the currently clicked touch event, then let's judge again. The sub-View calls the dispatcheTouchEvent (ev) method. Note: If the sub-View is a sub-View of the ViewGroup class, the sub-View calls dispatcheTouchEvent (ev) the method is the dispatcheTouchEvent (ev) method, so the execution process is the same. When you call the dispatcheTouchEvent (ev) method once, the design is simple as long as you are a ViewGroup class or its subclass, then I keep searching for the sub-View of the sub-View or even the sub-View of the sub-View. In this way, I find the last sub-View and the sub-View is a View instead of a ViewGroup, view does not call the dispatcheTouchEvent (ev) method of ViewGroup, but calls dispatcheTouchEvent (ev) in View, and dispatcheTouchEvent (ev) in View) whether the onTouchListener object, OnTouchListener, is set in the method. onTouch (this, event) returns false or true.

// View class dispatchTouchEvent (ev): public boolean dispatchTouchEvent (MotionEvent event) {if (! OnFilterTouchEventForSecurity (event) {return false;} if (mOnTouchListener! = Null & (mViewFlags & ENABLED_MASK) = ENABLED & mOnTouchListener. onTouch (this, event) {return true;} return onTouchEvent (event );}

If it is true, it indicates that this sub... If the View wants this event, the dispatcheTouchEvent (ev) method in the View returns true as a whole. In this way, the subview in the dispatcheTouchEvent (ev) method in the ViewGroup calls dispatcheTouchEvent (ev) the obtained value is true. In this way, the following code will be executed and the sub-sub... View is assigned as the event target, so that all the events are handled by sub-users... View.

If (child. dispatchTouchEvent (ev) {// event processing, now we have the target mMotionTarget = child; return true ;}

If OnTouchListener. onTouch (this, event) returns false, I am not afraid, because I have to execute the onTouchEvent (event) method, if the child... The onTouchEvent (event) method in View returns true, which also indicates that this event is a sub-project... If the View is ready, the dispatcheTouchEvent (ev) method in the View returns true as a whole. In this way, the subview in the dispatcheTouchEvent (ev) method in the ViewGroup calls the dispatcheTouchEvent (ev) method) the obtained value is true. In this way, the following code will be executed and the sub-sub... View is assigned as the event target, so that all the events are handled by sub-users... View.

If (! DisallowIntercept & onInterceptTouchEvent (ev) {final float xc = scrolledXFloat-(float) target. mLeft; final float yc = random-(float) target. mTop; mPrivateFlags ~ CANCEL_NEXT_UP_EVENT; ev. setAction (MotionEvent. ACTION_CANCEL); ev. setLocation (xc, yc); if (! Target. dispatchTouchEvent (ev) {// target didn't handle ACTION_CANCEL. not much we can do // but they shoshould have .} // clear the target mMotionTarget = null; // Don't dispatch this event to our own view, because we already // saw it when intercepting; we just want to give the following // event to the normal onTouchEvent (). return true ;}... omitting return target. dispatchTouchEvent (ev); // all events are handed over to the subview for processing.

If false is returned after the code in the dispatcheTouchEvent () method in the View is executed, it indicates that the sub-project... View does not want this event, so this event is returned to its parent View, so that we come to the sub View in the dispatcheTouchEvent (ev) method of ViewGroup to call dispatcheTouchEvent (ev) the obtained value is false. In this case, the event target is not assigned a value and is null.

Then, we came to the ViewGroup class again. Since the event target (mMotionTarget) is not assigned a value and is empty, the value assigned to the target is null. Since the target is empty, this satisfies the if (target = null) Statement, executes the method, and finally calls the dispatchTouchEvent (ev) of the parent class, which is processed by its parent View.

If (target = null) {// we have no target (the child does not handle these events), which means we can handle ev by ourselves. setLocation (xf, yf); if (mPrivateFlags & CANCEL_NEXT_UP_EVENT )! = 0) {ev. setAction (MotionEvent. ACTION_CANCEL); mPrivateFlags & = ~ CANCEL_NEXT_UP_EVENT;} return super. dispatchTouchEvent (ev); // all events are handled by the parent View}

If true is returned as soon as onInterceptTouchEvent () (the overwriting of this method is performed in the parent View), it means that the parent View intercepts the event, the code will directly come here as it wants these events:

If (target = null) {// we have no target (the child does not handle these events), which means we can handle ev by ourselves. setLocation (xf, yf); if (mPrivateFlags & CANCEL_NEXT_UP_EVENT )! = 0) {ev. setAction (MotionEvent. ACTION_CANCEL); mPrivateFlags & = ~ CANCEL_NEXT_UP_EVENT;} return super. dispatchTouchEvent (ev); // all events are handled by the parent View}

In addition, we attached two images to demonstrate the above theoretical knowledge so that you can better master the event processing!

Figure 1:

Figure 2:

So far, the event processing 2.3 source code analysis I will talk about this, thank you for browsing to the end, if you think I write well, please give me a good review, thank you!

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.