Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Touch event transfer Mechanism in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to achieve the touch event transmission mechanism in Android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

one。 Type of touch event

Touch events correspond to MotionEvent classes, and there are three main types of events:

ACTION_DOWN: the user presses the action to indicate the beginning of a touch event.

ACTION_MOVE: move when pressed. The slightest movement is passed to the event.

ACTION_UP: the user's finger leaves the screen to indicate a touch event

Note: if the user only clicks, only the ACTION_DOWN and ACTION_UP events will be executed, not the ACTION_MOVE event. So it is necessary that ACTION_DOWN and ACTION_UP are events.

two。 The transmission phase of touch events

1. Distribution (Dispatch)

In the Android system, all touch events are distributed by the dispatchTouchEvent method. This method determines whether the event is consumed (return true) or continues to be distributed to subview processing (return super.dispatchTouchEvent). If the current view is ViewGroup or its subclass, onInterceptTouchEvent is called to determine whether to intercept.

@ Override public boolean dispatchTouchEvent (MotionEvent event) {return super.dispatchTouchEvent (event);}

two。 Interception (Intercept)

The intercepting InterceptTouchEvent of events exists only in ViewGroup and its subclasses, and this method does not exist in activity and View. This method determines whether the event is return true and handed over to its own OnToucEvent method for consumption, or continues to be passed to the child view (return super.InterceptTouchEvent or return false).

@ Override public boolean onInterceptTouchEvent (MotionEvent ev) {return super.onInterceptTouchEvent (ev);}

3. Consumption (Consume)

The consumption of the event is determined by the OnTouchEvent method, whether it is consumed (return true) or not handled (return false) and passed on to the OnTouchEvent method of the parent view for processing.

@ Override public boolean onTouchEvent (MotionEvent event) {return super.onTouchEvent (event);}

All classes that have the ability to pass events:

Activity: owns dispatchTouchEvent and OnTouchEvent

ViewGroup: owns dispatchTouchEvent, OnInterceptTouchEvent, OnTouchEvent

View: owns dispatchTouchEvent and OnTouchEvent

Third, the event delivery mechanism of View

3.1 dome

Although ViewGroup is a subclass of View, View refers to a subclass of View controls other than ViewGroup. First, an MyTextView inheritance TextView is defined to print the trigger of each event to change the flow of event delivery.

MyTextView class

Public class MyTextView extends TextView {private String tag = "MyTextView"; public MyTextView (Context context) {super (context);} public MyTextView (Context context, AttributeSet attrs) {super (context, attrs);} @ Override public boolean dispatchTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "dispatchTouchEvent ACTION_UP"); break; case MotionEvent.ACTION_MOVE: Log.i (tag, "dispatchTouchEvent ACTION_MOVE"); break Case MotionEvent.ACTION_DOWN: Log.i (tag, "dispatchTouchEvent ACTION_DOWN"); break;} return super.dispatchTouchEvent (event);} @ Override public boolean onTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "onTouchEvent ACTION_UP"); break; case MotionEvent.ACTION_MOVE: Log.i (tag, "onTouchEvent ACTION_MOVE"); break Case MotionEvent.ACTION_DOWN: Log.i (tag, "onTouchEvent ACTION_DOWN"); break;} return super.onTouchEvent (event);}}

Define a MainActivity to display the MyTextView and set up onClick and onTouch listeners at the same time. MainActivity class

