Android手勢識別的實現
手勢識別系統:
先把手勢庫放到項目中:(創建手勢庫見下一篇博客)
在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:
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()); // 創建首飾庫對象GestureLibrary gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); // 加載手勢庫資源 loadState = gLibrary.load(); } private final class MyOnGesturePerformedListener implements OnGesturePerformedListener { public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { if (loadState) {//加載手勢資源成功 // 獲取畫的圖形進行匹配,匹配程度就是Prediction中的score ArrayList<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形狀的話,會退出這個程序
如果畫一個對鉤的話,會去進行撥號的操作
來自:http://blog.csdn.net/like7xiaoben/article/details/7172783
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!