GestureDetector 使用

GestureDetector 是 Android 中,專門用來進行手勢監聽的一個對象,在他的監聽器中,我們通過傳入 MotionEvents 對象,就可以在各種事件的回調方法中各種手勢進行監測。舉個例子: GestureDetector 的 OnGestureListener 就是一種回調方法,就是說在獲得了傳入的這個 MotionEvents 對象之後,進行了處理,我們通過重寫了其中的各種方法(單擊事件、雙擊事件等等),就可以監聽到單擊,雙擊,滑動等事件,然後直接在這些方法內部進行處理。

button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}
});

private void iniGestureListener(){
GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onDoubleTap(MotionEvent e) {
MyToast.makeToast(GestureDetectorActivity.this, “double click up!”);
return super.onDoubleTap(e);
}

    detector = new GestureDetector(GestureDetectorActivity.this, listener);
}

new Handler(Looper.getMainLooper())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章