android中手势识别的实现
第一步:建立手势库
使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-8\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures
第二步:在应用中加载手势库文件,然后开发手势识别代码。
把手势库文件gestures文件拷贝到项目的res/raw目录下。然后在布局文件中添加用于手势绘制的View:
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent“ android:layout_height="0dip"
android:layout_weight="1.0" />
为View添加手势监听事件:gestureOverlayView.addOnGesturePerformedListener();
得到手势库:mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
加载手势库:mLibrary.load();
List<Prediction> predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面
大多数情况下,手势都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,这时可以使用gestureStrokeType属性进行设置:android:gestureStrokeType="multiple"
String文件:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">GestureTest</string>
- <string name="notrecognize">不能识别该手势</string>
- <string name="noprediction">手势识别率太低,请重新输入</string>
- </resources>
布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <android.gesture.GestureOverlayView
- android:id="@+id/myGestureView"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1.0"
- />
- </LinearLayout>
Java文件:
- package cn.class3g.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.GestureOverlayView.OnGesturePerformedListener;
- import android.gesture.Prediction;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- public class GestureTestActivity extends Activity {
- GestureOverlayView getstureView;
- GestureLibrary gLibrary;
- boolean loadState = true;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init() {
- getstureView = (GestureOverlayView) this
- .findViewById(R.id.myGestureView);
- getstureView
- .addOnGesturePerformedListener(new MyOnGesturePerformedListener());
- // 创建手势库对象GestureLibrary
- gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
- // 加载手势库资源
- loadState = gLibrary.load();
- }
- private final class MyOnGesturePerformedListener implements
- OnGesturePerformedListener {
- public void onGesturePerformed(GestureOverlayView overlay,
- Gesture gesture) {
- if (loadState) {
- ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
- if (!predictions.isEmpty()) {
- Prediction prediction = predictions.get(0);
- Log.i("TAG", String.valueOf(prediction.score));
- if (prediction.score > 5) {//如果是1的话太容易失败,本人测试5比较合适
- if ("close".equals(prediction.name)) {
- finish();//实现关闭功能,用的是activity自带的
- } else if ("dialto".equals(prediction.name)) {
- //实现播出固定电话的功能,记得添加权限
- Intent intent = new Intent(Intent.ACTION_CALL,
- Uri.parse("tel:13333333333"));
- startActivity(intent);
- }
- }
- } else {
- showToast(R.string.notrecognize);
- }
- } else {
- showToast(R.string.noprediction);
- }
- }
- }
- private void showToast(int resId) {
- Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
- }
- }