Android Official Development Document Training series: multi-touch processing for gesture processing

Source: Internet
Author: User
Tags gety

Original address: http://android.xsoftlab.net/training/gestures/multi.html

Multi-touch refers to the case where multiple fingers touch the screen at the same time. This lesson focuses on how to detect multi-touch gestures.

Record multiple touch points

When the Dogan finger touches the screen at the same time, the system generates the following touch events:

    • Action_down-The first point that touches the screen. It is the starting event of the gesture. The pointer data for this touch point is always 0 in the index of the Motionevent object.
    • Action_pointer_down-a point other than the first touch point. The index of the pointer data for this touch point is returned by the Getactionindex () method.
    • Action_move-When the position of the finger on the screen changes.
    • ACTION_POINTER_UP-except when the other touch points that were initially pressed leave the screen.
    • Action_up-When the last touch point leaves the screen.

Each touch point in the Motionevent object can be traced by the corresponding cable or ID of each touch point:

    • Index: The Motionevent object stores information about the touch point in an array. The index of each touch point is the relative position of the touch point in the array. Most methods of the Motionevent object can use these indexes to interact with these points.
    • ID: Each Touch point also contains an ID mapping, which remains relative to the corresponding touch point throughout the lifetime of the gesture event.

The order in which each touch point appears is not fixed. Therefore, the index of the touch point can be moved from event to next index, but the ID of the touch point is always maintained as a constant. You can use the Getpointerid () method to get the ID of a specified touch point, so you can continue to maintain contact with the touch point in the remaining gesture events. Use the Findpointerindex () method to get the index of the touch point based on the specified ID:

private  int  Mactivepointerid; public  boolean  ontouchevent  (Motionevent event) { .... //Get the pointer ID  Mactivepointerid = Event.getpointerid (0 ); //... Many touch events later ...  //use the pointer ID to find the index of the active pointer  //and fetch its position  int  pointerindex = Event.findpointerindex (mAct Ivepointerid); //Get the pointer ' s current position  float  x = Event.getx (Pointerindex); float  y = event.gety (Pointerindex);} 
Get activity for an event

Use the getactionmasked () method to obtain motionevent activity. Unlike the Getaction () method, getactionmasked () applies to multiple touch points. It returns the activity that is being executed. You can use the Getactionindex () method to get the index of the touch point associated with it. The following code demonstrates this process:

Note: the Motioneventcompat class is used in the example. This class is located in the Support library. You should use this class to provide good backwards compatibility. Note that the Motioneventcompat class is not an alternative to the Motionevent class. This class provides a useful static method to extract the activities associated with the Motionevent object.

intAction = motioneventcompat.getactionmasked (event);//Get The index of the pointer associated with the action.intindex = Motioneventcompat.getactionindex (event);intXPos =-1;intYPos =-1; LOG.D (Debug_tag,"The action is"+ actiontostring (action));if(Event.getpointercount () >1) {LOG.D (Debug_tag,"Multitouch Event");//The coordinates of the current screens contact, relative to    //The responding View or Activity. XPos = (int) Motioneventcompat.getx (event, index); YPos = (int) Motioneventcompat.gety (event, index);}Else{//Single Touch eventLOG.D (Debug_tag,"Single Touch Event"); XPos = (int) Motioneventcompat.getx (event, index); YPos = (int) Motioneventcompat.gety (event, index);} ...//Given an action int, returns a string description Public StaticStringactiontostring(intAction) {Switch(action) { CaseMotionevent.action_down:return "Down"; CaseMotionevent.action_move:return "Move"; CaseMotionevent.action_pointer_down:return "Pointer Down"; CaseMOTIONEVENT.ACTION_UP:return "Up"; CaseMOTIONEVENT.ACTION_POINTER_UP:return "Pointer up"; CaseMotionevent.action_outside:return "Outside"; CaseMotionevent.action_cancel:return "Cancel"; }return "";}

For more information on Multitouch, see Course dragging and Scaling.

Android Official Development Document Training series: multi-touch processing for gesture processing

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.