Android4.4 GestureDetector (Gesture Recognition)
If the application requires complex gesture matching, you can use GestureDetector.
Steps:
1. The OnGestureListener class can inherit the SimpleOnGestureListener class and then rewrite the corresponding function;
2. Create a GestureDetector Class Object and add the Custom listening class object in step 1 as the parameter;
3. When a MotionEvent event is received, the OnGestureListener. onTouchEvent (onTouchEvent) function is called.
Some custom processing of gesture matching is implemented in the gesture listening class.
The following is a simple analysis of the above three steps: first look at the GestureDetector Constructor
public GestureDetector(Context context, OnGestureListener listener, Handler handler) { if (handler != null) { mHandler = new GestureHandler(handler); } else { mHandler = new GestureHandler(); } mListener = listener; if (listener instanceof OnDoubleTapListener) { setOnDoubleTapListener((OnDoubleTapListener) listener); } init(context); }The constructor shows that a new GestureDetector object requires an OnGestureListener object, which is stored in the mListener. If the gesture listening inherits the SimpleOnGestureListener class, setOnDoubleTapListener () is also called (). The init function does some initialization functions.
Next, let's take a look at the OnGestureListener. onTouchEvent (onTouchEvent) function processing in step 1. This function is a little long and can be read in segments.
public boolean onTouchEvent(MotionEvent ev) { if (mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onTouchEvent(ev, 0); } final int action = ev.getAction(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final boolean pointerUp = (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP; final int skipIndex = pointerUp ? ev.getActionIndex() : -1; // Determine focal point float sumX = 0, sumY = 0; final int count = ev.getPointerCount(); for (int i = 0; i < count; i++) { if (skipIndex == i) continue; sumX += ev.getX(i); sumY += ev.getY(i); } final int div = pointerUp ? count - 1 : count; final float focusX = sumX / div; final float focusY = sumY / div; boolean handled = false;InputEventConsistencyVerifier is used for debugging, and mVelocityTracker is a Speed Tracing object. The following code logic is used to find the center point of a MotionEvent. During debugging, it is found that getPointerCount has only one point, click or slide. Next, let's look at it.
switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: mDownFocusX = mLastFocusX = focusX; mDownFocusY = mLastFocusY = focusY; // Cancel long press and taps cancelTaps(); break; case MotionEvent.ACTION_POINTER_UP: mDownFocusX = mLastFocusX = focusX; mDownFocusY = mLastFocusY = focusY; // Check the dot product of current velocities. // If the pointer that left was opposing another velocity vector, clear. mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); final int upIndex = ev.getActionIndex(); final int id1 = ev.getPointerId(upIndex); final float x1 = mVelocityTracker.getXVelocity(id1); final float y1 = mVelocityTracker.getYVelocity(id1); for (int i = 0; i < count; i++) { if (i == upIndex) continue; final int id2 = ev.getPointerId(i); final float x = x1 * mVelocityTracker.getXVelocity(id2); final float y = y1 * mVelocityTracker.getYVelocity(id2); final float dot = x + y; if (dot < 0) { mVelocityTracker.clear(); break; } } break; case MotionEvent.ACTION_DOWN: if (mDoubleTapListener != null) { boolean hadTapMessage = mHandler.hasMessages(TAP); if (hadTapMessage) mHandler.removeMessages(TAP); if ((mCurrentDownEvent != null) && (mPreviousUpEvent != null) && hadTapMessage && isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, ev)) { // This is a second tap mIsDoubleTapping = true; // Give a callback with the first tap of the double-tap handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent); // Give a callback with down event of the double-tap handled |= mDoubleTapListener.onDoubleTapEvent(ev); } else { // This is a first tap mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT); } } mDownFocusX = mLastFocusX = focusX; mDownFocusY = mLastFocusY = focusY; if (mCurrentDownEvent != null) { mCurrentDownEvent.recycle(); } mCurrentDownEvent = MotionEvent.obtain(ev); mAlwaysInTapRegion = true; mAlwaysInBiggerTapRegion = true; mStillDown = true; mInLongPress = false; mDeferConfirmSingleTap = false; if (mIsLongpressEnabled) { mHandler.removeMessages(LONG_PRESS); mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime() + TAP_TIMEOUT + LONGPRESS_TIMEOUT); } mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + TAP_TIMEOUT); handled |= mListener.onDown(ev); break;
For ACTION_DOWN events, call mListener. onDown (ev); for processing, mListener is a gesture listening class object defined in step 1. For MotionEvent. ACTION_MOVE event, call mListener. onScroll (mCurrentDownEvent, ev, scrollX, scrollY); function, mCurrentDownEvent is the press point in the current slide gesture, ev is the current touch point, scrollX and scrollY are the offset of the x/y axis of the current touch point following the touch point. For ACTION_UP events, call the mListener. onSingleTapUp (ev) or mListener. onFling (mCurrentDownEvent, ev, velocityX, velocityY) function.
To be continued.