android之手勢識別
package cn.class3g.gesture;
…
public class MyGestureTestActivity extends Activity {
GestureOverlayView gestureView = null;
GestureLibrary libary = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
gestureView = (GestureOverlayView) this.findViewById(R.id.myGresture);
gestureView
.addOnGesturePerformedListener(new MyOnGesturePerformedListener());
// 建立手勢庫物件
libary = GestureLibraries.fromRawResource(this, R.raw.gestures);
// 載入手勢資源
libary.load();
}
private final class MyOnGesturePerformedListener implements
OnGesturePerformedListener {
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
ArrayList<Prediction> predictions = libary.recognize(gesture);
if (!predictions.isEmpty()) {
Prediction prediction = predictions.get(0);
Log.i("TAG", String.valueOf(prediction.score));
if (prediction.score > 5) { // 使用者輸入手勢與手勢庫中儲存手勢相似度大於10%
if ("close".equals(prediction.name)) {
finish();
} else if ("dialto".equals(prediction.name)) {
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:133123145678"));
startActivity(intent);
}
} else {
showToast(R.string.norecohnize);
}
} else {
showToast(R.string.nopediction);
}
}
}
private void showToast(int resId) {
Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
}
}