<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_gesturedetector.MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>

源代码:

package com.example.android_gesturedetector;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
/*
* 使用GestureDetector进行手势识别
* 手势交互过程(原理):
* 1.触屏一刹那,触发MotionEvent事件(移动事件)
* 2.被OnTouchListener监听,在onTouch()中获得MotionEvent对象
* 3.GestureDetector(手势识别器)转发MotionEvent对象至OnGestureListener
* 4.OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈
*
*
*
* MotionEvent:
* 1.用于封装手势,触摸笔,轨迹球等动作事件
* 2.内部封装用于记录横轴和纵轴坐标的属性x和y
*
* GestureDetector:(手势识别器)
* 识别各种手势
* 工作原理:
* 1.当接收到用户触摸消息时 ,将消息交给GestureDetector加工
* 2.通过设置监听器获得GestureDetector处理后的手势
* GestureDetector提供了两个接口
* OnGestureListener:(是被单击事件)
* 1.手势交互的监听接口,其提供多个抽象方法
* 2.根据GestureDetector的手势识别结果调用相对应的方法
* OnDoubleTapListener.
* SimpleOnGeistureListener实现了上面两种接口
* 1.继承SimpleOnGestureListener
* 2.可以只重载感兴趣的手势
*/
public class MainActivity extends Activity {
private ImageView image;
private GestureDetector myGestureDetector;
class MyGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if(e1.getX()-e2.getX()>50){
Toast.makeText(MainActivity.this, "从右往左滑", Toast.LENGTH_SHORT).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"从左往右滑",Toast.LENGTH_SHORT).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
myGestureDetector = new GestureDetector(new MyGestureListener());//实例化GestureDetector
image.setOnTouchListener(new OnTouchListener() { @Override//可以捕获到触摸屏幕发生的event事件
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myGestureDetector.onTouchEvent(event);//GestureDetector对象通过onTouchEvent方法将event事件转发给GestureListener接口的实现类
return true;
}
}); } }

Android_GestureDetector的更多相关文章

  1. 转:GestureDetector: GestureDetector 基本使用

    Gesture在 ViewGroup中使用 GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等. 使用GestureDetector分三步: 1. 定义GestureDete ...

  2. maven dependendency

    登录|注册     zhengsj的专栏       目录视图 摘要视图 订阅 [公告]博客系统优化升级     [收藏]Html5 精品资源汇集     博乐招募开始啦       Maven De ...

随机推荐

  1. Protobuf语言指南

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...

  2. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  3. matlab 函数说明—ordfilt2

    今天看harris角点实现的源码,在某一个版本中看到了这个函数,不是很理解,doc ordfilt2之后还是不清楚,终于在matlab论坛上搞清楚了ordfilt2的功能.   中文理解函数名就是顺序 ...

  4. [Objective-c 基础 - 2.3] 继承、组合

    A.继承的实现: is a 1.不允许子类.父类存在相同的成员变量 2.方法调用优先原则:子类调用方法的时候,优先从本类寻找,若无则向上在父类中寻找 3.重写:子类重新实现父类的方法,覆盖父类之前的方 ...

  5. 谷歌下设置滚动条的css样式

    .oLi-lists-scroll::-webkit-scrollbar { width:5px;  padding:1px; background:url(../images/repeat-bar. ...

  6. 【Stage3D学习笔记续】山寨Starling(一):从事件说起

    我在GitHub上新开了一个项目:https://github.com/hammerc/hammerc-study-Stage3D 山寨的Starling版本我取名叫做Scorpio2D,以后的笔记中 ...

  7. IOS 支付

    支付:http://www.cnblogs.com/wangerxiansheng/p/4498586.htmlhttp://www.cnblogs.com/iCocos/p/4598548.html ...

  8. 用MyGeneration模板生成NHibernate映射文件和关系

    用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many) MyGeneration的几个NHibernate模 ...

  9. 如何在线缩小jpg图片的大小

    直接使用在线PS保存成更小格式,即可! 在线PS网址:http://www.webps.cn/ 打开图片 点击保存 直接拖动滑动条就可以改变图片大小

  10. iOS 限制textField输入的长度

    1.电话号码(带3-3-4效果) //指定代理 self.phoneTextField.delegate = self; //当编辑改变的时候,进行字符校验 [self.phoneTextField ...