一、概念

  • 手势:其实是指用户手指或触摸笔在屏幕上的连续触碰行为,Andoird对两种手势行为都提供了支持:

    • Andorid提供了手势检测,并为手势检测提供了相应的监听器;

    • Android允许开发者添加手势,并提供了相应的API识别用户手势;

二、手势检测

  • Gesture类:代表了一个手势检测器;

  • GestureDectector.OnGestureListener类:代表一个监听器、负责对用户的手势行为提供响应;

    • boolean onDown(MotionEvent e):当触碰事件按下时触发的方法;

    • boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY):当用户在触摸屏上”拖过”时触发该方法,velocityX,velocityY代表“拖过”动作的横向、纵向上的速度;

    • abstract void onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法;

    • abstract void onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float diastanceY):当用户在屏幕上“滚动”时触发该方法;

    • void onShowPress(MotionEvent e):当用户在屏幕上按下,而且还未移动和松动的时候触发该方法;

    • boolean onSingleTapUp(MotionEvent e):当用户在触摸屏上的轻击事件将会触发该方法;

三、使用步骤

  1. 创建一个GestureDetector对象,创建对象时候必须创建一个GestureDectector.OnGestureListener监听器实例;

  2. 为应用程序的Activity(偶尔也可以为特定组件)的TouchEvent事件绑定监听器,在事件处理中制定把Activity(或特定组件)上的TouchEvent事件交给GestureDetector处理;

  • 使用实例(chapter08/GestureTest)

MainActivity.java文件

  1. public class MainActivity extends Activity implements OnGestureListener {
  2. // 定义手势检测实例
  3. GestureDetector detector;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main_activity);
  8. // 创建手势检测器
  9. detector = new GestureDetector(this, this);
  10. }
  11. @Override
  12. public boolean onTouchEvent(MotionEvent event) {
  13. // 将该Activity上的触碰事件交给GestureDetector处理
  14. return detector.onTouchEvent(event);
  15. }
  16. @Override
  17. public boolean onDown(MotionEvent e) {
  18. // 触碰时间按下时触发该方法
  19. Toast.makeText(this, "OnDown", Toast.LENGTH_LONG).show();
  20. return false;
  21. }
  22. @Override
  23. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  24. float velocityY) {
  25. // 当用户在屏幕上“拖动”时触发该方法
  26. Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show();
  27. return false;
  28. }
  29. @Override
  30. public void onLongPress(MotionEvent e) {
  31. // 当用户在屏幕上长按时触发该方法
  32. Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();
  33. }
  34. @Override
  35. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
  36. float distanceY) {
  37. // 当屏幕“滚动”时触发该方法
  38. Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();
  39. return false;
  40. }
  41. @Override
  42. public void onShowPress(MotionEvent e) {
  43. // 当用户在触摸屏幕上按下、而且还未移动和松开时触发该方法
  44. Toast.makeText(this, "onShowPress", Toast.LENGTH_LONG).show();
  45. }
  46. @Override
  47. public boolean onSingleTapUp(MotionEvent e) {
  48. // 在屏幕上的轻击事件将会触发该方法
  49. Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_LONG).show();
  50. return false;
  51. }
  52. }

四、增加手势

  • Android除了提供了手势之外,还允许应用程序把用户手势(多个持续的触摸事件在屏幕上形成特定的形状)添加到制定文件中,以备以后使用;

  • GestureLibrary类:代表手势库,并提供了GestureLibraries工具来创建手势库,提供了如下4个静态方法从不同的位置加载手势:

    • static GestureLibrary from(String path):从path代表的文件中加载手势库;

    • static GestureLibrary fromFile(File path):从path代表的文件加载手势库;

    • static GestureLibrary fromPrivateFile(Context context,String name):从制定应用程序的数据文件夹中name文件中加载手势库;

    • static GestureLibrary fromRawResoure(Context context,int resourceId):从resourceId所代表的资源中加载手势库;

  • 获取GestureLibrary对象之后,该对象提供了如下方法来添加手势和识别手势:

    • void addGesture(String entryName,Gesture gesture):添加一个名为name的手势;

    • Set<String> getGestureEntries():获取手势库中所有的手势名称;

    • ArrayList<Guesture> getGestures(String entryName):获取entryName名称对应的全部手势;

    • ArrayList<Prediction> recongize(Guesture gesture):从当前手势库中识别与gesture匹配的全部手势;

    • void removeEntry(String entryName):删除手势库中entryName对应的手势;

    • void removeGesture(String entryName,Gesture gesture):删除手势库中entryName,gesture对应的手势库;

    • boolean save():当向手势库中添加手势或从中删除手势后调用该方法保存手势库;

  • 为了监听GestureOverlayView组件上的手势事件,Android为GestureOverlayView提供了OnGestureLisnter、OnGesturePerformedListener、OnGesturingListener三个监听器接口,分别用于响应手势事件开始、结束、完成、取消事件;

  • 使用实例(chapter08/AddGesture)

