Usage and interpretation of ontouchevent and the transfer mechanism of touch events

Source: Internet
Author: User

Recently, a project has been associated with rewriting the view, and has high requirements on the control of the view touch event. In the past, I had a little understanding about the ontouchevent () method, however, I am not very familiar with the transfer mechanism in a view tree. Today I will write a test program to explore it.

First, let's outline the role of ontouchevent ().

SDK introduction:

Public Boolean ontouchevent (motionevent event)

Since: API Level 1

Implement this method to handle touch screen motion events.

Parameters
Event The motion event.
Returns
  • True if the event was handled, false otherwise.

We can clearly understand its meaning. We usually use a switch (event. getaction ())
{
Case motionevent. action_down:
...... // Control code
Break;
Case motionevent. action_move:
...... // Control code
Break;
Case motionevent. action_up:
...... // Control code
Break;
}
To control the response to the touch event.
It is worth noting that the function of the returned value the SDK means that the returned "true" means that you have taken over the "event" control and this "event" will not be passed to other objects, instead, they can "Digest" themselves.This can be verified by code. There is a key question I didn't mention, that is, where the "event" comes from and directly comes from the initial allocation of events (I personally think that there is a mechanism for controlling touch events in the Android system, he controlled the first event sending? Or is it from the transfer of other objects (parent or child nodes?So I did an experiment. Here I will tell you the result first, The event is accepted by the top-level view (child node) for the first time, and the touch event is passed to the parent node layer by layer (unless a node has digested the event ).

Paste the following code: Layout file: Main. xml
<?xml version="1.0" encoding="utf-8"?><com.yp.touchtest.Layout_one xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <com.yp.touchtest.Layout_two        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <com.yp.touchtest.View_one            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </com.yp.touchtest.View_one>    </com.yp.touchtest.Layout_two></com.yp.touchtest.Layout_one>

Among them, Com. yp. touchtest. layout_one and COM. yp. touchtest. layout_two are two rewritten linearlayout, and COM. yp. touchtest. view_one overwrites the view.
Avtivity class: touchtestactivity. Class

public class TouchTestActivity extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}}

Layout_one layout_two view_one only overrides the ontouchevent (motionevent event) method.
First of all, the touch gestures used in all cases are "pressed" --> "slide" --> "lifted" in three steps.
Scenario 1:The three objects do not digest the touch event, that is, the ontouchevent of the three objects returns "false" regardless of the situation ".
View_one layout_one and layout_two are both rewritten
@Overridepublic boolean onTouchEvent(MotionEvent event){String name ="onTouchEvent  ";switch (event.getAction()){case MotionEvent.ACTION_DOWN:System.out.println(Tag+name+"ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:System.out.println(Tag+name+"ACTION_MOVE");break;case MotionEvent.ACTION_UP:System.out.println(Tag+name+"ACTION_UP");break;}return false;}

System. out. println (TAG + name + "action_down"); in the print statement, the tag is the class name of the class (for example, "view_one"), and the name is the method name, at this time, the three classes are the same as "ontouchevent ".
The result after running is 04-24 17:08:37. 296: I/system. Out (23225): view_one: ontouchevent action_down.
04-24 17:08:37. 296: I/system. Out (23225): layout_two: ontouchevent action_down
04-24 17:08:37. 296: I/system. Out (23225): layout_one: ontouchevent action_down

Analysis: we can clearly see that the transfer order of touch events is subnode --> parent node. Surprisingly, none of the three objects received the move and up events. For the reason, you can look down and find out the answer.
Scenario 2:Based on the case 1 code, return "true" to the action_down event in the view_out class ".

case MotionEvent.ACTION_DOWN:System.out.println(Tag+name+"ACTION_DOWN");return true;

The result after running is 04-24 17:18:17. 921: I/system. Out (23323): view_one: ontouchevent action_down.
04-24 17:18:17. 944: I/system. Out (23323): view_one: ontouchevent action_move
04-24 17:18:18. 0 30: I/system. Out (23323): view_one: ontouchevent action_move
...... Many action_move
04-24 17:18:18. 061: I/system. Out (23323): view_one: ontouchevent action_move
04-24 17:18:18. 077: I/system. Out (23323): view_one: ontouchevent action_move
04-24 17:18:18. 108: I/system. Out (23323): view_one: ontouchevent action_up

Analysis: True is returned in action_down, but false is returned in action_down. What is surprising is that
All three action_down action_move action_up events are processed by view_one. In combination with scenario 1, we can know that as long as an object takes over (handled) The action_down event of the "event", the event will be processed and "digested" together with action_moveaction_up.
Case 3:The Code also gives case 1. Return "true" to the action_move event of layout_two"
The result after running is 04-24 18:21:41. 249: I/system. Out (23605): view_one: ontouchevent action_down.
04-24 18:21:41. 249: I/system. Out (23605): layout_two: ontouchevent action_down
04-24 18:21:41. 273: I/system. Out (23605): layout_two: ontouchevent action_move
04-24 18:21:41. 303: I/system. Out (23605): layout_two: ontouchevent action_move
...... Many action_move operations are omitted.
04-24 18:21:41. 405: I/system. Out (23605): layout_two: ontouchevent action_move
04-24 18:21:41. 444: I/system. Out (23605): layout_two: ontouchevent action_up

Analysis: this situation further confirms the conclusion of situation 2 analysis. In this case, layout_one processes and "Digests" the three events and does not pass them to his parent node.
Summary:Override the ontouchevent () method to process the user's touch events.The Touch event is transmitted from the child node to its parent node until a node returns true.Action_downAction_moveAction_up can be viewed as a whole (not actually ).Returned during action_down Processing
If it is set to true, all three events are processed and "digested" without being passed to its parent node.


In the future, I will conduct a write EXPERIMENT ON THE onintercepttouchevent (motionevent eV) method that is more relevant to ontouchevent.

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.