Step1: 生成一个Android Gusture Builder程序用于画Gusture然后存储起来用于其它的项目

首先导入android SDK\android-sdk-windows\samples\android-8\ 目录下的GestureBuilder项目,用于生成Gusture类库

导入过程中要加入几个文件才能导入到Eclipse中,如下图所示: 这几个文件可以从任意android项目中copy

.classpath .project default.properties 3个文件

导入之后生成项目如下图所示: 将该项目安装到模拟器中,如下图所示:

Android Gusture 手势识别小案例_第1张图片" src="https://img.it610.com/image/info5/d6cc8ff5fe92485fbb8dd4566da76232.jpg" width="254" height="398" style="border:1px solid black;">Android Gusture 手势识别小案例_第2张图片" src="https://img.it610.com/image/info5/559f574c14824834be0456c4d1770b98.jpg" width="379" height="534" style="border:1px solid black;">

step2:应用此程序生成我们需要的手势,点击 Add gesture按钮添加手势,随便添加几个手势之后

Android Gusture 手势识别小案例_第3张图片" src="https://img.it610.com/image/info5/4a04fbc1a40f48cebbf874a74a7480aa.jpg" width="372" height="533" style="border:1px solid black;">然后会在sdcard中生成一个gusture文件 如图所示

Android Gusture 手势识别小案例_第4张图片" src="https://img.it610.com/image/info5/001e2ffa67704dd19d4b155fc88d0c7c.jpg" width="650" height="122" style="border:1px solid black;">

step2:将此gusture文件导出到桌面,然后再复制到新建的项目中去

step3:新建项目Gusture,并将刚才的gusture文件导入到/res/raw目录下,如下图所示

Android Gusture 手势识别小案例_第5张图片" src="https://img.it610.com/image/info5/f7b516652d96499cb2f77fb5b3e14d06.jpg" width="256" height="405" style="border:1px solid black;">

step4: 设计应用的UI界面,main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><android.gesture.GestureOverlayViewandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:id="@+id/gesture" android:gestureStrokeType="multiple"/><!-- 多笔手势 --><!-- android:layout_weight="0"  值越低越先赋值  所以先对Button赋值再对android.gesture.GestureOverlayView赋值 --><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_weight="0"android:text="@string/recognize"android:onClick="findGesture" /></LinearLayout>


string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, MainActivity!</string>    <string name="app_name">手势识别</string>    <string name="notfind">手势不匹配</string>    <string name="notfull">手势匹配度太低</string>    <string name="recognize">识别</string></resources>


step5:MainActivity.java

