1、利用手勢工具繪制手勢:

2、在eclipse中的File Explorer中的sdcard中導出gestures手勢檔案,如果沒有顯示,用UltraISO工具打開建立的sdcard導出gestures手勢檔案;
3、在項目中的res目錄中建立raw目錄,将gestures手勢檔案複制到該目錄;
4、參考以下代碼試下手勢的識别:
xml中:
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gestureStrokeType="multiple"
/>
java中:
package com.asia.tmw.gesture;
import java.util.ArrayList;
import android.annotation.SuppressLint;
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.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class MainActivity extends Activity {
private GestureOverlayView overlayView;
private final String tag = "GesturePerformedListener";
private GestureLibrary library;
private Gesture mgesture;// 儲存最終的手勢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
library = GestureLibraries.fromRawResource(this, R.raw.gestures);// 得到手勢庫對象
library.load();// 加載手勢庫
overlayView = (GestureOverlayView) this.findViewById(R.id.gestures);
// 建立手勢監聽--隻針對單筆手勢
// overlayView.addOnGesturePerformedListener(new
// GesturePerformedListener());
// 建立手勢監聽--可以監聽單筆手勢也可以監聽多筆手勢
overlayView.addOnGestureListener(new GestureListener());
}
public void find(View v) {//xml中建立的點選按鈕調用的方法
recognize(mgesture);
overlayView.clear(true);
}
private final class GestureListener implements OnGestureListener {
// 畫的過程中不斷觸發
@Override
public void onGesture(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGesture()");
}
@Override
public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGestureCancelled()");
}
// 繪制手勢結束時,取得的是最終的手勢
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent arg1) {
//Log.i(tag, "onGestureEnded()");
mgesture = overlay.getGesture();// 取得使用者最後畫完的手勢
}
// 手勢開始時
@Override
public void onGestureStarted(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGestureStarted()");
}
}
private void recognize(Gesture gesture) {
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {
Prediction prediction = predictions.get(0);
Log.i(tag, "prediction.score:" + prediction.score);
Log.i(tag, "prediction.name:" + prediction.name);
Log.i(tag, prediction.name.substring(0, 5));
if (prediction.score >= 4) {// 取得相似度值為0-10,0表示相似,1表示相似度10%,10表示相似度100%
if (prediction.name.equals("phonecall")) {
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("18008995508"));
startActivity(intent);
} else if (prediction.name.equals("close")) {
finish();// 關閉視窗,會觸發onDestroy()方法
} else if (prediction.name.substring(0, 4).equals("left")) {
Toast.makeText(getApplicationContext(), "向左滑動", 1).show();
} else if (prediction.name.substring(0, 5).equals("right")) {
Toast.makeText(getApplicationContext(), "向右滑動", 1).show();
} else {
Toast.makeText(getApplicationContext(), R.string.otners, 1).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.low, 1).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();
}
}
private final class GesturePerformedListener implements
OnGesturePerformedListener {
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
recognize(gesture);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());// 殺死目前程序ID
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}