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

Android如何使用GestureDetector進行手勢檢測詳解

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

1.引言

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

2.進行手勢檢測

2.1 創(chuàng)建GestureDetector

進行手勢檢測之前,需要先新建GestureDetector對象,示例如下:

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

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

2.4 檢測雙擊手勢

雙擊手勢也是一種常見的手勢事件,使用GestureDetector檢測雙擊手勢需要調(diào)用setOnDoubleTapListener()方法設(shè)置GestureDetector.OnDoubleTapListener(),并實現(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

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

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

        return true;
    }
}

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

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

3.總結(jié)

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

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

相關(guān)文章

  • Android 2d游戲開發(fā)之貪吃蛇基于surfaceview

    Android 2d游戲開發(fā)之貪吃蛇基于surfaceview

    這篇文章主要介紹了Android 2d游戲開發(fā)基于surfaceview的貪吃蛇,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Android Studio配置Kotlin開發(fā)環(huán)境詳細步驟

    Android Studio配置Kotlin開發(fā)環(huán)境詳細步驟

    這篇文章主要介紹了Android Studio配置Kotlin開發(fā)環(huán)境詳細步驟的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android使用SQLite數(shù)據(jù)庫的簡單實例

    Android使用SQLite數(shù)據(jù)庫的簡單實例

    這篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的簡單實例,有需要的朋友可以參考一下
    2013-12-12
  • Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽示例

    Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽示例

    這篇文章主要介紹了Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽,結(jié)合實例形式分析了Android針對RadioGroup單選按鈕簡單實用技巧,需要的朋友可以參考下
    2017-07-07
  • Android快速實現(xiàn)無預(yù)覽拍照功能

    Android快速實現(xiàn)無預(yù)覽拍照功能

    這篇文章主要為大家詳細介紹了Android快速實現(xiàn)無預(yù)覽拍照功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 基于Android XML解析與保存的實現(xiàn)

    基于Android XML解析與保存的實現(xiàn)

    本篇文章小編為大家介紹,基于Android XML解析與保存的實現(xiàn)。需要的朋友參考下
    2013-04-04
  • Android如何快速集成騰訊Bugly

    Android如何快速集成騰訊Bugly

    這篇文章主要介紹了Android如何快速集成騰訊Bugly,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • 最新評論