main_activity.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center_horizontal"
  10. android:text="请在下面屏幕上绘制手势" />
  11. <!-- android:gestureStrokeType手势是否需要一笔完成-->
  12. <android.gesture.GestureOverlayView
  13. android:id="@+id/gesture"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:gestureStrokeType="multiple" />
  17. </LinearLayout>

sava.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginRight="8dip"
  14. android:text="请输入手势名称" />
  15. <!-- 定义一个文本框让用户输入手势名 -->
  16. <EditText
  17. android:id="@+id/gesture_name"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content" />
  20. </LinearLayout>
  21. <!-- 定义一个图片框来显示手势 -->
  22. <ImageView
  23. android:id="@+id/show"
  24. android:layout_width="match_parent"
  25. android:layout_height="128dp"
  26. android:layout_marginTop="10dp"
  27. android:layout_weight="0.29" />
  28. </LinearLayout>

MainActivity.java文件

  1. public class MainActivity extends Activity {
  2. EditText editText;
  3. GestureOverlayView gestureOverlayView;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main_activity);
  8. // 使用文本编辑器
  9. editText = (EditText) findViewById(R.id.gesture_name);
  10. // 获取手势编辑视图
  11. gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);
  12. // 设置手势绘图的颜色
  13. gestureOverlayView.setGestureColor(Color.RED);
  14. // 设置手势的绘制宽度
  15. gestureOverlayView.setGestureStrokeWidth(4);
  16. // 为gesture的手势完成事件绑定事件监听器
  17. gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
  18. @Override
  19. public void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) {
  20. // 加载save.xml界面布局代表的视图
  21. View saveDialog = getLayoutInflater().inflate(R.layout.save, null);
  22. // 获取saveDialog里的show组件
  23. ImageView imageView = (ImageView) saveDialog.findViewById(R.id.show);
  24. // 获取saveDialog的gesture_name组件
  25. final EditText gestureEditText = (EditText) saveDialog.findViewById(R.id.gesture_name);
  26. // 根据Gesture包含的手势创建一个位图
  27. Bitmap bitmap = gesture.toBitmap(128, 128, 10,0xffff0000);
  28. imageView.setImageBitmap(bitmap);
  29. // 使用对话框显示saveDialog组件
  30. new AlertDialog.Builder(MainActivity.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener() {
  31. @Override
  32. public void onClick(DialogInterface dialog,int which) {
  33. // 获取制定文件对应的手势库
  34. GestureLibrary guestureLibrary = GestureLibraries.fromFile(Environment
  35. .getExternalStorageDirectory().getPath()+ "/mygestures");
  36. // 添加手势
  37. guestureLibrary.addGesture(gestureEditText.getText().toString(), gesture);
  38. guestureLibrary.save();
  39. }
  40. }).setNegativeButton("取消", null).show(); }
  41. });
  42. }
  43. }

五、识别用户手势

  • recoginze(Gesture guesture)方法:识别手势,该方法将会返回该手势库中所有与ges匹配的手势—两个手势的图形越相似,相似度越高;

  • recogniza(Gusture ges)方法返回为ArrayList<Prediction>,启动Prediction封装了手势的匹配信息,Predictin对象的name属性代表了匹配的手势名,score属性代表了手势的相似度;

  • 应用实例(/chapter08/RecognizeGesture)

main_activity.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <android.gesture.GestureOverlayView
  7. android:id="@+id/gesture"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gestureStrokeType="multiple" />
  11. </LinearLayout>

