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. document.body.clientWidth vs document.documentElement.clientWidth

    http://blog.csdn.net/zb0567/article/details/7182391 mobile web javascript library http://www.gbin1.c ...

  2. Strongly connected

    hdu4635:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个有向图,然后问你最多可以加多少条边,是的原图不是一个强连通图. 题解:这一题 ...

  3. 加快AndroidStudio运行速度的方法

    之前用过其他人加速AndroidStudio构建速度的方法,确实在编译时有一定的效果 但是在实际使用中,随着项目越来越大,AndroidStudio有时还是会卡死,或者直接黑屏,我的笔记本是8g内存 ...

  4. LeetCode解题报告:Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...

  5. 最棒的Visual Studio扩展

    isual Studio是微软公司推出的开发环境,Visual Studio可以用来创建Windows平台下的Windows应用程序和网络应用程序,也可以用来创建网络服务.智能设备应用程序和Offic ...

  6. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  7. java多线程编程(2)交替输出数字和字母

    mark一下,不停的看看notify和wait的没有理解 class Printer { int index=0; //输出奇数 public synchronized void printA(int ...

  8. CSS 概览(CSS2.1)更新时间2014-0406

    CSS和HTML结合 <link rel="stylesheet" type="text/css" href="sheet1.css" ...

  9. MySQL生僻字(不常用字)的完整解决方案

    查看 MySQL 数据库服务器和数据库字符集 show variables like '%char%'; 查看 MySQL 数据表(table) 的字符集 show table status from ...

  10. lightoj 1027 简单概率dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 #include<cstdio> #include<cstri ...