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

private class DefaultGestureListener extends SimpleOnGestureListener{

        @Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
} @Override
public void onLongPress(MotionEvent e) { }
/**
* @param e1 The first down motion event that started the scrolling.
@param e2 The move motion event that triggered the current onScroll.
@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.
@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.
无论是用手拖动view,或者是以抛的动作滚动,都会多次触发 ,这个方法在ACTION_MOVE动作发生时就会触发 参看GestureDetector的onTouchEvent方法源码
* */
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}
/**
* @param e1 第1个ACTION_DOWN MotionEvent 并且只有一个
* @param e2 最后一个ACTION_MOVE MotionEvent
* @param velocityX X轴上的移动速度,像素/秒
* @param velocityY Y轴上的移动速度,像素/秒
* 这个方法发生在ACTION_UP时才会触发 参看GestureDetector的onTouchEvent方法源码
*
* */
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
} @Override
public void onShowPress(MotionEvent e) { }
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
},也就是不是“双击”的时候触发
* */
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
} }

2.2

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

2.3

在OnTouchListener的onTouch方法中

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

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也做了一些处理,只是没有对外提供接口。

做了什么处理:

if (mInLongPress) {
                mHandler.removeMessages(TAP);
                mInLongPress = false;

}

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

private OnTouchListener gestureTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return gDetector.onTouchEvent(event);
           case MotionEvent.ACTION_UP:
                MyGesture.FlagInfo info = mGesture.getFlagInfo();
                if(info.isConnected==true){
                    int firstVisiblePosition = mListView.getFirstVisiblePosition();
                    View view = mListView.getChildAt(info.position-firstVisiblePosition);
                    if(view!=null){
                        view.setBackgroundResource(R.drawable.listitem_background_blue);
                        info.isConnected = false;
                    }
                }
                return gDetector.onTouchEvent(event);
            case MotionEvent.ACTION_MOVE:
                return gDetector.onTouchEvent(event);
            }
            return false;
            
        }
    };

总结:

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

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

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

. 长按事件

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

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

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

.拖动

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

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

GestureDetector和SimpleOnGestureListener的使用教程的更多相关文章

  1. 【Andorid------手势识别】GestureDetector和SimpleOnGestureListener的使用教程(转)——

    FROM:http://www.cnblogs.com/transmuse/archive/2010/12/02/1894833.html 1. 当用户触摸屏幕的时候,会产生许多手势,例如down,u ...

  2. Android触控屏幕Gesture(GestureDetector和SimpleOnGestureListener的使用教程)

    1.当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vie ...

  3. Android触控屏幕Gesture(GestureDetector和SimpleOnGestureListener的使用教程) 分类:Androidandroid实例

    1.当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vie ...

  4. Android L(5.0)源码之手势识别GestureDetector

    本人新手,最近下了Android L的源码,正在研究手势识别,能力有限,现总结如下: Android识别触摸屏手势使得用户体验大大提高.在View类中有个View.OnTouchListener内部接 ...

  5. Android手势监听类GestureDetector的使用

    在使用自定义视图的时候,对触屏事件的处理是比不可少的,有能力的可以自己写代码处理,这样更加的灵活.如果不想这么麻烦,Android提供了一个手势监听类GestureDetector,可以供我们使用.G ...

  6. Android 手势&触摸事件 MotionEvent

    1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...

  7. Android 手势&触摸事件

    在刚开始学Android的时候,就觉得Google的文档不咋样,在研究手势时,更加的感觉Google的文档写得实在是太差了.很多常量,属性和方法,居然连个描述都没有. 没有描述也就罢了,但是OnGes ...

  8. GridView实现拖拽排序以及数据交互

    在研究项目中的一个效果的时候,查找资料过程中发现有人有这么一种需求,就是GridView在实现拖拽排序的基础上,如果是两个GridView之间实现拖拽效果,并要实现数据交互. 一.效果图: 实现这个效 ...

  9. 手势识别官方教程(6)识别拖拽手势用GestureDetector.SimpleOnGestureListener和onTouchEvent

    三种现实drag方式 1,在3.0以后可以直接用 View.OnDragListener (在onTouchEvent中调用某个view的startDrag()) 2,onTouchEvent()  ...

随机推荐

  1. windows driver inf文件

    原来修改了inf文件会导致签名过的驱动包哈希值不正确了啊.现在才知道. = = http://www.chiphell.com/thread-827956-1-1.html

  2. java内存模型和线程

    概述 多任务的处理在现在的计算机中可以说是"标配"了,在许多的情况下,让计算机同时做几件事情,不仅是因为计算机的运算能力的强大,还有一个重要的原因是:cpu的运算速度和计算机的存储 ...

  3. (一)一个简单的Web服务器

    万丈高楼平地起,首先我们必须了解 超文本传输协议(HTTP) 以后才能够比较清晰的明白web服务器是怎么回事. 1. 浅析Http协议 HTTP是一种协议,允许web服务器和浏览器通过互联网进行来发送 ...

  4. Javascript:字符串分割split()妙用

    概述: split() 方法将字符串分割为字符串数组,并返回此数组 语法格式: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...

  5. mysql下用户和密码生成管理

    应用上线,涉及到用户名和密码管理,随着上线应用的增加,用户名和密码的管理设置成为一个问题.还要对用户赋权,于是想着写一个脚本来管理,看到同事写的一个脚本,满足需求.思路大致是字母替换为数字,账号根据库 ...

  6. POJ 1322 Chocolate

    Chocolate Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8245   Accepted: 2186   Speci ...

  7. windows下体验Redis

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  8. [转]使用Composer管理PHP依赖关系

    简介 现在软件规模越来越大,PHP项目的开发模式和许多年前已经有了很大变化.记得初学PHP那会儿,boblog是一个很好的例子,几乎可以代表 PHP项目的开发模式.当时PHP 5.x以上的版本刚开始流 ...

  9. java 图片 批量 压缩 +所有压缩

    /* oldsrc  : 原图片地址目录 如 'd:/'    newsrc  : 压缩后图片地址目录 如 'e:/'    widthdist,heightdist : 压缩后的宽和高       ...

  10. install samba on crux without net.

    1. 解压文件 tar -xvfz samba-.tar.gz 2. 进入目录 cd samba- 3. 配置 ./configure 4. 编译 make 5. 安装 make install