網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解_Android
作者:QiShare ? 更新時(shí)間: 2022-04-08 編程語(yǔ)言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)更加的友好。
原文鏈接:https://juejin.cn/post/7057027268144529415
相關(guān)推薦
- 2022-07-01 Armbian5.9.0安裝docker及部署可視化portainer的詳細(xì)教程_docker
- 2024-01-31 在 Nginx 配置中,root 和 alias 指令的區(qū)別是什么
- 2023-06-04 Django修改端口號(hào)與地址的三種方式_python
- 2022-03-05 C語(yǔ)言宏函數(shù)container?of()簡(jiǎn)介_(kāi)C 語(yǔ)言
- 2022-05-08 Windows?Bat腳本實(shí)現(xiàn)定時(shí)重啟應(yīng)用程序的項(xiàng)目實(shí)踐_DOS/BAT
- 2022-04-18 Python簡(jiǎn)單的GUI程序示例詳解_python
- 2022-09-03 Python?pandas找出、刪除重復(fù)的數(shù)據(jù)實(shí)例_python
- 2023-01-02 使用Flutter?構(gòu)建Web應(yīng)用邏輯解析_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支