Android TouchEvent Event Learning

Source: Internet
Author: User

Study site: http://www.apkbus.com/forum.php?mod=viewthread&tid=44296

1:Android Touch event delivery mechanism parsing each View subclass in the Android system has the following three methods that are closely related to TouchEvent processing: 1)publicboolean dispatchtouchevent (motionevent ev) distribution touchevent 2)publicboolean onintercepttouchevent (motionevent ev) intercept touchevent 3)publicboolean ontouchevent (motionevent ev) processing touchevent

2. Delivery process

(1) The event is passed from Activity.dispatchtouchevent (), as long as it has not been stopped or intercepted, and is passed down (sub view) from the topmost view (ViewGroup). The child view can handle the event through Ontouchevent ().

(2) The event is passed by the parent view (ViewGroup) to the child View,viewgroup can intercept the event by Onintercepttouchevent () and stop it from passing down.

(3) If the event from the top down the process has not been stopped, and the bottom of the sub-view no consumption events, the event will be anti-yearning to pass, then the parent view (ViewGroup) can be consumed, if still not consumed, The Ontouchevent () function of the activity is finally reached.

(4) If the view does not consume the action_down, then other events will not be passed over.

(5) Ontouchlistener takes precedence over ontouchevent () to consume the event.

The above consumption means that the corresponding function return value is true.

Here's an example: 2 states defined

State 1: Touch event handled by Center
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical" > <dk.touch.MyLayout Android:id= "@+id/out"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:gravity= "Center"Android:background= "#ff345600" > <dk.touch.MyLayout Android:id= "@+id/middle"Android:layout_width= "200DP"Android:layout_height= "200DP"android:gravity= "Center"Android:background= "#ff885678" > <dk.touch.MyLayout Android:id= "@+id/center"Android:layout_width= "50DP"Android:layout_height= "50DP"Android:background= "#ff345678"android:focusable= "true"Android:focusableintouchmode= "true"android:clickable= "true" > </dk.touch.MyLayout> </dk.touch.MyLayout> </dk.touch.myla Yout></linearlayout>

Processing mechanism:

State 2: No events are handled

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="fill_parent"android:orientation="Vertical"> <dk.touch.MyLayout Android:id="@+id/out"Android:layout_width="fill_parent"Android:layout_height="fill_parent"android:gravity="Center"Android:background="#ff345600"> <dk.touch.MyLayout Android:id="@+id/middle"Android:layout_width="200DP"Android:layout_height="200DP"android:gravity="Center"Android:background="#ff885678"> <dk.touch.MyLayout Android:id="@+id/center"Android:layout_width="50DP"Android:layout_height="50DP"Android:background="#ff345678"> </dk.touch.MyLayout> </dk.touch.MyLayout> </dk.touch.mylayout></linearlayou T>

Processing mechanism:

Keep learning.

Android TouchEvent Event Learning

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.