Public class MainActivity extends AppCompatActivity implements View.OnClickListener,View.OnTouchListener {private MyTextView mMyTextView; private String tag = "MainActiviy"; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); mMyTextView = findViewById (R.id.text_view); / / Click to listen to mMyTextView.setOnClickListener (this); / / Touch Monitor mMyTextView.setOnTouchListener (this) } / / MyTextView click event @ Override public void onClick (View view) {switch (view.getId ()) {case R.id.text_view: Log.i (tag, "MyTextView onClick"); break;}} / / MyTextView touch event @ Override public boolean onTouch (View view, MotionEvent motionEvent) {switch (motionEvent.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "MyTextView onTouch ACTION_UP"); break Case MotionEvent.ACTION_MOVE: Log.i (tag, "MyTextView onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_DOWN: Log.i (tag, "MyTextView onTouch ACTION_DOWN"); break;} return false;} / / Activity   event distribution @ Override public boolean dispatchTouchEvent (MotionEvent ev) {switch (ev.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "dispatchTouchEvent ACTION_UP"); break Case MotionEvent.ACTION_MOVE: Log.i (tag, "dispatchTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_DOWN: Log.i (tag, "dispatchTouchEvent ACTION_DOWN"); break;} return super.dispatchTouchEvent (ev);} / / Activity event consumption @ Override public boolean onTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "onTouchEvent ACTION_UP"); break Case MotionEvent.ACTION_MOVE: Log.i (tag, "onTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_DOWN: Log.i (tag, "onTouchEvent ACTION_DOWN"); break;} return super.onTouchEvent (event);}}

3.2 print Log

After running, click the print log of Text View feedback

03-28 08VOV 05VOV 14.824 1219-1219/com.mvp.chenzhesheng.androidadvance I/MainActiviy: dispatchTouchEvent ACTION_DOWN

03-28 08VOV 05VOV 14.824 1219-1219/com.mvp.chenzhesheng.androidadvance I/MyTextView: dispatchTouchEvent ACTION_DOWN

03-28 08VOV 05VOV 14.824 1219-1219/com.mvp.chenzhesheng.androidadvance I/MainActiviy: MyTextView onTouch ACTION_DOWN

03-28 08VOV 05VOV 14.824 1219-1219/com.mvp.chenzhesheng.androidadvance I/MyTextView: onTouchEvent ACTION_DOWN

03-28 08VR 05purl 15.034 1219-1219/com.mvp.chenzhesheng.androidadvance I/MainActiviy: dispatchTouchEvent ACTION_UP

03-28 08VR 05purl 15.034 1219-1219/com.mvp.chenzhesheng.androidadvance I/MyTextView: dispatchTouchEvent ACTION_UP

03-28 08VR 05purl 15.034 1219-1219/com.mvp.chenzhesheng.androidadvance I/MainActiviy: MyTextView onTouch ACTION_UP

03-28 08VR 05purl 15.034 1219-1219/com.mvp.chenzhesheng.androidadvance I/MyTextView: onTouchEvent ACTION_UP

03-28 08VR 05purl 15.044 1219-1219/com.mvp.chenzhesheng.androidadvance I/MainActiviy: MyTextView onClick

There are three situations in the return values of dispatchTouchEvent and OnTouchEvent methods:

Return to true directly.

Return to false directly.

Returns the parent class method with the same name, super.dispatchTouchEvent or super.OnTouchEvent.

Because of having different return values, the event delivery process is also different. After constantly modifying the return value test, we finally get the flow chart of click events, and the delivery processes of ACTION_DOWN and ACTION_UP events are the same.

3.3 event delivery flowchart

A conclusion can be drawn from the above flow chart:

The touch event starts from dispatchTouchEvent, and the parent class method super of the same name is returned by default. The event will be passed from outside to inside according to the nested level (MainActivity to MyTextView). When it reaches the innermost View, it will be handled by View's OnTouchEvent method. When this method returns true, consumption will no longer be passed, and when false is returned, it will be passed from inside to outside, and handled by the outer OnTouchEvent.

If human interference returns true consumption during the outer layer to the inner layer, it will not continue to pass like the inner layer.

The event control order of View executes onTouch before onClick, and if onTouch returns true consumption, it will not be passed and the onClick method will not be executed.

IV. Event delivery mechanism of ViewGroup

4.1 dome

ViewGroup is the control container of View, which has three methods: dispatchTouchEvent, onInterceptTouchEvent, and onTouchEvent, and has one more onInterceptTouchEvent method than View. For a better look, we need to customize the MyRelativeLayout inheritance RelativeLayout.

MyRelativeLayout class

Public class MyRelativeLayout extends RelativeLayout {private final static String tag = "MyRelativeLayout"; public MyRelativeLayout (Context context) {super (context);} public MyRelativeLayout (Context context, AttributeSet attrs) {super (context, attrs);} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {switch (ev.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "dispatchTouchEvent ACTION_UP"); break; case MotionEvent.ACTION_MOVE: Log.i (tag, "dispatchTouchEvent ACTION_MOVE") Break; case MotionEvent.ACTION_DOWN: Log.i (tag, "dispatchTouchEvent ACTION_DOWN"); break;} return super.dispatchTouchEvent (ev);} @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {switch (ev.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "onInterceptTouchEvent ACTION_UP"); break; case MotionEvent.ACTION_MOVE: Log.i (tag, "onInterceptTouchEvent ACTION_MOVE"); break Case MotionEvent.ACTION_DOWN: Log.i (tag, "onInterceptTouchEvent ACTION_DOWN"); break;} return super.onInterceptTouchEvent (ev);} @ Override public boolean onTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_UP: Log.i (tag, "onTouchEvent ACTION_UP"); break; case MotionEvent.ACTION_MOVE: Log.i (tag, "onTouchEvent ACTION_MOVE"); break Case MotionEvent.ACTION_DOWN: Log.i (tag, "onTouchEvent ACTION_DOWN"); break;} return super.onTouchEvent (event);}}

Main_activity.xml file

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report