Research and analysis of Android touch message passing mechanism

Source: Internet
Author: User

In Android, the message delivery control is mainly through two methods to work together to the user's touch message distribution, the following to see these two methods;

    1. Onintercepttouchevent: This method is defined in ViewGroup, as the name implies, this method is used for ViewGroup interception (intercept) touch messages;
    2. Ontouchevent: This method is defined in the view to handle the user's touch events;

The definitions of the two methods are as follows:

 Public Boolean onintercepttouchevent (motionevent ev);  Public boolean ontouchevent (Motionevent event);

One common denominator of these two methods is that there is a parameter motionevent to get specific information about touch events (such as press, move, and so on) and function return values (for controlling touch handling in different situations);

Simply say Motionevent, it contains the type of user touch message, several commonly used types of touch messages are: Action_down,action_move,action_up,action_cancel, which means touch pressed, moved, end of the state These types of messages are not concurrency-generated, but are,down->move->up/cancel; as ordered by the order in which the touch occurs.

Below is an example of a simple viewgroup and view that is used to track the execution of Ontouchevent and onintercepttouchevent, respectively;

Llinearlayout

 Public classLlinearlayoutextendsLinearLayout { PublicLlinearlayout (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicLlinearlayout (Context context) {Super(context); }    Private floatlasty; @Override Public Booleanonintercepttouchevent (motionevent ev) {String tag= "Onintercepttouchevent"; LOG.W (Tag,"" +Super. Onintercepttouchevent (EV)); Switch(Ev.getaction ()) { CaseMOTIONEVENT.ACTION_DOWN:LOG.W (Tag,"Action_down"); Lasty=Ev.getx ();  Break;  CaseMOTIONEVENT.ACTION_MOVE:LOG.W (Tag,"Action_move"); if(Ev.getx ()-lasty > 20) {LOG.W (tag,"Action_move>20"); return true; }             Break;  Casemotionevent.action_up: CaseMOTIONEVENT.ACTION_CANCEL:LOG.W (Tag,"Action_up");  Break; }        return false; } @Override Public Booleanontouchevent (Motionevent event) {LOG.W ("Ontouchevent", "" "+Super. Ontouchevent (event)); return false; }}

LView

 Public classLViewextendsImageView { PublicLView (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicLView (Context context) {Super(context); } @Override Public Booleanontouchevent (Motionevent event) {String tag= "Lview.ontouchevent"; LOG.E (Tag,"" +Super. Ontouchevent (event)); Switch(Event.getaction ()) { CaseMOTIONEVENT.ACTION_DOWN:LOG.E (Tag,"Action_down");  Break;  CaseMOTIONEVENT.ACTION_MOVE:LOG.E (Tag,"Action_move");  Break;  Casemotionevent.action_up: CaseMOTIONEVENT.ACTION_CANCEL:LOG.E (Tag,"Action_up");  Break; }        return false; }}

Add LView to Llinearlayout in the XML file and control it through a series of changes;

Step1:

Through the log information above, it can be seen that the touch event was first intercepted by Llinearlayout (Onintercepttouchevent), in this demo, the method return value is False, and then the touch event to LView, LView's ontouchevent responds to this touch event and also returns false, and the touch event is then passed to Llinearlayout's ontouchevent for processing;

From the above we can simply see that the touch event was first intercepted by Llinearlayout and then passed to Lview,lview to execute ontouchevent processing logic And then llinearlayout to execute his own ontouchevent processing logic;@1

Step2: Change the Onintercepttouchevent method return value above Llinearlayout to True to run the program again;

Compared to the above log information, you can see the message is not passed to the LView; here, a small conclusion should be drawn;

The Onintercepttouchevent return value inside the ViewGroup returns true to intercept the touch event and no longer passes the touch event to the child view inside the ViewGroup;

Step3: Go back to Step1, change the ontouchevent return value in LView to True, run the program again, swipe your finger from the screen to the left;

From the log information, it can be seen that when the Ontouchevent return value of LView is true, LView's touch event is passed from down to move and then to up; The whole process was first received by Llinearlayout's onintercepttouchevent, where the touch event was not intercepted, but the touch event was passed to the child view; the attentive friend might find that here, Did not implement the Ontouchevent method to Llinearlayout, why? In fact, the Ontouchevent event of LView returns True, indicating that processing consumes this event and no longer passes on. Also does not carry on to the Llinearlayout the Ontouchevent method;

Conclusion:the Ontouchevent return value of the view indicates whether the touch event will continue to be passed, for example, if true, the touch pattern will be passed from down to move to up (this is not an exact statement, This means that the entire Ontouchevent method return value is true, and no fragmentation is returned, such as when the move pattern returns false);

STEP4: Continue step3, run the program, swipe your finger from the screen to the right;

In Llinearlayout's onintercepttouchevent, if the move message moves to the right by a distance greater than 20, the touch event is blocked, so that after the event is intercepted, it will no longer resemble step3. Move message will be lview in the ontouchevent, but is interrupted, the touch of the form to action_up that is the finger lift (in fact, this is not true here, it is accurate to say that the touch of the form into a action_cancel, Because I handle up and CANCEL as a class, so for example, the printed log information is action_up, in fact it is essentially action_cancel); and then because Action_move is intercepted, So the llinearlayout ontoucheevent is constantly being called when the fingers move.

End of summary:

The onintercepttouchevent default value in ViewGroup is false, only if the return value is false, the touch event is passed to the child view and then to the ontouchevent of the child view, and the return value is true. The user touch event is blocked, and the child view does not capture the touch event;

The Ontouchevent method of the view, when the return value is true, the event will continue to pass down, passed from Action_down to action_move to action_up, and conversely if the return value is false, It will only capture the action_down morphology of motionevent;

Research and analysis of Android touch message passing mechanism

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.