一、准备手势库:

  使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-10\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,将其他正确项目下的".classpath",".project"和"project.properties"三个文件拷贝到GestureBuilder项目下,导入到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures。

二、开发:

  1、配置文件:

  在配置文件中加入拨打电话权限,后面用到打电话。

<!-- 拨打电话  -->
<uses-permission android:name="android.permission.CALL_PHONE"/>

  布局文件:

<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=".MainActivity" > <!--
gestureStrokeType:可以设置单笔识别或多笔识别
layout_weight:优先级别默认为0,最高为0,次之1...。显示界面时,先测量按钮的高度,然后再用窗口高度减去按钮高度,所得的高度设置到手势界面
-->
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/btnCheck"
android:layout_weight="1"
android:gestureStrokeType="multiple" />
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_weight="0"
android:onClick="match"
android:text="@string/check" />
</RelativeLayout>

  2、后台代码:

package com.example.gesture;

import java.util.List;

import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG="Gesture";
private GestureOverlayView gestureOverlayView;
private GestureLibrary mLibrary;
private boolean state;
private Gesture gesture;//用户最终画完手势 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
state = mLibrary.load();//加载手势库
Log.i(TAG, state+"");
gestureOverlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
//当用户完成一次Gesture绘制后,系统将自动调用Listener对象的onGesturePerformed()方法,只支持单笔手势
//gestureOverlayView.addOnGesturePerformedListener(new GestureListener());
//可以监听单笔和多笔识别
gestureOverlayView.addOnGestureListener(new MyGestureListener());
} public void match(View v){
matchGesture(gesture);
gestureOverlayView.clear(true);
}
//单多笔监视
private final class MyGestureListener implements OnGestureListener{
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGesture() ... ");
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
Log.i(TAG, "onGestureCancelled() ... ");
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGestureEnded() ... ");
gesture=overlay.getGesture();
}
@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
Log.i(TAG, "onGestureStarted() ... ");
}
} //单笔手势监听类
private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener{
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
if(state){
matchGesture(gesture);
}
}
}
private void matchGesture(Gesture gesture) {
//123:对号,258:X,369:李,456:Z
//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面
List<Prediction> predictions = mLibrary.recognize(gesture);
if(!predictions.isEmpty()){
Prediction prediction = predictions.get(0);
//prediction的score属性代表了与手势的相似程度
//prediction的name代表手势对应的名称
if(prediction.score > 6){
if("123".equals(prediction.name)){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:159016"));
startActivity(intent);
}else if ("456".equals(prediction.name)) {
finish();//关闭activity,会触发onDestroy方法,进而关闭应用
}else if ("369".equals(prediction.name)) {
Toast.makeText(getApplicationContext(), "结果:李", Toast.LENGTH_LONG).show();
}else if ("258".equals(prediction.name)) {
Toast.makeText(getApplicationContext(), "结果:X", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(), R.string.notfull, Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), R.string.notfind, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
//杀掉进程
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());//关闭应用
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

Android学习笔记_28_手势识别的更多相关文章

  1. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  2. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  3. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  4. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  5. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  6. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  7. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  8. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

  9. 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...

随机推荐

  1. Beautiful Soup库介绍

    开始前需安装Beautiful Soup 和lxml. Beautiful Soup在解析时依赖解析器,下表列出bs4支持的解析器. 解析器 使用方法 Python标准库 BeautifulSoup( ...

  2. [转]<加密算法c#>——— 3DES加密之ECB模式 和 CBC模式

    本文转自:http://www.cnblogs.com/qq278360339/archive/2013/06/05/3119222.html 最近 一个项目.net 要调用JAVA的WEB SERV ...

  3. 一个C#后台调用接口的例子

    string url = ConfigurationSettings.AppSettings["resurl"].ToString(); var wc = new WebClien ...

  4. js【jquery】 - DOM操作

    1.修改元素样式 js方式: var div2 = document.getElementById("") div2.style.width = '200px'; div2.cla ...

  5. Git连接远程服务器输入密码问题

    当遇到使用Git从远程pull 或者 push代码的时候提示输入密码的时候,毫无疑问SSH Key出问题了! 一.如果只是使用Git Bash,重新生成一下ssh,把新的id_rsa.pub添加到Gi ...

  6. 如何更新maven需要的jar包

    第一次使用maven,检出项目生成时出现缺少xxx.jar,目录在C盘下: 拿mybatis-spring-1.2.2.jar来说,发现在C:\Users\Administrator\.m2\repo ...

  7. BZOJ3033: 太鼓达人(欧拉回路)

    题意 题目链接 Sol 第一问的答案是\(2^M\),因为每个位置只有\(0 / 1\)两种情况,最优情况下一定是每个位置代表着一个长度为\(K\)的字符串 考虑相邻两个字符串之间的转化,第二个字符串 ...

  8. 原生js实现雪花飘落效果

    雪花飘落的效果实现步骤:1.使用setInterval定时器每800毫秒创建一个雪花:2.把每一个雪花作为参数传进动态下落的方法中即可. <style> *{padding: 0;marg ...

  9. jQuery小测验

    1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? 提示使用has() $(div:has(span) ...

  10. matlab练习程序(圆柱投影)

    圆柱投影就是将一张二维的图像投影到三维的圆柱体上,不过在显示图像的时候依然是以二维的形式给出. 投影最重要的步骤就是计算投影变换公式,和图像旋转类似,只要得到变换公式,再依照公式进行代码编写就很容易了 ...