• Android手势识别类,GestureDetector,ScaleGestureDetector


    Android程序使用中会有很多的手势,双击、长按、滑动、缩放等,我们可以通过手势识别类GestureDetectorScaleGestureDetector进行识别。

    1.GestureDetector

    (1) 包含方法

    onDown  按下触发。

    onShowPress  按下但尚未松开或移动手指时调用。

    onSingleTapUp  单点后抬起触发。

    onScroll  滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。

    onLongPress  长按触发。

    onFling  迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。

    (2) 使用GestureDetector

    可使用OnGestureListenerSimpleOnGestureListener作为参数;参数使用OnGestureListener需要实现全部方法;参数使用SimpleOnGestureListener只需要实现任意数量的方法。

    1. //参数使用OnGestureListener需要实现全部方法
    2. GestureDetector gestureDetector=new GestureDetector( context , new GestureDetector.OnGestureListener() {
    3. public boolean onDown(MotionEvent motionEvent) {
    4. //按下触发。
    5. return false;
    6. }
    7. public void onShowPress(MotionEvent motionEvent) {
    8. //按下但尚未松开或移动手指时调用。
    9. }
    10. public boolean onSingleTapUp(MotionEvent motionEvent) {
    11. //单点后抬起触发。
    12. return false;
    13. }
    14. public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
    15. //滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。
    16. return false;
    17. }
    18. public void onLongPress(MotionEvent motionEvent) {
    19. //长按触发。
    20. }
    21. public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float vx, float vy) {
    22. //迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。
    23. return false;
    24. }
    25. });
    26. imageView.setOnTouchListener(new View.OnTouchListener() {
    27. public boolean onTouch(View view, MotionEvent motionEvent) {
    28. //ix= motionEvent.getX();
    29. //iy=motionEvent.getY();
    30. //传入触碰事件
    31. gestureDetector.onTouchEvent(motionEvent);
    32. return true;
    33. }
    34. });
    1. //参数使用SimpleOnGestureListener只需要实现任意数量的方法
    2. GestureDetector gestureDetector = new GestureDetector( context , new GestureDetector.SimpleOnGestureListener() {
    3. public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    4. //滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。
    5. return false;
    6. }
    7. });
    8. imageView.setOnTouchListener(new View.OnTouchListener() {
    9. public boolean onTouch(View view, MotionEvent motionEvent) {
    10. //ix= motionEvent.getX();
    11. //iy=motionEvent.getY();
    12. //传入触碰事件
    13. gestureDetector.onTouchEvent(motionEvent);
    14. return true;
    15. }
    16. });

    2.ScaleGestureDetector

    (1) 包含方法

    onScale  缩放时调用。

    onScaleBegin  缩放开始时调用。

    onScaleEnd  缩放结束时调用。

    (2) 使用ScaleGestureDetector

    可使用OnScaleGestureListenerSimpleOnScaleGestureListener作为参数;参数使用OnScaleGestureListener需要实现全部方法;参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法。

    1. //参数使用OnScaleGestureListener需要实现全部方法
    2. ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context , new ScaleGestureDetector.OnScaleGestureListener() {
    3. public boolean onScale( ScaleGestureDetector scaleGestureDetector) {
    4. //缩放时调用。
    5. return false;
    6. }
    7. public boolean onScaleBegin( ScaleGestureDetector scaleGestureDetector) {
    8. //缩放开始时调用。
    9. return false;
    10. }
    11. public void onScaleEnd( ScaleGestureDetector scaleGestureDetector) {
    12. //缩放结束时调用。
    13. }
    14. });
    15. imageView.setOnTouchListener(new View.OnTouchListener() {
    16. public boolean onTouch(View view, MotionEvent motionEvent) {
    17. //ix= motionEvent.getX();
    18. //iy=motionEvent.getY();
    19. //传入触碰事件
    20. scaleGestureDetector.onTouchEvent(motionEvent);
    21. return true;
    22. }
    23. });
    1. //参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法
    2. ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context ,new ScaleGestureDetector.SimpleOnScaleGestureListener(){
    3. public boolean onScale( ScaleGestureDetector scaleGestureDetector) {
    4. //缩放时调用。
    5. return false;
    6. }
    7. });
    8. imageView.setOnTouchListener(new View.OnTouchListener() {
    9. public boolean onTouch(View view, MotionEvent motionEvent) {
    10. //ix= motionEvent.getX();
    11. //iy=motionEvent.getY();
    12. //传入触碰事件
    13. scaleGestureDetector.onTouchEvent(motionEvent);
    14. return true;
    15. }
    16. });

  • 相关阅读:
    Alist访问主页显示空白解决方法
    qsv加速滤镜
    Unity SRP 管线【第二讲:Draw Call】
    一文教你在 centos7 下安装 Oracle19 C(完整版)
    【字节跳动实习面经(测试开发岗 二面)希望渺茫】
    【2022年玄武云科技AI算法岗秋招面试记录】
    使用cmd登录阿里云服务器
    信息系统项目管理师(2022年)—— 重点内容:管理科学基础知识(23)
    研发效能工程实践-利用Superset快速打造大数据BI平台
    java计算机毕业设计商店管理系统源码+系统+mysql数据库+lw文档+部署
  • 原文地址:https://blog.csdn.net/m0_57150356/article/details/134404409