参考:

http://blog.csdn.net/qq418716640/article/details/8508973
http://www.cnblogs.com/mengdd/p/3335508.html

效果:一个手指实现(所有手势事件)和(部分事件的);

A. 所有手势

activity_main.xml

<TextView
android:id="@+id/gesture"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:text="单击手势变化" /> <TextView
android:id="@+id/doubleTap"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:text="双击手势变化" />

MainActivity.java

public class MainActivity extends Activity {

    private static final String LOG_TAG = "HelloGesture";
private GestureDetector mGestureDetector = null;
private TextView mGestureTextView = null;
private TextView mDoubleTapTextView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureTextView = (TextView) findViewById(R.id.gesture);
mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap);
// 构造GestureDetector对象,传入监听器对象
mGestureDetector = new GestureDetector(this, mOnGestureListener);
// 传入双击监听器对象
mGestureDetector.setOnDoubleTapListener(mDoubleTapListener); } @Override
public boolean onTouchEvent(MotionEvent event) {
// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
} private OnGestureListener mOnGestureListener = new OnGestureListener() { @Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i(LOG_TAG, "onSingleTapUp: " + e.toString());
mGestureTextView.setText("onSingleTapUp: ");
return false; } @Override
public void onShowPress(MotionEvent e) {
Log.i(LOG_TAG, "onShowPress: " + e.toString());
mGestureTextView.setText("onShowPress: ");
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i(LOG_TAG, "onScroll: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText("onScroll ");
return false;
} @Override
public void onLongPress(MotionEvent e) {
Log.i(LOG_TAG, "onLongPress: " + e.toString());
mGestureTextView.setText("onLongPress: ");
} @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i(LOG_TAG, "onFling: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText("onFling ");
return false;
} @Override
public boolean onDown(MotionEvent e) { Log.i(LOG_TAG, "onDown: " + e.toString());
mGestureTextView.setText("onDown: "); return false; }
};
private OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener() {

        @Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("LOG_TAG", "onSingleTapConfirmed: " + e.toString());
mDoubleTapTextView.setText("onSingleTapConfirmed: ");
return false;
} @Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i("LOG_TAG", "onDoubleTapEvent: " + e.toString());
mDoubleTapTextView.setText("onDoubleTapEvent: ");
return false;
} @Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("LOG_TAG", "onDoubleTap: " + e.toString());
mDoubleTapTextView.setText("onDoubleTap: ");
return false;
}
};
}

B. 部分手势

如果你仅仅只想处理几种手势,你可以选择继承GestureDetector.SimpleOnGestureListener类,而不是实现
GestureDetector.OnGestureListener接口

MainActivity.java


public class MainActivity extends Activity {

    private GestureDetector mGestureDetector = null;
private TextView mGestureTextView = null;
private TextView mDoubleTapTextView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureTextView = (TextView) findViewById(R.id.gesture);
mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap);
// 构造GestureDetector对象,传入监听器对象
mGestureDetector = new GestureDetector(this, new MyGestureListener());
// 传入双击监听器对象 } @Override
public boolean onTouchEvent(MotionEvent event) {
// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的
this.mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String DEBUG_TAG = "Gestures"; @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText("onFling ");
return false;
} @Override
public boolean onDown(MotionEvent e) {
Log.d(DEBUG_TAG, "onDown: " + e.toString());
mGestureTextView.setText("onDown: ");
return false; }
} }
												

