private GestureLibrary gesLib;
private GestureOverlayView gesOverlay;
private String gesPath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_gesture);
/* 查看SDCard是否存在 */
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
/* SD卡不存在,显示Toast信息 */
Toast.makeText(GetGesture.this, "SD卡不存在!程序无法运行", Toast.LENGTH_LONG)
.show();
finish();
}
else {
/* 取得GestureLibrary的文件路径 */
gesPath = new File(Environment.getExternalStorageDirectory(),
"gestures").getAbsolutePath();
File file = new File(gesPath);
if (!file.exists()) {
/* gestures文件不存在,显示Toast信息 */
Toast.makeText(GetGesture.this, "gestures文件不存在!程序无法运行",
Toast.LENGTH_LONG).show();
finish();
}
else {
/* 初始化GestureLibrary */
gesLib = GestureLibraries.fromFile(gesPath);
if (!gesLib.load()) {
/* 读取失败,显示Toast信息 */
Toast.makeText(GetGesture.this, "gestures文件读取失败!程序无法运行",
Toast.LENGTH_LONG).show();
finish();
} else {
/* GestureOverlayView初始化 */
gesOverlay = (GestureOverlayView) findViewById(R.id.myGestures1);
gesOverlay.addOnGesturePerformedListener(new MyListener(
this));
}
}
}
}
/* 告定义OnGesturePerformedListener */
public class MyListener implements OnGesturePerformedListener {
private Context context;
public MyListener(Context context) {
this.context = context;
}
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
/* 手势比对 */
ArrayList<Prediction> predictions = gesLib.recognize(gesture);
if (predictions.size() > 0) {
/* 取最接近的手势 */
Prediction prediction = predictions.get(0);
/* 取得预测值吹少己于1.0 */
if (prediction.score > 1.0) {
/* prediction.name?为?串预测结果 */
Toast.makeText(this.context, prediction.name,
Toast.LENGTH_SHORT).show();
} else {
/* 比对不到,显示Toast */
Toast.makeText(this.context, "找不到匹配的Gesture!",
Toast.LENGTH_SHORT).show();
}
} else {
/* 比对不到,显示Toast */
Toast.makeText(this.context, "找不到匹配的Gesture!",
Toast.LENGTH_SHORT).show();
}
}
}
<?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:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请在屏幕上画出Gesture" android:textSize="18sp" /> <android.gesture.GestureOverlayView android:id="@+id/myGestures1" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" android:gestureStrokeType="multiple" /> </LinearLayout>