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"

手势识别代码:

public class MainActivity extends Activity {
    private GestureOverlayView gestureOverlayView;
    private GestureLibrary mLibrary;
    private boolean state;
    private EditText addressText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addressText = (EditText)this.findViewById(R.id.address);
        gestureOverlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
        //当用户完成一次Gesture绘制后,系统将自动调用Listener对象的onGesturePerformed()方法
        gestureOverlayView.addOnGesturePerformedListener(new GestureListener());
        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        state = mLibrary.load();//加载手势库
    }
    
    private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener{
		@Override
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			if(state){
				List<Prediction> predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面
				if(!predictions.isEmpty()){
					Prediction prediction = predictions.get(0);
					//prediction的score属性代表了与手势的相似程度
					//prediction的name代表手势对应的名称
					if(prediction.score > 1){
						addressText.setText(prediction.name);
					}
				}
			}
		}
    }
}
完整示例代码:

package cn.itcast.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.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class DemoActivity extends Activity {
	private GestureOverlayView mGestureOverlayView;
	private Gesture gesture;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mGestureOverlayView = (GestureOverlayView) this
				.findViewById(R.id.gestures);

		// 单笔 手势事件的listener
		// mGestureOverlayView
		// .addOnGesturePerformedListener(new MyGesturePerformedListener());

		// 多笔 手势事件的listener
		mGestureOverlayView.addOnGestureListener(new MyGestureListener());
	}

	private class MyGestureListener implements
			GestureOverlayView.OnGestureListener {
		public void onGestureStarted(GestureOverlayView overlay,
				MotionEvent event) {
			System.out.println("onGestureStarted");
		}

		public void onGesture(GestureOverlayView overlay, MotionEvent event) {
			System.out.println("onGesture");
		}

		public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
			System.out.println("onGestureEnded");
			gesture = overlay.getGesture();// 获取当前手势
		}

		public void onGestureCancelled(GestureOverlayView overlay,
				MotionEvent event) {
			System.out.println("onGestureCancelled");
		}
	}

	private void recoginizeGesture(Gesture gesture, GestureLibrary mLibrary) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
		Prediction prediction = predictions.get(0);
		if (prediction.score >= 5) {
			if ("close".equals(prediction.name)) {
				System.out.println("关闭的操作 ");
				finish();
			} else if ("call".equals(prediction.name)) {
				System.out.println("拨打电话 ");
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:15538311981"));
				startActivity(intent);
			} else if ("exit".equals(prediction.name)) {
				finish();
			}
		} else {
			Toast.makeText(getApplicationContext(), "手势不能被识别 ", 0).show();
			System.out.println("手势不能被识别 ");
		}
	}

	/**
	 * 单笔手势对应的listener
	 */
	public void onrecognize(View view) {
		// 把已经定义好的手势 转化成一个手势库
		GestureLibrary mLibrary = GestureLibraries.fromRawResource(
				DemoActivity.this, R.raw.gestures);
		// 把手势库加载到内存
		mLibrary.load();
		recoginizeGesture(gesture, mLibrary);
	}
}
main.xml:

<?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/gestures"
        android:fadeDuration="2000"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:gestureStrokeType="multiple" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:onClick="onrecognize"
        android:text="识别手势" />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值