package cn.roco.gesture;import java.util.ArrayList;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.Prediction;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG="MainActivity";private GestureLibrary library;private Gesture mygesture;private GestureOverlayView gestureOverlayView ;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);/** 根据放入到res/raw/目录下的gestures文件生成GestureLibrary手势库 */library = GestureLibraries.fromRawResource(this, R.raw.gestures);/**加载手势库*/library.load();gestureOverlayView = (GestureOverlayView) this.findViewById(R.id.gesture);/**只能监听单笔手势*//*gestureOverlayView.addOnGesturePerformedListener(new MyGesturePerformedListener());*//**可以监听单笔手势也可以监听多笔手势*/gestureOverlayView.addOnGestureListener(new MyGestureListener());}/** * 处理按钮响应的方法 * 点击识别按钮 开始识别手势 */public void findGesture(View view){recognizeGesture(mygesture);//清除画出的手势gestureOverlayView.clear(true);}private final class MyGestureListener implements android.gesture.GestureOverlayView.OnGestureListener{@Overridepublic void onGestureStarted(GestureOverlayView overlay,MotionEvent event) {Log.i(TAG, "onGestureStarted");}@Overridepublic void onGesture(GestureOverlayView overlay, MotionEvent event) {Log.i(TAG, "onGesture");}@Overridepublic void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {Log.i(TAG, "onGestureEnded");/**取得用户最后画完后的手势*/mygesture=overlay.getGesture();}@Overridepublic void onGestureCancelled(GestureOverlayView overlay,MotionEvent event) {Log.i(TAG, "onGestureCancelled");}}/** * 用户绘制完手势后响应 */private final class MyGesturePerformedListener implementsandroid.gesture.GestureOverlayView.OnGesturePerformedListener {@Overridepublic void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {recognizeGesture(gesture);}}/**识别手势*/private void recognizeGesture(Gesture gesture) {/** * 从手势库中查询匹配的内容, 匹配结果可能包含多个相似的内容, * 匹配度高的结果放在最前面 */ArrayList<Prediction> predictions = library.recognize(gesture);if (!predictions.isEmpty()) {Prediction prediction=predictions.get(0);if (prediction.score>=6) {   //匹配度大于等于60%if ("ouyang".equals(prediction.name)) {/**调用手机拨号程序*/Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:15245474568"));startActivity(intent);}else if("close".equals(prediction.name)){finish();//关闭Activity   会引发onDestory方法}}else{//手势匹配度太低Toast.makeText(getApplicationContext(), R.string.notfull, 1).show();}} else {//不匹配Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();}}@Overrideprotected void onDestroy() {super.onDestroy();/**杀掉本应用的进程 * 第一种方法:首先获取当前进程的id,如何杀死该进程  */android.os.Process.killProcess(android.os.Process.myPid());//关闭应用/** * 关闭应用还有2种方法 *  * 第二种方法:终止当前正在运行的Java虚拟机,导致程序终止 *   System.exit(0); *    *    * 第三种方法:强制关闭与该包有关联的一切执行 *   android.app.ActivityManager manager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);manager.restartPackage(getPackageName());还要添加权限才行  <uses-permission android:name="android.permission.RESTART_PACKAGES"/>*/}} 



step6:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.roco.gesture"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />        <!-- 拨打电话的权限 -->    <uses-permission android:name="android.permission.CALL_PHONE"/>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


step7:将项目部署到模拟器中,如果所示界面:

Android Gusture 手势识别小案例_第6张图片" src="https://img.it610.com/image/info5/694ae62c0dad46789eb3c62f05494308.jpg" width="375" height="535" style="border:1px solid black;">Android Gusture 手势识别小案例_第7张图片" src="https://img.it610.com/image/info5/0d2240ac9e4145e798f355b4b961850b.jpg" width="374" height="532" style="border:1px solid black;">

然后画手势 点击“识别”按钮进行识别上面的时候一点击 就退出了此应用


Android Gusture 手势识别小案例_第8张图片" src="https://img.it610.com/image/info5/a65ae0c2d0774df3a8a362d884259061.jpg" width="375" height="538" style="border:1px solid black;">Android Gusture 手势识别小案例_第9张图片" src="https://img.it610.com/image/info5/89fd015ab85f48cabf6d5d618bde8ad1.jpg" width="375" height="533" style="border:1px solid black;">

然后画手势 点击“识别”按钮进行识别 就激活了拨号程序进行拨号

Android Gusture 手势识别小案例_第10张图片" src="https://img.it610.com/image/info5/5f68f848cc0b429db7749bd6bdaf7660.jpg" width="375" height="534" style="border:1px solid black;">Android Gusture 手势识别小案例_第11张图片" src="https://img.it610.com/image/info5/d0f98ff0ca3f426db37e97a858bd0d81.jpg" width="372" height="536" style="border:1px solid black;">

然后画手势 点击“识别”按钮进行识别 显示匹配度太低




附注:自己写一个添加手势的应用

AddGesture.java

package cn.roco.gesture;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.graphics.Bitmap;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;/** * 添加手势 */public class AddGesture extends Activity {EditText editText;GestureOverlayView gestureOverlayView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.addgesture);// 获取文本编辑器editText = (EditText) findViewById(R.id.gesture_name);// 获取手势编辑视图gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);// 设置手势的绘制颜色gestureOverlayView.setGestureColor(Color.RED);// 设置手势的绘制宽度gestureOverlayView.setGestureStrokeWidth(4);// 为 gesture的手势完成事件绑定事件监听器gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) {// 加载save.xml界面布局代表的视图View saveDialog = getLayoutInflater().inflate(R.layout.save, null);// 获取 saveDialog里的ImageView组件ImageView imageView = (ImageView) saveDialog.findViewById(R.id.show);// 获取 saveDialog里的 EditText组件final EditText gestureName = (EditText) saveDialog.findViewById(R.id.gesture_name);// 根据 Gesture包含的手势创建一个位图Bitmap bitmap = gesture.toBitmap(128, 128, 10,0xFFFF0000);imageView.setImageBitmap(bitmap);// 使用对话框显示saveDialog组件new AlertDialog.Builder(AddGesture.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 获取指定的文件对应的手势库GestureLibrary gestureLibrary = GestureLibraries.fromFile("/sdcard/gestures");// 添加手势gestureLibrary.addGesture(gestureName.getText().toString(), gesture);// 保存手势库gestureLibrary.save();Toast.makeText(getApplicationContext(), "保存成功", 1).show();}}).setNegativeButton("取消", null).show();}});}}

addgesture.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:text="请在下面屏幕上绘制手势" android:layout_width="fill_parent"android:layout_height="wrap_content" /><!-- 使用手势绘制组件 --><android.gesture.GestureOverlayViewandroid:layout_width="fill_parent" android:layout_height="fill_parent"android:layout_weight="1" android:id="@+id/gesture"android:gestureStrokeType="multiple" /><!-- 多笔手势 --></LinearLayout>


save.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="wrap_content"><TextView android:text="请输入保存的手势名称"android:layout_marginRight="8dip" android:layout_width="wrap_content"android:layout_height="wrap_content" /><!-- 定义一个文本框来让用户输入手势名 --><EditText android:layout_width="fill_parent"android:layout_height="wrap_content" android:id="@+id/gesture_name" /></LinearLayout><ImageView android:id="@+id/show" android:layout_marginTop="10dp"android:layout_width="128dp" android:layout_height="128dp " /></LinearLayout>


Android Gusture 手势识别小案例_第12张图片" width="375" height="535" style="border:1px solid black;"> Android Gusture 手势识别小案例_第13张图片" width="373" height="534" style="border:1px solid black;">



==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================


更多相关文章

  1. python起点网月票榜字体反爬案例
  2. Android本地语音识别引擎PocketSphinx-语言建模
  3. Android原生SpeechRecognizer(语音识别)
  4. [Android] 获取Android设备的唯一识别码|设备号|序号|UUID
  5. Android语音识别编程初步
  6. ArcGIS API for Android(安卓)案例教程 17
  7. Android静态图片人脸识别的完整demo(附完整源码)
  8. GestureOverlayView
  9. Android(安卓)NDK系列(三)-AS编写C文件没有提示和不识别NULL

随机推荐

  1. Android中利用GridView实现水平和垂直均
  2. Android中RelativeLayout各个属性的含义
  3. Android中 ScrollView(ListView)中嵌套List
  4. Android简单自定义圆形和水平ProgressBar
  5. Android原生(Native)C(JNI/NDK)开发之二:f
  6. Android软硬整合设计与框架揭秘教程
  7. Android学习-RecyclerView默认scrollbar
  8. TextView去除内边距
  9. ubuntu 配置Android(安卓)开发环境
  10. Arcgis android 10.2安装方法