網站首頁 編程語言 正文
1.引言
在操作應用的時候,會有很多不同的手勢操作,如按下、單擊、雙擊、長按等手勢,我們可以在這些手勢事件中添加相應的業務邏輯,那么如何檢測不同的手勢操作就比較重要了,本文將帶大家了解如何使用GestureDetector進行手勢檢測。
2.進行手勢檢測
2.1 創建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結合使用
示例中重寫了Activity的onTouchEvent(MotionEvent event)方法,并在其內部使用GestureDetector處理觸摸事件,示例如下:
@Override public boolean onTouchEvent(MotionEvent event) { boolean b = gestureDetector.onTouchEvent(event); if (b) { return true; } return super.onTouchEvent(event); }
2.3 GestureDetector.OnGestureListener
實現GestureDetector.OnGestureListener內的方法,在其中可以檢測到多種手勢,如onDown(MotionEvent e)按下、onShowPress(MotionEvent e)已經執行按下,還沒有移動或抬起、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檢測雙擊手勢需要調用setOnDoubleTapListener()方法設置GestureDetector.OnDoubleTapListener(),并實現其中的方法,其中的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
如果不想實現GestureDetector.OnGestureListener 內的多個方法,那么可以創建類并繼承GestureDetector.SimpleOnGestureListener,示例如下:
class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener{ @Override public boolean onDown(MotionEvent e) { return true; } }
在創建GestureDetector對象的時候,傳入擴展后的類對象即可,示例如下:
gestureDetector = new GestureDetector(context, new SimpleGestureListener());
3.總結
使用GestureDetector能方便地進行手勢檢測,靈活合理地使用手勢檢測,在其中處理應用的業務邏輯,能讓體驗更加的友好。
原文鏈接:https://juejin.cn/post/7057027268144529415
相關推薦
- 2021-11-05 一文搞懂Golang?時間和日期相關函數_Golang
- 2022-06-22 python?實現?mp3Play?音頻播放_python
- 2022-06-19 Rainbond使用Dockerfile構建便捷應用運行流程_云其它
- 2022-02-12 asp.net 報錯 “/”應用程序中的服務器錯誤。 String or binary data w
- 2023-01-28 C#實現繪制鼠標的示例代碼_C#教程
- 2022-09-09 使用Dockerfile腳本定制鏡像的方法_docker
- 2022-10-17 Python?pywin32實現word與Excel的處理_python
- 2022-09-19 ASP.NET?Core模仿中間件方式實現列表過濾功能_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支