用户输入 i. 检测常用手势(一)的更多相关文章

  1. PHP用户输入安全过滤和注入攻击检测

    摘抄自ThinkPHP /** * 获取变量 支持过滤和默认值 * @param array $data 数据源 * @param string|false $name 字段名 * @param mi ...

  2. Java安全编码之用户输入

    0x00 安全引言 1.传统Web应用与新兴移动应用 (1)传统Web应用:浏览器 HTTP 服务器(2)新兴移动应用:APP HTTP 服务器 从安全角度看,传统Web应用与新兴移动应用没有本质区别 ...

  3. python学习笔记(基础二:注释、用户输入、格式化输出)

    注释 单行:# 多行:上下各用3个连续单引号或双引号 3个引号除了多行注释,还可以打印多行 举例: msg = ''' name = "Alex Li" name2 = name ...

  4. 第4章 Java接收用户输入

    第4章 Java接收用户输入 1.输入 使用Scanner工具类可以换取用户输入的数据Scanner类位于java.util包中,使用时需要导入此包使用步骤: 1.导入java.util.Scanne ...

  5. 大量无线键盘存在KeySniffer漏洞-可嗅探用户输入的内容

    几美元的一根天线.一个无线发射器,还有几行Python代码,有了这些,黑客就可以在几十米开外主动地记录下你的用户名.密码.信用卡.你写的稿子,总之就是你用无线键盘输入的任何东西. 黑客所利用的是一种无 ...

  6. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程04:技能的输入与检测》

    4.技能的输入与检测 概述: 技能系统的用户体验,制约着玩家对整个游戏的体验.游戏角色的技能华丽度,连招的顺利过渡,以及逼真的打击感,都作为一款游戏的卖点吸引着玩家的注意.开发者在开发游戏初期,会根据 ...

  7. Linux&shell之处理用户输入

    写在前面:案例.常用.归类.解释说明.(By Jim) 命令行参数$1为第一个参数,$2为第二个参数,依次类推...示例: #!/bin/bash # using one command line p ...

  8. read命令读取用户输入

    read命令用于从终端或文件中读取用户输入,它读取整行输入,如果没有指定名称,读取的行被赋值给内部变量REPLY.read命令常用选项:-a,-p,-s,-t,-n 1.REPLY变量 $readhe ...

  9. 《Linux命令行与shell脚本编程大全》第十四章 处理用户输入

    有时还会需要脚本能够与使用者交互.bash shell提供了一些不同的方法来从用户处获得数据, 包括命令行参数,命令行选项,以及直接从键盘读取输入的能力. 14.1 命令行参数 就是添加在命令后的数据 ...

随机推荐

  1. Jqgrid使用

    $('#mygrid').jqGrid('GridUnload');   //保留table元素 $('#mygrid').jqGrid('GridDestroy '); //相当于remove,移除 ...

  2. 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  3. 【BZOJ】【2157】旅游

    LCT 直到动手写拆边为点的时候才发现根本不会写……去orz了一下Hzwer(话说这题应该也用不着LCT吧……下次再换种姿势写一遍好了) /****************************** ...

  4. 字符串匹配--kmp算法原理整理

    kmp算法原理:求出P0···Pi的最大相同前后缀长度k: 字符串匹配是计算机的基本任务之一.举例,字符串"BBC ABCDAB ABCDABCDABDE",里面是否包含另一个字符 ...

  5. hdu 4901

    一个简单的dp,比赛的时候太坚信自己的小聪明没用二维数组一直WA到死: #include<cstdio> #include<cstring> #define maxn 1009 ...

  6. WinForm 资源文件的使用

    1. 创建资源文件: 2.双击资源文件,打开如下图:添加一个字符串: 名称为cnnstr 值为-- 3.添加文本文件和图像 4. 调用代码 MessageBox.Show(Resource1.cnns ...

  7. 【hadoop2.6.0】MapReduce原理

    看了几篇博文,感觉还是云里雾里的. http://blog.csdn.net/opennaive/article/details/7514146 http://www.aboutyun.com/thr ...

  8. [C++]默认构造函数

    默认构造函数(default constructor)就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有的形参提供默认实参的构造函数定义.若个定义某个类的变量时没有提供初始 ...

  9. hdu 1850 Being a Good Boy in Spring Festival 博弈论

    求可行的方案数!! 代码如下: #include<stdio.h> ]; int main(){ int n,m; while(scanf("%d",&n)&a ...

  10. Orcle数据库查询练习复习:二

    一.题目 1.找出所有成绩均低于80的学生姓名 select sname from student where sid in( ) select sname from student where si ...