参考:

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. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  2. MVC3 Model Binding验证方式

    1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...

  3. 2013 Asia Regional Changchun

    Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 #include<cstdio> ]; int main(){ int t ...

  4. asp.net 获取客户机IP地址

    /// <summary> ///get client IP /// </summary> /// <returns></returns> public ...

  5. hdu 4187 Alphabet Soup

    这题的主要就是找循环节数,这里用找字符串最小覆盖来实现,也就是n-next[n],证明在这http://blog.csdn.net/fjsd155/article/details/6866991 #i ...

  6. springmvc图片上传

    //-------------------------------------上传图片--------------------------------------------------- @Requ ...

  7. JavaScript DOM编程基础精华01(DOM入门,DOM模型和获取页面元素,事件,window对象的方法)

    DOM入门 DOM就是Html页面的模型,将每个标签都做为一个对象,JavaScript通过调用DOM中的属性.方法就可以对网页中的文本框.层等元素进行编程控制.比如通过操作文本框的DOM对象,就可以 ...

  8. hibernate 联合主键生成机制(组合主键XML配置方式)

    hibernate 联合主键生成机制(组合主键XML配置方式)   如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...

  9. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  10. 01-语言入门-01-A+B Problem

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=1    描述 此题为练手用题,请大家计算一下a+b的值   输入 输入两个数,a,b 输 ...