一、准备手势库:

  使用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;
} }

最新文章

  1. 迷你MVVM框架 avalonjs 实现上的几个难点
  2. Theano3.3-练习之逻辑回归
  3. C++Builder 中 Enter键或者Tab键无效
  4. XPath具体解释
  5. php实现网页标签补全方法(转)
  6. JBoss部署项目log4j配置会造成死锁问题,浏览器访问一直pending状态
  7. android camera(二):摄像头工作原理、s5PV310 摄像头接口(CAMIF)
  8. module_init和init_module的区别
  9. break,continue,return 区别
  10. RDLC系列(一)ASP.NET RDLC 报表自定义数据源
  11. 自定义 Layout布局 UICollectionViewLayout
  12. 对HTML5标签的认识(二)
  13. 高性能mysql 第六章查询性能优化 总结(上)查询的执行过程
  14. C/C++基础----string, vector, array
  15. Android Studio系列教程
  16. Postman简明教程
  17. Windows server 2008系统各类版本的优缺点比较,Windows2008系统标准版 企业版 数据中心版 WEB版等
  18. HDU4628
  19. 如果有人问你ZooKeeper是什么,就把这篇文章发给他。
  20. Hive初识(四)

热门文章

  1. [转]mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
  2. wex5新增数据库
  3. 简单的Extjs中的Combox选择下拉框使用
  4. C#使用第三方组件Epplus操作Excel表
  5. WSAAsyncSelect 消息模型
  6. oracle学习篇二:常用SQL
  7. 从零开始的全栈工程师——JS面向对象( 原型 this 继承)
  8. String StringBuffer StringBuilder对比
  9. 什么时候修改class
  10. ECMAscript 变量作用域