<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. [转] python程序的调试方法

    qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.4

    If $\dim \scrH=3$, then $\dim \otimes^3\scrH =27$, $\dim \wedge^3\scrH =1$ and $\dim \vee^3\scrH =10 ...

  3. TPL

    namespace TPLTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...

  4. SGU132 - Another Chocolate Maniac(状态压缩DP)

    题目大意 给定一个N*M大小的大小的蛋糕,蛋糕的有些地方已经放置了东西,要求你在蛋糕上放入尽量少的1*2大小的巧克力,使得蛋糕不能够再放入巧克力 题解 和POJ1038恰好相反,此题是放入尽量少的巧克 ...

  5. Redis+MongoDB 最佳实践 做到读写分离 -摘自网络

    方案1. (被否定) 加上Redis,做到MongoDB的读写分离,单一进程从MongoDB及时把任务同步到Redis中. 看起来很完美,但是上线后出现了各种各样的问题,列举一下: 1.Redis队列 ...

  6. android中OnItemClickListener的参数解释

    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} ...

  7. nginx变量

    nginx的全局变量参数解释: $arg_PARAMETER#这个变量包含GET请求中,如果有变量PARAMETER时的值. $args   #这个变量等于请求行中(GET请求)的参数,例如foo=1 ...

  8. 在virtualbox上安装mac os mavericks遇到Missing Bluetooth Controller Transport问题解决办法

    挂载 HackBoot_Mav.iso 作为光驱,启动在 磁盘选择 界面,选择磁盘后按空格键,输入 启动参数 -v -x(-x的意思是安全模式,不这样的话,有蓝牙驱动作怪, 会输出 [IOBlueto ...

  9. hdoj 3790 最短路径问题

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  10. 备份数据表为insert 脚本

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...