GestureDetector

我記得有個(gè)在工作中有個(gè)需求是關(guān)于判斷雙擊。當(dāng)時(shí)我一個(gè)同事給我一個(gè)方案:用計(jì)時(shí)的方法去做,在view的onTouchEvent中。當(dāng)ACTION_DOWN時(shí)記錄當(dāng)時(shí)的時(shí)間。然后第二次ACTION_DOWN時(shí)再記錄一次,通過(guò)這兩次點(diǎn)擊的時(shí)間間隔來(lái)判斷是不是雙擊。 當(dāng)時(shí)我就采納了這個(gè)建議,效果也做出來(lái)了,還有點(diǎn)小高興。
某天下午,偶然看到GestureDetector這個(gè)東西然后就大概的了解了一下。
突然覺(jué)得在onTouch里面處理雙擊有點(diǎn)過(guò)于麻煩。感覺(jué)我和同事都是:

哈哈

然后我們來(lái)看下源代碼(注釋太多我全部干掉了 只剩下幾個(gè)方法)。

public class GestureDetector {
    public interface OnGestureListener {
        //down事件 
        boolean onDown(MotionEvent e);
        //觸摸屏幕,尚未松開(kāi)或者拖動(dòng) 
        void onShowPress(MotionEvent e);
        //觸摸屏幕后松開(kāi),單擊事件 
        boolean onSingleTapUp(MotionEvent e);
        //觸摸屏幕后拖動(dòng),滑動(dòng)事件
        boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
        //長(zhǎng)按事件
        void onLongPress(MotionEvent e);
        //快速滑動(dòng)
        boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
    }

    public interface OnDoubleTapListener {
        //嚴(yán)格的單擊行為,不是雙擊中的一次單擊
        boolean onSingleTapConfirmed(MotionEvent e);
        //雙擊事件
        boolean onDoubleTap(MotionEvent e);
        //表示發(fā)生了雙擊行為
        boolean onDoubleTapEvent(MotionEvent e);
    }

    public interface OnContextClickListener {
        boolean onContextClick(MotionEvent e);
    }
}

從以上源碼可以看到GestureDetector 已經(jīng)包括了大部分的手勢(shì)事件。但是源碼的用意我感覺(jué)有點(diǎn)奇怪如果我要監(jiān)聽(tīng)滑動(dòng)又要監(jiān)聽(tīng)雙擊那我不是要繼承兩個(gè)接口?如果我只監(jiān)聽(tīng)其中一個(gè)事件,難道我要重寫(xiě)所有方法?于是又有下面SimpleOnGestureListener 這個(gè)類(lèi),它是屬于GestureDetector的靜態(tài)內(nèi)部類(lèi)
SimpleOnGestureListener實(shí)現(xiàn)了OnGestureListener 和 OnDoubleTapListener 。實(shí)現(xiàn)了兩個(gè)接口中的所有方法。如此一來(lái)你寫(xiě)個(gè)類(lèi)繼承SimpleOnGestureListener然后根據(jù)自己的需求重寫(xiě)需要的方法即可。
看源碼:

public class GestureDetector {
    //前面三個(gè)接口就是上面提到的接口
    public interface OnGestureListener {
        boolean onDown(MotionEvent e);
        void onShowPress(MotionEvent e);
        boolean onSingleTapUp(MotionEvent e);
        boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
        void onLongPress(MotionEvent e);
        boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
    }

    public interface OnDoubleTapListener {
        boolean onSingleTapConfirmed(MotionEvent e);
        boolean onDoubleTap(MotionEvent e);
        boolean onDoubleTapEvent(MotionEvent e);
    }
  
    public interface OnContextClickListener {
        boolean onContextClick(MotionEvent e);
    }

  //警察叔叔,就是這個(gè)類(lèi)
    public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener,
            OnContextClickListener {

        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        public void onLongPress(MotionEvent e) {
        }

        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            return false;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            return false;
        }

        public void onShowPress(MotionEvent e) {
        }

        public boolean onDown(MotionEvent e) {
            return false;
        }

        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }

        public boolean onContextClick(MotionEvent e) {
            return false;
        }
    }

我寫(xiě)了個(gè)方法去測(cè)試這些方法。(至于怎么去測(cè)試,這個(gè)確實(shí)簡(jiǎn)單,想怎么寫(xiě)就怎么寫(xiě)
不過(guò)還是把幾句至關(guān)重要的代碼指出來(lái),我寫(xiě)的是自定義View。直接寫(xiě)在Activity中也可以,測(cè)試方法很多。但是實(shí)現(xiàn)方式是一樣的)
以下是關(guān)鍵代碼:

 GestureDetector gestureDetector;

   public TestGestureDetectorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化(注意:GestureDetector的構(gòu)造函數(shù)有一堆,我這里傳入了一個(gè)繼承SimpleOnGestureListener 的自定義類(lèi))
        gestureDetector = new GestureDetector(context, new MySimpleOnGestureListener());
    }


 @Override
 public boolean onTouchEvent(MotionEvent event) {
        // 在onTouchEvent調(diào)用GestureDetector的onTouchEvent方法
        gestureDetector.onTouchEvent(event);
        return true;
    }

以下為源碼:

public class TestGestureDetectorView extends View  {

    GestureDetector gestureDetector;

    public TestGestureDetectorView(Context context) {
        this(context, null);
    }

    public TestGestureDetectorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestGestureDetectorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        gestureDetector = new GestureDetector(context, new MySimpleOnGestureListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }

    private class MySimpleOnGestureListener extends SimpleOnGestureListener {

        public MySimpleOnGestureListener() {
            super();
        }

        @Override
        public boolean onDown(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onDown");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onShowPress");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onSingleTapUp");
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.i("TestGestureDetectorView", "onScroll");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onLongPress");
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i("TestGestureDetectorView", "onFling");
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onSingleTapConfirmed");
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onDoubleTap");
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.i("TestGestureDetectorView", "onDoubleTapEvent");
            return false;
        }

        @Override
        public boolean onContextClick(MotionEvent e) {
            return super.onContextClick(e);
        }
    }
}

推薦的博客:
http://www.itdecent.cn/p/bac3875908d7
http://blog.sina.com.cn/s/blog_77c6324101017hs8.html
http://blog.csdn.net/harvic880925/article/details/39520901
我翻了下Android開(kāi)發(fā)藝術(shù)探索,里面也提到了,不過(guò)沒(méi)有我寫(xiě)的這么詳細(xì)。不過(guò)書(shū)里面有句話我加在最后:
如果只是監(jiān)聽(tīng)滑動(dòng)相關(guān)的,建議在onTouchEvent中實(shí)現(xiàn),如果要監(jiān)聽(tīng)雙擊的這種行為,那么就使用GestureDetector。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容