手势交互之GestureDetector
GsetureDetector
一.交互过程
- 触屏的一刹那,触发MotionEvent事件
- 被OnTouchListener监听,在onTouch()中获得MotionEvent对象
- GestureDetector转发MotionEvent对象到OnGestureListener
- OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈
二.内部类:
1.OnGestureListener:单击类
方法
- 单击onDown(MotionEvent e)
- 抬起onSingleTapUp(MotionEvent e)
- 短按onShowPress(MotionEvent e)
- 长按onLongPress(MotionEvent e)
- 滚动onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
- 滑动onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)
2.OnDoubleTapListener:双击
方法
- 双击OnDoubleTap(MotionEvent e)
- 双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)?应用例子?
- 单击确定onSingleTapConfirmed(MotionEvent e)?
3.SimpleOnGestureListener
概述: SimpleOnGestureListener实现了上面的连个接口 OnGestureListener and OnDoubleTapListener,可以通过继承这个类来实现你所想实现的手势交互动作。需要的动作可以在接口里找对应的方法,如果方法返回false就是什么都不做。
三.运用例子
效果
这个例子在主界面加载一个图片,然后左右滑动提示相应的文字。
过程
- OnTouchListener监听,在onTouch中获得MotionEvent对象
img.setOnTouchListener(new OnTouchListener() {
@Override//捕获的触摸屏发生的event事件
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;//记得这里改为ture
}
});
- 在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的更多相关文章
- 基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统)
基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统) zouxy09@qq.com http://blog.csdn.net/zouxy09 一年多前开始接触计算机视觉这个领域的时候,年幼无 ...
- 手势监听GestureDetector 案例
以下只做长按和甩出(用户按下朝某一方向甩动手指)案例 OnGestureListener可以查看到更多的手势事件 案例 package com.qf.mobliesafe.activity; impo ...
- 【手势交互】4. Kinect for XBox
"You are the Controller",Kinect for Xbox的广告词.明白说明了Kinect体感的交互方式.作为一款集成了诸多先进视觉技术的自然交互设备,Kin ...
- 【手势交互】8. Nimble Sense
早在2012.2013年度,手势识别领域就发生了几起重要的行业并购.比方英特尔对Omek的收购,谷歌对Flutter的收购.而苹果公司更是以3.45亿美金得高价收购了之前为微软Kinect提供技术支持 ...
- iOS查看3D效果的手势交互
公司项目中用到的,仿的人家厂子的效果,看起来还是挺高大上的,其实实现起来很简单,是一种伪3D;用手势滑动查看一个商品的3D展示. 在手机上手指左右滑动可以360°无死角查看这个商品,有兴趣的可以下de ...
- 手势交互之GestureOverlayView
一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...
- 【手势交互】6. 微动VID
中国 天津 http://www.sharpnow.com/ 微动VID是天津锋时互动科技有限公司开发的中国Leap Motion. 它能够识别并跟踪用户手部的姿态.包含:指尖和掌心的三维空间位置:手 ...
- 【手势交互】9. PS Move
索尼研发体感控制技术已有10年,在过去那么多年里.尝试了3D摄像头.超声波和电磁感应等各种技术.最后还是觉得眼下的MOVE所使用的技术最为合适.PS Move是索尼于2010年9月份推出.用来让PS3 ...
- iOS7自定义back按钮和pop交互手势
Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ...
随机推荐
- Codeforces Round #207 (Div. 2)
A:超级大水题: 代码: #include<cstdio> #define maxn 105 using namespace std; int n,a[maxn],x,y,ans; int ...
- iOS9 App Thinning(应用瘦身)功能介绍
iOS9 发布后,产生了一个使 App Thinning 无法正常运行的 bug.在iOS9.0.2 版本中,这个 bug 已经被修复,App Thinning 已经可以正常使用.当你从应用商店(Ap ...
- zookeeper kazoo Basic Usage
http://kazoo.readthedocs.org/en/latest/basic_usage.html Basic Usage Connection Handling To begin usi ...
- 重载VerifyRenderingInServerForm
重载VerifyRenderingInServerForm 今天在做一个AjaxPro的应用时,VerifyRenderingInServerForm给我带来了麻烦,在网上找了找,发现大多数人的解决方 ...
- Delphi基本图像处理代码
//浮雕procedure Emboss(SrcBmp,DestBmp:TBitmap;AzimuthChange:integer);overload;var i, j, Gray, Azimuth ...
- 14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率:
14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率: 主线程 在InnoDB 是一个线程 执行各种任务在后台. ...
- crontab(linux下定时执行任务命令)
在linux在可以通过在脚本里(列如sh)写如日常需要进行的操作,然后通过crontab定时运行脚本. Linux下的任务调度分为两类,系统任务调度和用户任务调度. 系统任务调度:系统周期性所要执行的 ...
- OA请假流程 -- 编码
OA请假流程 -- 编码 凡是内容不会发生变化的,都要写在xml配置文件中.需要定义如下内容: <process>标签 id英文命名 和 name 中文命名,然后只要与该流程相关的资源均以 ...
- Linq中小心使用IndexOf
我们平常在做字符串的模糊查询时,有可能会用到下面的类似LINQ写法: string.IsNullOrEmpty(_SN) ? true : a.SN.IndexOf(_SN) != -1 这条 ...
- 4种字符串匹配算法:BS朴素 Rabin-karp(上)
字符串的匹配的算法一直都是比较基础的算法,我们本科数据结构就学过了严蔚敏的KMP算法.KMP算法应该是最高效的一种算法,但是确实稍微有点难理解.所以打算,开这个博客,一步步的介绍4种匹配的算法.也是& ...