zoukankan      html  css  js  c++  java
  • 【Andorid------手势识别】GestureDetector和SimpleOnGestureListener的使用教程(转)——

    1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的,因此这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener,SimpleOnGestureListener类是GestureDetector提供给我们的一个更方便的响应不同手势的类,这个类实现了上述两个接口(但是所有的方法体都是空的),该类是static class,也就是说它实际上是一个外部类。程序员可以在外部继承这个类,重写里面的手势处理方法。

    通过GestureDetector的构造方法可以将SimpleOnGestureListener对象传递进去,这样GestureDetector能处理不同的手势了。

    2. 具体用法:

    2.1  

     1 private class DefaultGestureListener extends SimpleOnGestureListener{
     2         @Override
     3         public boolean onSingleTapUp(MotionEvent e) {
     4             return false;
     5         }
     6         @Override
     7         public void onLongPress(MotionEvent e) {
     8             
     9         }
    10         /**
    11          * @param e1 The first down motion event that started the scrolling.
    12            @param e2 The move motion event that triggered the current onScroll.
    13            @param distanceX The distance along the X axis(轴) that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
    14            @param distanceY The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
    15                        无论是用手拖动view,或者是以抛的动作滚动,都会多次触发 ,这个方法在ACTION_MOVE动作发生时就会触发 参看GestureDetector的onTouchEvent方法源码
    16          * */
    17         @Override
    18         public boolean onScroll(MotionEvent e1, MotionEvent e2,
    19                 float distanceX, float distanceY) {
    20             return false;
    21         }
    22         /**
    23          * @param e1 第1个ACTION_DOWN MotionEvent 并且只有一个
    24          * @param e2 最后一个ACTION_MOVE MotionEvent 
    25          * @param velocityX X轴上的移动速度,像素/秒  
    26          * @param velocityY Y轴上的移动速度,像素/秒
    27          * 这个方法发生在ACTION_UP时才会触发 参看GestureDetector的onTouchEvent方法源码
    28          * 
    29          * */
    30         @Override
    31         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    32                 float velocityY) {
    33             return false;
    34         }
    35         @Override
    36         public void onShowPress(MotionEvent e) {
    37             
    38         }
    39         @Override
    40         public boolean onDown(MotionEvent e) {
    41             return false;
    42         }
    43         @Override
    44         public boolean onDoubleTap(MotionEvent e) {
    45             return false;
    46         }
    47         @Override
    48         public boolean onDoubleTapEvent(MotionEvent e) {
    49             return false;
    50         }
    51         /**
    52          * 这个方法不同于onSingleTapUp,他是在GestureDetector确信用户在第一次触摸屏幕后,没有紧跟着第二次触摸屏幕,也就是不是“双击”的时候触发
    53          * */
    54         @Override
    55         public boolean onSingleTapConfirmed(MotionEvent e) {
    56             return false;
    57         }
    58         
    59     }

    2.2  public GestureDetector (Context context, GestureDetector.OnGestureListener listener)通过构造方法将手势响应交给手势识别类

    1 private OnTouchListener gestureTouchListener = new OnTouchListener() {
    2         @Override
    3         public boolean onTouch(View v, MotionEvent event) {
    4             return gDetector.onTouchEvent(event);
    5         }
    6     };

    ok,到此为止就结束了

    遇到的问题:

    1. onFling(***)无法触发

         通过设置 mListView.setLongClickable(true);即可(我处理的是ListView的手势事件),只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。

    2. 用户长按手机屏幕,就会触发长按事件,离开屏幕时,就会触发up事件,但是SimpleOnGestureListener没有对longPress事件的up事件对外提供接口

    解决办法:

    类似于这样,截获up事件,因为所有的都是有OnTouchListener 先获得,然后传递给SimpleOnGestureListener的,这里有一点必须要注意:

    截获到up事件,我们进行了处理后,必须要将这个事件再交给SimpleOnGestureListener处理,虽然我们只截获长按事件的up,但是SimpleOnGestureListener对于长按事件的up也做了一些处理,只是没有对外提供接口。

    做了什么处理:

    1 if (mInLongPress) {
    2                 mHandler.removeMessages(TAP);
    3                 mInLongPress = false;
    4 }

    如果不交给SimpleOnGestureListener处理,那么单击动作也会触发onLongPress方法。

     1 private OnTouchListener gestureTouchListener = new OnTouchListener() {
     2         @Override
     3         public boolean onTouch(View v, MotionEvent event) {
     4             switch (event.getAction()) {
     5             case MotionEvent.ACTION_DOWN:
     6                 return gDetector.onTouchEvent(event);
     7            case MotionEvent.ACTION_UP:
     8                 MyGesture.FlagInfo info = mGesture.getFlagInfo();
     9                 if(info.isConnected==true){
    10                     int firstVisiblePosition = mListView.getFirstVisiblePosition();
    11                     View view = mListView.getChildAt(info.position-firstVisiblePosition);
    12                     if(view!=null){
    13                         view.setBackgroundResource(R.drawable.listitem_background_blue);
    14                         info.isConnected = false;
    15                     }
    16                 }
    17                 return gDetector.onTouchEvent(event);
    18             case MotionEvent.ACTION_MOVE:
    19                 return gDetector.onTouchEvent(event);
    20             }
    21             return false;
    22             
    23         }
    24     };

    总结:

    1. 点击屏幕上的某项的执行流程  有两种情况,一种是时间很短,一种时间稍长

    时间很短:onDown--------》onSingleTapUp--------》onSingleTapConfirmed

    时间稍长:onDown--------》onShowPress------》onSingleTapUp--------》onSingleTapConfirmed

    2. 长按事件

    onDown--------》onShowPress------》onLongPress

    3.抛:手指触动屏幕后,稍微滑动后立即松开

    onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling

    4.拖动

    onDown------》onScroll----》onScroll------》onFiling

    注意:有的时候会触发onFiling,但是有的时候不会触发,个人理解是人的动作不标准所致。

  • 相关阅读:
    windos下安装多个mysql服务
    mysql 提示too many connections”的解决办法
    python打包成exe
    对于点击后需要等待一段时间才会有的元素的操作
    python自动化报错
    自动化测试中对不能输入的框体的处理
    对于下拉框的处理方法
    selenium自动化遇见Cannot find class in classpath问题
    企业如何留住人才
    如何成为一名优秀的前端工程师
  • 原文地址:https://www.cnblogs.com/zl1991/p/5168766.html
Copyright © 2011-2022 走看看