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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
private boolean success;
private GestureLibrary library;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到手势库
library=GestureLibraries.fromRawResource(this, R.raw.gestures);
//加载手势库
success=library.load();
GestureOverlayView getView=(GestureOverlayView) findViewById(R.id.gestures);
getView.addOnGesturePerformedListener(new GestureListener());
}
private final class GestureListener implements OnGesturePerformedListener{
public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
if (success) {
//从手势库中查找匹配的手势,最匹配的记录会放在最前面
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {//不为空的话,就会有匹配项
Prediction prediction=
predictions.get(0);//取得第一个元素,最匹配的值
if(
prediction.score>3){//40%的匹配
if ("close".equals(prediction.name) ){
//关闭应用
android.os.Process.killProcess(android.os.Process.myPid());
}else if("5566".equals(prediction.name)){
System.out.println("进入了!");
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5566"));
startActivity(intent);
}
}
}
}
}
}
}