android4.4之GestureDetector(手勢識別)

來源:互聯網
上載者:User

android4.4之GestureDetector(手勢識別)

應用如果需要複雜的手勢匹配,這時候可以使用GestureDetector來實現。

實現步驟:

1、實現OnGestureListener類,也可繼承SimpleOnGestureListener類然後複寫相應函數;

2、建立一個GestureDetector類對象,然後new一個第1步中自訂的監聽類對象作為參數穿進去;

3、在接收到MotionEvent事件時,調用OnGestureListener.onTouchEvent(onTouchEvent)函數。

對手勢匹配做的一些自訂處理在手勢監聽類中實現。

下面對以上三步做一些簡單分析:先看GestureDetector建構函式

    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);    }
從建構函式看出new一個GestureDetector對象需要一個OnGestureListener對象,該對象儲存在mListener中,如果手勢監聽是繼承SimpleOnGestureListener類,那麼還會調用setOnDoubleTapListener()。init函數做一些初始化函數,先不看。

接著看第3步OnGestureListener.onTouchEvent(onTouchEvent)函數處理。此函數稍微有點長,分段閱讀

    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是調試用的,mVelocityTracker是一個速度追蹤類對象,緊接著的這段代碼邏輯是求一個MotionEvent的中心點,這邊調試時發現getPointerCount只有一個點而已,不管是點擊或滑動。接著往下看

        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;

對於ACTION_DOWN事件,調用mListener.onDown(ev);處理,mListener是指向第1步中定義的手勢監聽類對象。對於MotionEvent.ACTION_MOVE事件,調用mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY);函數,mCurrentDownEvent是當前滑動手勢中的按下點,ev是當前觸摸點,scrollX、scrollY是當前觸摸點跟上一個觸摸點的x/y軸位移量。對於ACTION_UP事件,調用mListener.onSingleTapUp(ev)或者mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY);函數。

未完待續。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.