先上个一道菜,手势的识别。。。。。
java.lang.Object ↳ Android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ android.gesture.GestureOverlayView
介绍下GestureOverlayView,这个透明的view就是让你在上面画手势用的,可叠在其他View上面。
Android的gesture的识别和自定义gesture源码下载地址:
具体下载目录在 /pub/Android源码集锦/2011年/12月/Android的gesture的识别和自定义gesture/
和这个类相关的还有三个接口,分别是
GestureOverlayView.OnGestureListener;
GestureOverlayView.OnGesturePerformedListener(作用:根据在GestureOverlayView上画的手势来识别是否匹配手势库里的手势);
GestureOverlayView.OnGesturingListener.
GestureOverlayView的xml的属性介绍:
android:gestureStrokeType
设置手势的笔画数,它有两个值,GESTURE_STROKE_TYPE_MULTIPLE(多笔),GESTURE_STROKE_TYPE_SINGLE(一笔)
public final class GestureLibraries
static GestureLibrary fromFile(File path) static GestureLibrary fromFile(String path) static GestureLibrary fromPrivateFile(Context context, String name) static GestureLibrary fromRawResource(Context context, int resourceId)
想从SD卡或者raw的资源中直接加载手势;
下面介绍下手势的识别功能,先上代码:
GestureIdentifyDemoActivity.xml
package com.potato; import java.util.ArrayList; import android.app.Activity; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.Prediction; import android.os.Bundle; import android.widget.Toast; public class GestureIdentifyDemoActivity extends Activity { // 手势库 GestureLibrary mGestureLib; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 手势画板 GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gesture_overlay_view_test); // 手势识别的监听器 gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { // 注1 @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { //从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面 ArrayList<Prediction> predictions = mGestureLib .recognize(gesture); // 注3 if (predictions.size() > 0) { Prediction prediction = (Prediction) predictions.get(0); // 匹配的手势 if (prediction.score > 1.0) { Toast.makeText(GestureIdentifyDemoActivity.this, prediction.name, Toast.LENGTH_SHORT).show(); } } } }); // 从raw中加载已经有的手势库 mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); // 注2 if (!mGestureLib.load()) { finish(); } } }
注1: