Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解
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)文章希望大家以后多多支持腳本之家!
- Android GestureDetector用戶手勢(shì)檢測(cè)實(shí)例講解
- android使用gesturedetector手勢(shì)識(shí)別示例分享
- Android GestureDetector手勢(shì)滑動(dòng)使用實(shí)例講解
- Android手勢(shì)識(shí)別器GestureDetector使用詳解
- Android自定義viewgroup可滾動(dòng)布局 GestureDetector手勢(shì)監(jiān)聽(tīng)(5)
- Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
- Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)效果
- Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽(tīng)與處理的方法
- Android觸摸及手勢(shì)操作GestureDetector
- Android使用手勢(shì)監(jiān)聽(tīng)器GestureDetector遇到的不響應(yīng)問(wèn)題
相關(guān)文章
如何使用SurfaceView實(shí)現(xiàn)魚兒游動(dòng)動(dòng)畫
這篇文章主要教大家如何使用SurfaceView實(shí)現(xiàn)魚兒游動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04android判斷一個(gè)Activity是否處于棧頂?shù)膶?shí)例
下面小編就為大家分享一篇android判斷一個(gè)Activity是否處于棧頂?shù)膶?shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android 2d游戲開(kāi)發(fā)之貪吃蛇基于surfaceview
這篇文章主要介紹了Android 2d游戲開(kāi)發(fā)基于surfaceview的貪吃蛇,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Android Studio配置Kotlin開(kāi)發(fā)環(huán)境詳細(xì)步驟
這篇文章主要介紹了Android Studio配置Kotlin開(kāi)發(fā)環(huán)境詳細(xì)步驟的相關(guān)資料,需要的朋友可以參考下2017-05-05Android使用SQLite數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12Android開(kāi)發(fā)之RadioGroup的簡(jiǎn)單使用與監(jiān)聽(tīng)示例
這篇文章主要介紹了Android開(kāi)發(fā)之RadioGroup的簡(jiǎn)單使用與監(jiān)聽(tīng),結(jié)合實(shí)例形式分析了Android針對(duì)RadioGroup單選按鈕簡(jiǎn)單實(shí)用技巧,需要的朋友可以參考下2017-07-07Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06基于Android XML解析與保存的實(shí)現(xiàn)
本篇文章小編為大家介紹,基于Android XML解析與保存的實(shí)現(xiàn)。需要的朋友參考下2013-04-04