Android Touch Basics: Motionevent

Source: Internet
Author: User
Tags gety

The previous article layer describes the transfer mechanism for Android Touch events (Motionevent) from the framework layer. This article will detail some of the members and methods of Motionevent. Understand that Motionevent has a great effect on developing some effects, such as dragging controls or multipoint scaling controls. At the same time, mastering the Motionevent class is also the foundation of learning Android Touch technology.

First, some constants

Common action constants:

Public static final int action_down = 0; Single Touch Action

public static final int action_up = 1;Single touch Leave action
public static final int action_move = 2;Touch Point Move Action
public static final int action_cancel = 3; Touch Action Cancel
public static final int action_outside = 4; Touch action out of bounds
public static final int action_pointer_down = 5; multi-Touch action
public static final int action_pointer_up = 6; Multi-point off action
Here are some non-touch events
public static final int action_hover_move = 7;
public static final int action_scroll = 8;
public static final int action_hover_enter = 9;
public static final int action_hover_exit = 10;

Mask Constants

Action_mask = 0x000000ff Action Mask
Action_pointer_index_mask = 0X0000ff00 Touch Point Index Mask

Action_pointer_index_shifT = 8 Gets the number of bits that the touch point index needs to move


Ii. Related methods

the Getaction () method returns the int type, using only the lower 16 bits, Where: Low eight bits is the type of action, high 8 bits is the representation of the touch Point index value (single point is 0, double point is 1)

Get action type: int action = Event.getaction () & Action_mask or use getactionmasked ()

Get Touch Point index type: int pointerindex = (event.getaction () & action_pointer_index_mask) >> Action_pointer _index_shift

or use Getactionindex ()

Why should I have index information?

With the index information, we can judge whether the Motionevent object in the Ontouchevent event corresponds to a single point of information or multipoint information.

The following code snippet allows the user to drag an object on the screen. It records the position of the initial point, calculates the distance the point moves, and moves the object to the new location. It correctly handles this situation: when the first finger drags the control to a position, then presses the second finger, and the second finger is on the same control. When the user lifts the first finger, the control does not run to the position of the second finger and the second finger can continue to drag the control.

The ' active pointer ' is the one currently moving our object.private int mactivepointerid = invalid_pointer_id; @Override    public boolean ontouchevent (motionevent ev) {/-Let the scalegesturedetector inspect all events.                 Mscaledetector.ontouchevent (EV);             Final int action = motioneventcompat.getactionmasked (EV); Switch (action) {case Motionevent.action_down: {final int pointerindex = motioneventcompat.getactionindex (EV)         ;         Final float x = motioneventcompat.getx (EV, POINTERINDEX);                     Final float y = motioneventcompat.gety (ev, POINTERINDEX);        Remember where we started (for dragging) Mlasttouchx = x;        Mlasttouchy = y;        Save the ID of this pointer (for dragging) Mactivepointerid = Motioneventcompat.getpointerid (EV, 0);    Break        } case Motionevent.action_move: {//Find the index of the active pointer and fetch it position  Final int pointerindex =               Motioneventcompat.findpointerindex (EV, Mactivepointerid);        Final float x = motioneventcompat.getx (EV, POINTERINDEX);                    Final float y = motioneventcompat.gety (ev, POINTERINDEX);        Only move if the scalegesturedetector isn ' t processing a gesture. if (!mscaledetector.isinprogress ()) {//Calculate the distance moved final float dx = X-mlasttouc            HX;            Final float dy = y-mlasttouchy;            Mposx + = DX;            Mposy + = dy;        Invalidate ();        }//Remember This touch position for the next move event Mlasttouchx = x;        Mlasttouchy = y;    Break        } case MOTIONEVENT.ACTION_UP: {mactivepointerid = invalid_pointer_id;    Break        } case motionevent.action_cancel: {mactivepointerid = invalid_pointer_id;    Break } case MOTIONEVENT.ACTION_POINTER_UP: {final int poiNterindex = Motioneventcompat.getactionindex (EV);         Final int pointerid = Motioneventcompat.getpointerid (EV, POINTERINDEX); if (Pointerid = = Mactivepointerid) {//This is our active pointer going up.            Choose a new//active pointer and adjust accordingly. Final int newpointerindex = Pointerindex = = 0?            1:0;             Mlasttouchx = Motioneventcompat.getx (EV, NEWPOINTERINDEX);             Mlasttouchy = motioneventcompat.gety (EV, NEWPOINTERINDEX);        Mactivepointerid = Motioneventcompat.getpointerid (EV, NEWPOINTERINDEX);    } break; }} return true;

Motionevent also contains other historical mobile data in the move operation to easy to handle touch moving operations.

The Android SDK has an expression in this class:

for efficiency, motion events with Action_move may batch together multiple movement samples within a single object.



Related Article

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.