Android手势识别

这篇博客介绍了如何在Android中实现手势识别。首先,通过GestureBuilder创建手势库,将生成的gestures文件放入res/raw目录。接着,在布局XML中添加GestureOverlayView,并设置手势监听。加载手势库,使用recognize方法进行匹配。当需要处理多笔画手势时,可以调整gestureStrokeType属性。
摘要由CSDN通过智能技术生成
 

第一步:建立手势库

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

                                                                  2012年1月3日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值