MainActivity.java文件

  1. public class MainActivity extends Activity {
  2. // 定义手机编辑组件
  3. GestureOverlayView gestureOverlayView;
  4. // 记录手机上已有的手势库
  5. GestureLibrary gestureLibrariLibrary;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main_activity);
  10. gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);
  11. gestureLibrariLibrary = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getPath() + "/mygestures");
  12. if (gestureLibrariLibrary.load()) {
  13. Toast.makeText(MainActivity.this, "手势文件装在成功", Toast.LENGTH_LONG).show();
  14. } else {
  15. Toast.makeText(MainActivity.this, "手势文件装在失败", Toast.LENGTH_LONG).show();
  16. }
  17. // 定义手势编辑组件绑定监听器
  18. gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
  19. @Override
  20. public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
  21. // 识别用户刚刚所绘制的手势
  22. ArrayList<Prediction> predictions = gestureLibrariLibrary.recognize(gesture);
  23. ArrayList<String> result = new ArrayList<String>();
  24. // 遍历所有找到的Prediction对象
  25. for (Prediction prediction : predictions) {
  26. // 只有相似度大于0.2的手势才会被输出
  27. if (prediction.score > 0.2) {
  28. result.add("与手势{" + prediction.name + "}相似度为:"+ prediction.score);
  29. }
  30. }
  31. if (result.size() > 0) {
  32. ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(MainActivity.this,
  33. android.R.layout.simple_dropdown_item_1line,
  34. result.toArray());
  35. new AlertDialog.Builder(MainActivity.this).setAdapter(arrayAdapter, null).setPositiveButton("确定", null).show();
  36. } else {
  37. Toast.makeText(MainActivity.this, "无法能找到匹配的手势",
  38. Toast.LENGTH_LONG).show();
  39. }
  40. }});
  41. }
  42. }

Android基本功:手势的更多相关文章

  1. Android 触摸手势基础 官方文档概览

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  2. Android 触摸手势基础 官方文档概览2

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  3. android GestureDetector 手势基础

    1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vi ...

  4. Android Gesture 手势创建以及使用示例

    在Android1.6的模拟器里面预装了一个叫Gestures Builder的程序,这个程序就是让你创建自己的手势的(Gestures Builder的源代码在sdk问samples里面有,有兴趣可 ...

  5. Android VersionedGestureDetector手势事件

    今天研究了一下PhotoView,发现里面的自定义的手势事件可以支持所有的SDK版本,该事件可以实现拖拽.滑动.缩放功能.下面直接上代码: public abstract class Versione ...

  6. Android基本功:Handler消息传送机制

    一.什么是UI线程 当程序第一次启动的时候,Android会同时启动一条主线程( Main Thread). 主要负责处理与UI相关的事件. 二.UI线程存在的问题 出于性能优化考虑,Android的 ...

  7. android GestureDetector 手势的判断

    import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Ges ...

  8. Android 屏幕手势滑动中onFling()函数的技巧分析

    关于如何处理手势操作以及那四个基本固定的顺序我就不讲解了,这里直接跳到我们获得瞬间滑动后回调onFling()这个抽象函数时,应该如何根据参数比较准确的判断滑动方向.如果你没有前面的基础知识,你可以去 ...

  9. android实现手势锁

    通过简单的设置后即可实现简单的手势锁: setLineVisible方法设置是否显示手势路径: setLineWidth方法设置手势路径连线的粗细: setLineColor方法设置常规状态手势路径连 ...

随机推荐

  1. FreeBSD_11-系统管理——{Part_0-基础}

    Tips: sysctl -d kern.maxvnodes #查看系统控制选项的含义 true > file #清空文件内容 alias ls 'ls -I(大写i)' #取消 root 的 ...

  2. aspectj pointcut 找不到类型pointcut cannot be resolved to a type

    引入了aspectJ后,文件提示找不到pointcut类型.修改如下: .project文件添加内容,红色字体为添加的引用 <?xml version="1.0" encod ...

  3. 使用my exclipse对数据库进行操作(1)

    一.查找 public class class1 { public static void main(String[] args) { // TODO Auto-generated method st ...

  4. 【洛谷P1941】飞扬的小鸟

    f [ i ] [ j ] 表示横坐标为 i ,高度为 j 时的最小点击次数 分别dp处理: 1.上升,(1)<m (2)>=m 2.下降 3.管道 #include<cstdio& ...

  5. 取得ascii的例子

    取得ascii的例子:    String abc = "hello world";    int m = abc.c_str()[0];    ShowMessage(m); i ...

  6. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...

  7. C#设计模式——单件模式

    一.为何需要单件模式 需求 我们开发了一个大型的项目,其中存在许多的工具类.但是其中很多的工具类我们并不是经常使用得到,甚至 一次都不会使用.但是这些工具类都是静态的类,会消耗很多的内存,即使一次都不 ...

  8. centos7 安装 notejs

    1.安装集成工具 yum -y install gcc make gcc-c++ 2.安装notejs 自行选择版本:https://nodejs.org/dist/ wget https://nod ...

  9. MVC3不能正确识别JSON中的Enum枚举值

    一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...

  10. [OpenCV] 1、读取图片

    >_<" 安装及配置请看http://www.cnblogs.com/zjutlitao/p/4042074.html >_<" 这篇是一个简单的在VS20 ...