Objective
With the development of science and technology, smartphones have become one of the essential "partners" of our contemporary people, comparable to the target girlfriend. Every day we repeat the operation of the mobile 点击 phone 滑动 , and the mobile phone with our operation to show us her wonderful.
...
That's the end of the crap.
See here, even if not as a mobile farm, you should know the importance of touch events on the phone (except for the classic keyboard machine).
What is touch events
As the name implies, it is the event that occurs after touching the phone screen. Please take out your mobile phone, click on a button on the screen (do not let go), move a distance, give up.
This process typically produces the following events:
- Click (Down) event
- Move Event
- Let go (UP) event
Android has encapsulated a touch event class for us MotionEvent , and the three processes above correspond to the MotionEvent types of events in the, MotionEvent.ACTION_DOWN MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP We can use this to implement different logic, that is, event distribution processing. The distribution of so-called touch events can actually be understood as MotionEvent the distribution process of events, that is, when one is MotionEvent produced, the system needs to pass the event to a specific view, and the process of passing it is the distribution process.
Event Three Musketeers
The distribution process for general events is accomplished by the Three Musketeers (methods) of the event.
/** * 剑客一:用于事件的分发 */public boolean dispathTouchEvent(MotionEvent ev)/** * 剑客二:在剑客一种被调用,用于事件的拦截 */ public boolean onInterceptTouchEvent(MotionEvent ev)/** * 剑客三:在剑客一种被调用,处理点击事件,true:消耗了当前事件 false:当前view无 * 法再次接收事件 */ public boolean onTouchEvent()(MotionEvent ev)
The relationship of the Musketeers as shown (taking the dispathtouchevent of activity as an example)
Analysis indicates:
1. Touch Event EV class received the action of the Click, will callback Onuserinteraction method, the general project we put some need to start touch the user needs to execute the task code here.
2. The next touch event EV is passed to the activity window binding root View rootView (View/viewgroup), if the root view also has child views, the event EV will be distributed one level at a level, if the EV is consumed in this process, the event ends up being distributed, otherwise entering the step 3.
3. All view layouts do not consume EV events, and the activity's Ontouchevent () method is called. The following is a specific complaint.
A brief analysis of Android interface
Before we go into specific cases, let's look at the Android interface architecture. If the mobile phone is a school, then every app in the cell phone is a unique landscape in the school, it is that they constitute the charm of the school. And each app is made up of one activity.
Or are you talking nonsense ...
As shown, we can see clearly that each Activity one contains an Window object. And window objects are usually PhoneWindow implemented by. PhoneWindowsets one DecorView to the root view of the entire application window. It divides the screen into two parts, one is Titleview and the other is the ContentView Contentview layout that you are familiar with. Contentview is an ID content FrameLayout , and the Activity_xx.xml layout we've been writing is set in such a framelayout.
DecorViewThe specific content to be displayed is presented PhoneWindow , and all of the view's listening events (click, swipe, and so on) are received by a name WindowManagerService (specifically a deep understanding of the Android Volume III), and the corresponding listener is tuned back and forth through the activity.
To make it easier for everyone to understand, let's write a little demo below
Run results
Small scene, see the truth Scene One
Let's write one of the simplest demos as follows
To run the program, click button and see the log output as follows:
Modify Dispathtouchevent, direct return false ;
Run the program and click button to see if the console has no output. A visible event is passed to the root view of the activity and is finished distributing. The following has been the scene and the specific exploration of the process.
Scene Two
If in the university, the school to the math teacher a task, the teacher said the task assigned to the female monitor, and the female monitor will this task to the handsome me, I have to do this task, and then to the female monitor, the female monitor felt that the completion of a good, praised the handsome me a few words (secretly love on), Then the task submitted to the teacher, the teacher saw the next also feel that the completion of a good, the task to submit to the school.
Based on the above scenario, we design a scene instance as follows
- Teacher--teacherviewgroup
- Female Monitor--monitressviewgroup
- Handsome me,--handsomemyview.
The layout hierarchy is as shown
Teacherviewgroup and Monitressviewgroup code as follows, rewriting the Three Musketeers method
Handsomemyview code below, view is no Swordsman 2 (method) onInterceptTouchEvent()
Click 帅气的我 to see log print as follows
There are two processes that can be seen in general events
- Delivery process: Teacher (teacherviewgroup)--Woman Monitor (Monitressviewgroup)--Handsome Me (Handsomemyview)
- Process: Handsome Me (Handsomemyview)--Women Monitor (Monitressviewgroup)--Teacher (Teacherviewgroup)
Process method of delivery: Swordsman 1 (dispatchtouchevent), Swordsman 2 (onintercepttouchevent)
Process method of Processing: Swordsman 3 (Ontouchevent)
To make it easier for everyone to understand, organize the view as follows:
From this we can see that the touch event EV will 相反 have the opportunity to consume this touch event EV in order of sub-view addition to the viewgroup sequence, which is the first opportunity to consume this touch event (the premise of consumption is that the touch point is within the view range of this sub-view). In short, the pass-through, consumption (processing) by the inward and outward.
In the previous event the careful classmates will find that their return values are boolean types, so what does true and false mean?
Here I first tell you the conclusion, and then in the validation of this conclusion:
dispatchTouchEvent()AndonInterceptTouchEvent()
- Returns true to indicate that the event was intercepted and not resumed;
- Returns false to indicate that the event is not intercepted and continues the next process;
onTouchEvent()
- Returns true to indicate that the event has been processed without being passed to the upper level view;
- Returns false to indicate that the event is handed over to the upper level view processing;
Initially, their default return values are false .
Intercept Onintercepttouchevent ()
Assuming that the female monitor secretly love me, I secretly help me to complete the task, this time the incident was the female monitor (Monitressviewgroup) onInterceptTouchEvent() method interception, that is MonitressViewGroup onInterceptTouchEvent() , the return of ture, log output as follows
The grooming view is as follows:
Similarly, it can be assumed that teachers are better, do not bear the trouble of students, their own treatment, the process is similar to the female monitor process.
Here, I think we have a relatively clear understanding of the distribution of events, interception. Next we look at the handling of the event.
Handling Ontouchevent ()
We finish the task is to report the completion of the results to the superior, that is, handsome I need to report to my dear female monitor results, the monitor to the teacher report results. Suppose I can not finish the task on time, do not report the result of the task to the female monitor, that is, the method of Handsomemyview onTouchEvent() returns True (the event is processed, do not return to the superior), at this time log output as follows, female monitor and teacher do not continue to deal with the incident
The grooming view is as follows:
Similarly, the female monitor and the teacher can also not report like their respective superiors, the process is similar 帅气的我 (Handsomemyview).
Ref
- Mastering the Android Touch System
- Android Elite Biography
...
Android Touch Event mechanism