文章目錄
- 手勢識別系統:
- 先把手勢庫放到項目中:(建立手勢庫見下一篇部落格)
- 在res檔案夾下建立一個名為raw的檔案夾,然後把手勢庫放進去
-
- 然後開始項目的建立:
-
- strings.xml:
手勢識別系統:先把手勢庫放到項目中:(建立手勢庫見下一篇部落格)在res檔案夾下建立一個名為raw的檔案夾,然後把手勢庫放進去 然後開始項目的建立: strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">GestureTest</string> <string name="notrecognize">沒有手勢</string> <string name="noprediction">手勢識別率太低,請重新輸入</string> <string name="noloading">手勢庫沒有載入成功</string></resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.gesture.GestureOverlayView android:id="@+id/myGesture" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" /></LinearLayout>
GestureTestActivity.java:
package cn.csdn.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;import android.util.Log;import android.widget.Toast;public class GestureTestActivity extends Activity {GestureOverlayView gestureView;GestureLibrary gLibrary;boolean loadState;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();} private void init() {gestureView = (GestureOverlayView) this.findViewById(R.id.myGesture);gestureView.addOnGesturePerformedListener(new MyOnGesturePerformedListener());// 建立首飾庫對象GestureLibrarygLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);// 載入手勢庫資源loadState = gLibrary.load();}private final class MyOnGesturePerformedListener implementsOnGesturePerformedListener {public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {if (loadState) {//載入手勢資源成功// 擷取畫的圖形進行匹配,匹配程度就是Prediction中的scoreArrayList<Prediction> predictions = gLibrary.recognize(gesture);if (!predictions.isEmpty()) {// 如果使用者畫了圖形,就會匹配Prediction prediction = predictions.get(0);Log.i("TAG", String.valueOf(prediction.score));if (prediction.score > 5) {// 判斷相似性大於1,與裡面的兩者進行匹配if ("close".equals(prediction.name)) {//關閉finish();} else if ("dialto".equals(prediction.name)) {//打電話Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:11111111111"));startActivity(intent);}} else {// 相似性小於1,不識別showToast(R.string.noprediction);}} else {//沒有畫圖形showToast(R.string.notrecognize);}} else {showToast(R.string.noloading);}}}private void showToast(int tesId) {Toast.makeText(this, tesId, Toast.LENGTH_LONG).show();}}
:(必須畫的比較精確)
如果畫c形狀的話,會退出這個程式
如果畫一個對鉤的話,會去進行撥號的操作