Mastering the Android Touch event mechanism

Source: Internet
Author: User

Mastering the Android Touch event mechanism

Davesmith

@devunwired

Points covered

L Touch System Overview

L framwork layer of touch events

• Handling of touch events

L System-provided touchhandlers

L System-provided gesturehandlers

How does the Android system handle touch events?

• Each touch event is encapsulated as a Motionevent object

L This object describes the user's current action

–action_down

–action_up

–action_move

–action_pointer_down

–action_pointer_up

–action_cancel

L Event Metadata includes

– The location of the touch

– Number of touch points (or the number of fingers touching the root)

– The time when the event occurred

L A "gesture (gesture)" begins with Action_down and ends with ACTION_UP.

The event starts with the activity's Dispatchtouchevent () function (it is known that this is a distribution of events)

L events are passed between views in turn

– Parent view (viewgroups) passes an event to his child view

– Events can be intercepted at any time during delivery

L Events flow through the "chain of delivery" until they return to activity, unless they are consumed during the flow.

All events that are not consumed will eventually return to the activity's ontouchevent () and end the pass.

The externally defined Ontouchlistener can intercept events in any view/viewgroup, and the implementation of the interface is optional, that is, the interface can be implemented or not implemented.

L Activity.dispatchtouchevent ()

– Always the first one to be called.

– Send events to the root view of Windows (Root views)

–ontouchevent ()

L If there is no view consumption event, the function will eventually be called

L is always the last function to be called.

L View.dispatchtouchevent ()

– If View.ontouchlistener is registered, the event is first passed to it for processing, called:

L View.onTouchListener.onTouch ()

– If not consumed, the view will process itself, calling:

L View.ontouchevent ()

L Viewgroup.dispatchtouchevent ()

–onintercepttouchevent ()

L Check if you want to intercept the event and no longer pass it to the child view

L but pass the Action_cancel event to Activechild

L Once you consume all subsequent events, the function returns True

– For each sub-view, follow the order in which they were added, as follows:

L If the touch event is meaningful (that is, within the view), call the Dispatchtouchevent () of the child view;

L Pass an event to the next view if the event is not processed.

– If no child view handles the event, it is given to ViewGroup's Ontouchlistener (if defined) processing, called:

L Ontouchlistener.ontouch ()

– If no ontouchlistener is defined or ontouchlistener is not processed, call:

L Ontouchevent ()

n the intercepted event skips the processing steps of the child view.

n Some examples

Delivery process without view handling event

Delivery process with view handling events

ViewGroup the delivery process when intercepting events

Touch Event Handling

• How to handle touch events

– Ontouchevent () function of the subclass replication (override) Parent class

– Implement Ontouchlistener interface

L Consumption Events

–action_down returns true to indicate that you are interested in the event

L return True even if you are not interested in Action_down

– For other events, return true to interrupt the continuation of the event delivery

L Viewconfiguration Some useful methods

–getscaledtouchslop ()

Represents a distance that is considered a move event only after the distance of the hand is ≥ the distance.

–getscaledminimumflingvelocity ()

Represents a speed that is considered to be a fling event when the data of the hand slips ≥ the speed

–getlongpresstimeout ()

Represents a period of time when exceeding the click event ≥ that time will not be considered a long-press event

– Displays device density values for each device

L Trigger "flow" of touch events

– Call Dispatchtouchevent ()

– Do not call Ontouchevent directly ()

Similarly, call Requestlayout () instead of onlayout (), call invalidate () instead of OnDraw (), call PerformClick () rather than onclick () (translator note);

L Intercept Touch Events (for ViewGroup)

– Onintercepttouchevent () function of the subclass replication (override) Parent class

– return True when you want to intercept an event

All events for the current operation (Currentgesture) will go directly to your ontouchevent ()

Onintercepttouchevent () will no longer be called for all of these events

– All current targets (view and activity) are connected to the Action_cancel event

L call Super.ontouchevent () whenever possible

–view.ontouchevent () Does a lot of work to maintain the status (pressed, checked, etc.), and if you handle these events yourself, you will lose a lot of processing work.

• Boundary check of Action_move events (Protect Action_move with slop checks)

– Because the finger operation is not fine after all (fingers arefat and twitchy)

L always remember to deal with Action_cancel

– Some container Class View (for example, ScrollView) intercepts events, so you need to process action_cancel to reset the state

– Remember that there will be no more events after you have finished processing the action_cancel.

Multi-Touch

L Motionevent.getpointercount ()

-Returns how many touch points are on the screen at the current moment

L use Action_pointer_down and action_pointer_up to monitor how many "two touch points" are available.

–motionevent.action_pointer_down: Triggered when a point on the screen has been held down and then pressed at another point.

–MOTIONEVENT.ACTION_POINTER_UP: When there are multiple points on the screen being held down, the trigger is released when one of the points is loosened (that is, when the last point is not released).

–moeonevent.getaceonmasked ()

–moeonevent.getaceonindex ()

Batch Processing

l to improve efficiency, action_move events can be handled uniformly in a single motionevent

The L standard function always returns the most recent (current) event.

such as: GetX (), GetY (), Geteventtime () returns only the x-coordinate, y-coordinate, and event time of the most recent (current) event.

L events occurring between Action_move and the last event can be obtained in the historical Method (historical)

–gethistoricalx (), Gethistoricaly (), Gethistoricaleventtime ()

–gethistoricalsize () returns the number of events for a unified batch

System-encapsulated Touch Event processing Object

L commonly used are:

? Onclicklistener

? Onlongclicklistener

? Ontouchlistener

-A listener can monitor a single event

-a listene can consume one or more events

L onscrolllistener/view.onscrollchanged ()

-function called when a view slide with sliding function

• For more complex touch events, like the following objects

Gesturedetector

–ondown ()

–onsingletapup ()

–ondoubletap ()

–onlongpress ()

–onscroll ()

–onfling ()

Scalegesturedetector

–onscalebegin ()

–onscale (),

–onscaleend ()

The above operations are essentially implemented by Ontouchlistener () and Ontouchevent ().

Disadvantages

– Consumption of up events without exposing interfaces to handle the cancel event

Delegate Touch

Android allows you to use Touchdelegate to extend the response area of the touch event.

How to use:

ViewGroup parent;//Parent View

View child;//child view of the desired delegate

Rect toucharea;//Delegate Touch area

Parent.settouchdelegate (Newtouchdelegate (Toucharea, child));

Original link Http://wugengxin.cn/download/pdf/android/PRE_andevcon_mastering-the-android-touch-system.pdf

Mastering the Android Touch event 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.