GestureDetector和SimpleOnGestureListener的使用教程

原文:http://www.cnblogs.com/transmuse/archive/2010/12/2.html

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;
        }
        /**
         * 这个方法不同于onSingleTapUp,他是在GestureDetector确信用户在第一次触摸屏幕后,没有紧跟着第二次触摸屏幕,也就是不是“双击”的时候触发
         * */
        @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,到此为止就结束了.

3.案例:

package com.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class Res extends Activity implements View.OnTouchListener {
    Button btn = null;
    private GestureDetector mGestureDetector = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnTouchListener(this);
        mGestureDetector = new GestureDetector(this, new LearnGestureListener());
    }

    public boolean onTouch(View view, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent ev) {
            Log.d("DEBUG","onSingleTapUp");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent ev) {
            Log.d("DEBUG","onShowPress");
        }

        @Override
        public void onLongPress(MotionEvent ev) {
            Log.d("DEBUG","onLongPress");
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            Log.d("DEBUG","onScroll");
            return true;
        }

        @Override
        public boolean onDown(MotionEvent ev) {
            Log.d("DEBUG","onDownd");
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            Log.d("DEBUG","onFling");
            return true;
        }
        public boolean onDoubleTap(MotionEvent event){
            Log.d("DEBUG","onDoubleTap");
            return true;
        }
    }

}

4.遇到的问题:
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;

        }
    };

总结:

1. 点击屏幕上的某项的执行流程 有两种情况,一种是时间很短,一种时间稍长
时间很短:onDown——–》onSingleTapUp——–》onSingleTapConfirmed
时间稍长:onDown——–》onShowPress——》onSingleTapUp——–》onSingleTapConfirmed
2. 长按事件
onDown——–》onShowPress——》onLongPress
3.抛:手指触动屏幕后,稍微滑动后立即松开
onDown—–》onScroll—-》onScroll—-》onScroll—-》………—–>onFling
4.拖动
onDown——》onScroll—-》onScroll——》onFiling
注意:有的时候会触发onFiling,但是有的时候不会触发,个人理解是人的动作不标准所致。

时间: 2024-08-03 06:49:16

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

面向大众的移动技术:签名,封装和发布Android app

作者: Andrew Glover 原文地址 译者:Ahaha  校对:赵峰 面向大从的移动打桩其它四篇文章地址(校对添加): (一).android简介: (二).轻轻一划,在android中为手势编码: (三).Android 应用程序生命周期中的活动与图标: (四).Overheard Word 的单词和手势. 添加一个多选择quiz到你的Android手机app,然后用一个安全数字证书签名 用网络逻辑,内容为王.但是对与手机用户来说,交互规则才是王道.对移动app静态信息设计在减少,并且

浅析GestureDetector

原文:http://www.cnblogs.com/xirihanlin/archive/2010/12/29/1920356.html 最近在研究场景切换的动画效果,其中需要用到三连击的动作触发.三连击,即点三下屏幕,但意义上是双击效果. 因此,我需要研究如何识别三连击的动作. 我们知道,一般的View只能响应点击(Click)和长按(LongPress)事件.这是因为View里只暴露了这些listener给我们使用.而实质上,View是在onTouchEvent(MotionEvent ev

Android UI基础教程 目录

  从csdn下载了这本英文版的书之后,又去京东搞了一个中文目录下来.对照着看. 话说,这本书绝对超值.有money的童鞋看完英文版记得去买中文版的~~ Android UI基础教程完整英文版 pdf+源码  百度网盘下载地址:http://pan.baidu.com/share/link?shareid=1905104804&uk=436295647  对照中文目录如下,英文不好的,建议开一个词典划词翻译着看.我觉得应该没问题.毕竟代码很易懂,文字就靠翻译了~~实在觉得吃不消,去当当或者京东买

Android学习教程之圆形Menu菜单制作方法(1)_Android

本文实例为大家分享了Android圆形菜单的使用方法,供大家参考,具体内容如下 MainActivity.java代码: package siso.handlerdemo; import android.app.NotificationManager; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.vi

Android GestureDetector手势滑动使用实例讲解_Android

Gesture在 ViewGroup中使用 GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等. 使用GestureDetector分三步: 1. 定义GestureDetector类 2. 初始化手势类,同时设置手势监听 3. 将touch事件交给gesture处理 先来了解一下如何使用,后面会有示例: package com.example.y2222.myview; import android.content.Context; import android.ut

自定义View系列教程01--常用工具介绍

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Android多分辨率适配框架(3)- 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View

Android 中文API (91) —— GestureDetector

前言 本章内容是android.view.GestureDetector,版本为Android 2.3 r1,翻译来自"Haiya 胡蝶",再次感谢"Haiya 胡蝶" !期待你一起参与Android中文API的翻译,联系我over140@gmail.com.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android中文翻译组:http://goo.gl/6vJQl   正文 一.结构 public cl

android使用gesturedetector手势识别示例分享_Android

复制代码 代码如下: public class MyGestureLintener extends SimpleOnGestureListener {private Context context;public MyGestureLintener(Context context) {    super();    this.context = context;} // 单击,触摸屏按下时立刻触发/*@Overridepublic boolean onDown(MotionEvent e) {  

Android GestureDetector手势滑动使用实例讲解

Gesture在 ViewGroup中使用 GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等. 使用GestureDetector分三步: 1. 定义GestureDetector类 2. 初始化手势类,同时设置手势监听 3. 将touch事件交给gesture处理 先来了解一下如何使用,后面会有示例: package com.example.y2222.myview; import android.content.Context; import android.ut