一、准备手势库:

  使用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. VMware虚拟机上安装xp操作系统

    前提:安装好虚拟机 资料:windows xp 的虚拟机操作系统 上面这个文件最好保存好一份,防止以后虚拟机用坏了可以重新安装. 1 新建目录D:\virtual machine\vSQL\vm将我们 ...

  2. nutz框架使用记录之Cnd.wrap

    这是对Cnd.wrap 官方用法 , 直接硬编码 , [JAVA]List<Person> crowd = dao.query(Person.class, Cnd.wrap("n ...

  3. 简介SWT Jface

    可以使用标准窗口小部件工具箱(Standard Widget Toolkit,SWT)和 JFace 库来开发用于 Eclipse 环境的图形用户界面,而且还可以将它们用于开发单独的 GUI 本机应用 ...

  4. jquery返回顶部和底部插件和解决ie6下fixed插件

    (function($){ //返回顶部和底部插件 $.fn.extend({ goTopBootom:function (options){ //默认参数 var defaults = { &quo ...

  5. geth

    >geth --networkid 123 --dev --datadir "d:/blockchain/project/ethereum" --rpc --rpcaddr ...

  6. 51nod 1597 有限背包计数问题 (背包 分块)

    题意 题目链接 Sol 不会做啊AAA.. 暴力上肯定是不行的,考虑根号分组 设\(m = \sqrt{n}\) 对于前\(m\)个直接暴力,利用单调队列优化多重背包的思想,按\(\% i\)分组一下 ...

  7. 10th week task -1

    1:For each ... inFor...in ExamplesFor...of 对以上的内容进行 Examples和Explanation (1)For...in 以任意顺序遍历一个对象的可枚举 ...

  8. H5新特性监听手机的返回键

    var hiddenProperty ='hidden' in document ? 'hidden' :'webkitHidden' in document ? 'webkitHidden' : ' ...

  9. show与ShowDialog substring

    substring public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beg ...

  10. 关于i 标签盛放背景图像

    1.html部分 <div class="hover right"> <i class="log_change state_psd">& ...