为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:

boolean  onDoubleTap(MotionEvent e) 解释:双击的第二下Touch down时触发 boolean  onDoubleTapEvent(MotionEvent e)

解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。 boolean  onDown(MotionEvent e)

解释:Touch down时触发 boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

解释:Touch了滑动一点距离后,up时触发。 void  onLongPress(MotionEvent e)

解释:Touch了不移动一直Touch down时触发 boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

解释:Touch了滑动时触发。 void  onShowPress(MotionEvent e)

解释:Touch了还没有滑动时触发 (与onDown,onLongPress比较 onDown只要Touch down一定立刻触发。 而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。 所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。

boolean  onSingleTapConfirmed(MotionEvent e) boolean  onSingleTapUp(MotionEvent e)

解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

点击一下非常快的(不滑动)Touchup: onDown->onSingleTapUp->onSingleTapConfirmed 点击一下稍微慢点的(不滑动)Touchup: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

使用GestureDetector需要在View中重写onTouchEvent事件,例如:

GestureDetector mGesture = null;

@Override   public boolean onTouch(View v, MotionEvent event)   {

// TODO Auto-generated method stub    return mGesture.onTouchEvent(event);

}

详细的测试例子如下: package com.jiubang.android.gesturetest;

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.view.GestureDetector.SimpleOnGestureListener;

import android.view.View.OnTouchListener;

import android.widget.Button;

public class GestureActivity extends Activity    implements OnTouchListener {

private Button mButton = null;  GestureDetector mGesture = null;

/** Called when the activity is first created. */

@Override     public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Log.i("TEST", "onCreate");

mButton = (Button)findViewById(R.id.button1);

mButton.setOnTouchListener(this);

mGesture = new GestureDetector(this, new GestureListener());

}

@Override  public boolean onTouch(View v, MotionEvent event)  {

// TODO Auto-generated method stub   return mGesture.onTouchEvent(event);

}    class GestureListener extends SimpleOnGestureListener  {

@Override   public boolean onDoubleTap(MotionEvent e)   {

// TODO Auto-generated method stub    Log.i("TEST", "onDoubleTap");    return super.onDoubleTap(e);   }

@Override   public boolean onDown(MotionEvent e)   {

// TODO Auto-generated method stub    Log.i("TEST", "onDown");    return super.onDown(e);

}

@Override   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,     float velocityY)   {

// TODO Auto-generated method stub    Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);    return super.onFling(e1, e2, velocityX, velocityY);

}

@Override   public void onLongPress(MotionEvent e)   {

// TODO Auto-generated method stub    Log.i("TEST", "onLongPress");    super.onLongPress(e);   }

@Override   public boolean onScroll(MotionEvent e1, MotionEvent e2,     float distanceX, float distanceY)   {

// TODO Auto-generated method stub    Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);    return super.onScroll(e1, e2, distanceX, distanceY);

}

@Override   public boolean onSingleTapUp(MotionEvent e)   {

// TODO Auto-generated method stub    Log.i("TEST", "onSingleTapUp");    return super.onSingleTapUp(e);

}

}

}

GestureDetector.OnGestureListener的更多相关文章

  1. 关于GestureDetector.OnGestureListener的onScroll参数distance问题

    关于GestureDetector.OnGestureListener类的onScroll方法参数distanceX和distanceY问题 看到有文章上说onScroll方法中distanceX和d ...

  2. android学习——GestureDetector.OnGestureListener 详解

    Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等.这些Gesture会使用户体验大大提升.An ...

  3. 手势GestureDetector.OnGestureListener事件的调起

    @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionE ...

  4. 说说GestureDetector.OnGestureListener onScroll函数

    public abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) ...

  5. android GestureDetector 手势基础

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

  6. Android 手势识别类 ( 一 ) GestureDetector 基本介绍

    为了加强鼠标响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Ta ...

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

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

  8. Android GestureDetector方法详解

    为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...

  9. OnTouchListener事件监听实现方式之GestureDetector

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

随机推荐

  1. 无法读取配置节“protocolMapping”,因为它缺少节声明

    无法读取配置节“protocolMapping”,因为它缺少节声明 1.正常情况   :  Web.config文件中有protocolMapping节点, 发现在IIS部署时使用了.NET 2.0的 ...

  2. Loadrunner:error -86401 Failed to connceted xxx.xxx.xxx.xxx:25问题解决

    [转自:http://www.51testing.com/html/00/130600-3578408.html]windows 2003上安装的LoadRunner11做为负载机在SMTP协议压测时 ...

  3. leetcode 8

    string类型转换为int类型,需要考虑不同的转换情况. “   04”  转换结果   4: “   4   43”  转换结果  4: “a@12 ”   转换结果    0: “12a”   ...

  4. 一个简单的SpringMVC3 程序

    初学者对于Spring框架的难度:引用Jar包不全,或者不正确: 1.运行界面 2.客户端页面 index.jsp 的代码 <%@ page language="java" ...

  5. Android IOS WebRTC 音视频开发总结(五五)-- 音视频通讯中的抗丢包与带宽自适应原理

    本文主要分析webrtc中的抗丢包与带宽自适应原理,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help 文章内容主要来自中国电信北京研究院丁博士在 ...

  6. 配置DNS域名解析服务器

    bind这个DNS域名解析服务器解析好后,执行下面的语句实现开启服务 named -c named.conf & -c指配置脚本named.conf的文件地址 named.conf主要有下面几 ...

  7. gdb调试工具使用方法分享

    刚才看了一个CSDN上分享gdb调试工具使用的教程,讲得非常好,推荐到这里: http://blog.csdn.net/liigo/article/details/582231

  8. php实现文件上传的源码

    php实现文件上传的源码,更多php技术开发就去php教程网,http://php.662p.com <?php ##author :Androidyue ##sina @androidyue ...

  9. 空间session失效的解决方法

    今天访问自己的网站的时候(by thinkphp),突然发现身份验证失效了,Session无法跨页,而且登陆的时候总是提示验证码错误(验证码也是通过Session传递的),才意识到可能是Session ...

  10. css中li、a、span行内强制不换行

    li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后