GsetureDetector

一.交互过程

  1. 触屏的一刹那,触发MotionEvent事件
  2. 被OnTouchListener监听,在onTouch()中获得MotionEvent对象
  3. GestureDetector转发MotionEvent对象到OnGestureListener
  4. OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈

二.内部类:

1.OnGestureListener:单击类

方法
  1. 单击onDown(MotionEvent e)
  2. 抬起onSingleTapUp(MotionEvent e)
  3. 短按onShowPress(MotionEvent e)
  4. 长按onLongPress(MotionEvent e)
  5. 滚动onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
  6. 滑动onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)

2.OnDoubleTapListener:双击

方法
  1. 双击OnDoubleTap(MotionEvent e)
  2. 双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)?应用例子?
  3. 单击确定onSingleTapConfirmed(MotionEvent e)?

3.SimpleOnGestureListener

概述: SimpleOnGestureListener实现了上面的连个接口 OnGestureListener and OnDoubleTapListener,可以通过继承这个类来实现你所想实现的手势交互动作。需要的动作可以在接口里找对应的方法,如果方法返回false就是什么都不做。

三.运用例子

效果

这个例子在主界面加载一个图片,然后左右滑动提示相应的文字。

过程
  1. OnTouchListener监听,在onTouch中获得MotionEvent对象
img.setOnTouchListener(new OnTouchListener() {
@Override//捕获的触摸屏发生的event事件
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;//记得这里改为ture
}
});
  1. 在Activity建立一个继承于SimpleOnGestureListener的内部类
class MyGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
//e1和e2分别是开始和结束的动作对象
//通过e1和e2位置的比较判断手势的动作
if (e1.getX() - e2.getX() > 50) {
Toast.makeText(MainActivity.this, "从右向左滑动",0).show();
}else if(e2.getX() - e1.getX() >50){
Toast.makeText(MainActivity.this, "从左向右滑动",0).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}


3.在onCreate方法中获得一个GestureDetextor对象(声明省略了)

    gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());

4.在onTouch方法中gestureDetector转发MotionEvent对象到OnGestureListener(这里是MyGestureListener)

    gestureDetector.onTouchEvent(event);

5.MyGestureListener获得该对象,根据该对象封装的信息做出合适的反馈(在图片上左右滑动提示对应的文字)

完整代码

public class MainActivity extends Activity {
ImageView img;
GestureDetector gestureDetector;
//3.这就是继承于一个SimpleOnGestureListener来利用对应的方法
//这里演示了一个滑动的手势效果
class MyGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
//e1和e2分别是开始和结束的动作对象
//通过e1和e2位置的比较判断手势的动作
if (e1.getX() - e2.getX() > 50) {
Toast.makeText(MainActivity.this, "从右向左滑动",0).show();
}else if(e2.getX() - e1.getX() >50){
Toast.makeText(MainActivity.this, "从左向右滑动",0).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
//4.获得gestureDetector对象
gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());
//1.交互过程第一部是用户点击触发了MotionEvent时间
//2.下面是第二步,OnTouchListener监听,在onTouch中获得MotionEvent对象
img.setOnTouchListener(new OnTouchListener() { @Override//捕获的触摸屏发生的event事件
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//5.gestureDetector转发MotionEvent对象到OnGestureListener(这里是MyGestureListener)
gestureDetector.onTouchEvent(event);
//这里要改为true
return true;
}
}); }
}

手势交互之GestureDetector的更多相关文章

  1. 基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统)

    基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统) zouxy09@qq.com http://blog.csdn.net/zouxy09 一年多前开始接触计算机视觉这个领域的时候,年幼无 ...

  2. 手势监听GestureDetector 案例

    以下只做长按和甩出(用户按下朝某一方向甩动手指)案例 OnGestureListener可以查看到更多的手势事件 案例 package com.qf.mobliesafe.activity; impo ...

  3. 【手势交互】4. Kinect for XBox

    "You are the Controller",Kinect for Xbox的广告词.明白说明了Kinect体感的交互方式.作为一款集成了诸多先进视觉技术的自然交互设备,Kin ...

  4. 【手势交互】8. Nimble Sense

    早在2012.2013年度,手势识别领域就发生了几起重要的行业并购.比方英特尔对Omek的收购,谷歌对Flutter的收购.而苹果公司更是以3.45亿美金得高价收购了之前为微软Kinect提供技术支持 ...

  5. iOS查看3D效果的手势交互

    公司项目中用到的,仿的人家厂子的效果,看起来还是挺高大上的,其实实现起来很简单,是一种伪3D;用手势滑动查看一个商品的3D展示. 在手机上手指左右滑动可以360°无死角查看这个商品,有兴趣的可以下de ...

  6. 手势交互之GestureOverlayView

    一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...

  7. 【手势交互】6. 微动VID

    中国 天津 http://www.sharpnow.com/ 微动VID是天津锋时互动科技有限公司开发的中国Leap Motion. 它能够识别并跟踪用户手部的姿态.包含:指尖和掌心的三维空间位置:手 ...

  8. 【手势交互】9. PS Move

    索尼研发体感控制技术已有10年,在过去那么多年里.尝试了3D摄像头.超声波和电磁感应等各种技术.最后还是觉得眼下的MOVE所使用的技术最为合适.PS Move是索尼于2010年9月份推出.用来让PS3 ...

  9. iOS7自定义back按钮和pop交互手势

    Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ...

随机推荐

  1. Reason: image not found 错误解决方法

    dyld: Library not loaded: /System/Library/Frameworks/Social.framework/Social   Referenced from: /var ...

  2. SecureCRT 颜色

    默认的情况下,SecureCRT 是没有颜色方案的. 也就是说:用vim,你是看不到色彩显示效果,用ll 文件和文件夹也不会有颜色区别.  那如何支持颜色显示呢?方法如下:  www.2cto.com ...

  3. mysql之索引方面的知识点总结

    索引的类型: 普通索引:这是最基本的索引类型,没唯一性之类的限制. 唯一性索引:和普通索引基本相同,但所有的索引列只能出现一次,保持唯一性. 主键:主键是一种唯一索引,但必须指定为"PRIM ...

  4. C语言嵌入式系统编程修炼之二:软件架构篇

    模块划分的"划"是规划的意思,意指怎样合理的将一个很大的软件划分为一系列功能独立的部分合作完成系统的需求.C语言作为一种结构化的程序设计语言,在模块的划分上主要依据功能(依功能进行 ...

  5. android Process.killProcess 和 System.exit(0) 区别

    1 Process.killProcess  和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...

  6. ibatas的一些实例及解释

    Student.xml : <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPE sqlMap PUB ...

  7. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  8. PuTTY 'modmul()' 函数缓冲区下溢漏洞(CVE-2013-4206)

    漏洞版本: PuTTY 0.52 - 0.63 漏洞描述: BUGTRAQ ID: 61645 CVE(CAN) ID: CVE-2013-4206 PuTTY是Windows和Unix平台上的PuT ...

  9. mkfs 的使用

    使用方法: [root@localhost beinan]# mkfs -t 文件系统  存储设备 注:这里的文件系统是要指定的,比如 ext3 :reiserfs :ext2 :fat32 :msd ...

  10. codeforces --- 115A

    A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...