GestureDetector

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

哈哈

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

public class GestureDetector {
    public interface OnGestureListener {
        //down事件 
        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);
        //表示發(fā)生了雙擊行為
        boolean onDoubleTapEvent(MotionEvent e);
    }

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

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

public class GestureDetector {
    //前面三個接口就是上面提到的接口
    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);
    }

  //警察叔叔,就是這個類
    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;
        }
    }

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

 GestureDetector gestureDetector;

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


 @Override
 public boolean onTouchEvent(MotionEvent event) {
        // 在onTouchEvent調用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://m.itdecent.cn/p/bac3875908d7
http://blog.sina.com.cn/s/blog_77c6324101017hs8.html
http://blog.csdn.net/harvic880925/article/details/39520901
我翻了下Android開發(fā)藝術探索,里面也提到了,不過沒有我寫的這么詳細。不過書里面有句話我加在最后:
如果只是監(jiān)聽滑動相關的,建議在onTouchEvent中實現(xiàn),如果要監(jiān)聽雙擊的這種行為,那么就使用GestureDetector。

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容