Previous articles introduced the Android Touch event (MotionEvent) transfer mechanism from the Framework layer. This article describes in detail some of the MotionEvent members and methods. MotionEvent is helpful for developing special effects such as drag controls or multi-point scaling controls. Mastering the MotionEvent class is also the basis for learning android touch technology well.
1. Constants
Common Action constants:
Public static final int ACTION_DOWN = 0; spof
Public static final int ACTION_UP = 1; spof
Public static final int ACTION_MOVE = 2; touch point movement
Public static final int ACTION_CANCEL = 3; the touch action is canceled.
Public static final int ACTION_OUTSIDE = 4; The touch action exceeds the boundary.
Public static final int ACTION_POINTER_DOWN = 5; multi-point touch action
Public static final int ACTION_POINTER_UP = 6; Multi-Point exit action
The following 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 constant
ACTION_MASK = 0X000000ff action mask
ACTION_POINTER_INDEX_MASK = 0X0000ff00 touch point index mask
ACTION_POINTER_INDEX_SHIFT = 8 obtain the number of digits to be moved by the touch point index.
Ii. Related Methods
The getAction () method returns the int type and uses only the lower 16 bits. The lower eight bits are the action type, the top 8 bits indicate the index value of the touch point (single point is 0, double point is 1)
Obtain the action type: int action = event. getAction () & ACTION_MASK or use getActionMasked ()
Obtain the index type of the touch point: int pointerIndex = (event. getAction () & ACTION_POINTER_INDEX_MASK)> ACTION_POINTER_INDEX_SHIFT
Or use getActionIndex ()
Why is there index information?
With the index information, we can determine in the onTOuchEvent event whether the MotionEvent object is a single point of information or multi-point information.
The following code snippet enables users to drag an object on the screen. It records the location of the initial vertex, calculates the distance from the vertex movement, and moves the object to a new location. It correctly handles this situation: when the first finger dragged the control to a position, then pressed the second finger, and the second finger and the same control. When the user raises the first finger, the control does not run to 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;@Overridepublic 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 its 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 - mLastTouchX; 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 was 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 a mobile operation to facilitate touch-based mobile operations.
The android sdk provides the following description for this class:
For efficiency, motion events with ACTION_MOVE may batch together multiple movement samples within a single object.