欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解

 更新時(shí)間:2022年01月26日 16:22:25   作者:QiShare  
GestureDetector使用很方便,提供了單擊,雙擊,長(zhǎng)按等操作的處理,但是一般的定義界面都比較復(fù)雜,還用很多需要注意的地方,這篇文章主要給大家介紹了關(guān)于Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)的相關(guān)資料,需要的朋友可以參考下

1.引言

在操作應(yīng)用的時(shí)候,會(huì)有很多不同的手勢(shì)操作,如按下、單擊、雙擊、長(zhǎng)按等手勢(shì),我們可以在這些手勢(shì)事件中添加相應(yīng)的業(yè)務(wù)邏輯,那么如何檢測(cè)不同的手勢(shì)操作就比較重要了,本文將帶大家了解如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)。

2.進(jìn)行手勢(shì)檢測(cè)

2.1 創(chuàng)建GestureDetector

進(jìn)行手勢(shì)檢測(cè)之前,需要先新建GestureDetector對(duì)象,示例如下:

gestureDetector = new GestureDetector(context, new GestureDetector.OnGestureListener() {
    @Override
    public boolean onDown(MotionEvent e) {
        log("onDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        log("onShowPress");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        log("onSingleTapUp");
        return true;
    }

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

    @Override
    public void onLongPress(MotionEvent e) {
        log("onLongPress");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        log("onFling");
        return true;
    }
});

2.2 與onTouchEvent結(jié)合使用

示例中重寫了Activity的onTouchEvent(MotionEvent event)方法,并在其內(nèi)部使用GestureDetector處理觸摸事件,示例如下:

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

2.3 GestureDetector.OnGestureListener

實(shí)現(xiàn)GestureDetector.OnGestureListener內(nèi)的方法,在其中可以檢測(cè)到多種手勢(shì),如onDown(MotionEvent e)按下、onShowPress(MotionEvent e)已經(jīng)執(zhí)行按下,還沒(méi)有移動(dòng)或抬起、onSingleTapUp(MotionEvent e)單擊、onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)滾動(dòng)、onLongPress(MotionEvent e)長(zhǎng)按、onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)。

2.4 檢測(cè)雙擊手勢(shì)

雙擊手勢(shì)也是一種常見(jiàn)的手勢(shì)事件,使用GestureDetector檢測(cè)雙擊手勢(shì)需要調(diào)用setOnDoubleTapListener()方法設(shè)置GestureDetector.OnDoubleTapListener(),并實(shí)現(xiàn)其中的方法,其中的onDoubleTap(MotionEvent e)表示雙擊事件,示例如下:

gestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        log("onSingleTapConfirmed");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        log("onDoubleTap");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        log("onDoubleTapEvent");
        return true;
    }
});
}

2.5 GestureDetector.SimpleOnGestureListener

如果不想實(shí)現(xiàn)GestureDetector.OnGestureListener 內(nèi)的多個(gè)方法,那么可以創(chuàng)建類并繼承GestureDetector.SimpleOnGestureListener,示例如下:

class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener{
    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }
}

在創(chuàng)建GestureDetector對(duì)象的時(shí)候,傳入擴(kuò)展后的類對(duì)象即可,示例如下:

gestureDetector = new GestureDetector(context, new SimpleGestureListener());

3.總結(jié)

使用GestureDetector能方便地進(jìn)行手勢(shì)檢測(cè),靈活合理地使用手勢(shì)檢測(cè),在其中處理應(yīng)用的業(yè)務(wù)邏輯,能讓體驗(yàn)更加的友好。

到此這篇關(guān)于Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)的文章就介紹到這了,更多相關(guān)Android GestureDetector手勢(shì)檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論