2015年6月29日 星期一

Android GestureOverlayView 辨識 手勢 繪圖

之前下載一個遊戲App Magic Touch ,使用GestureOverlayView 辨識手勢繪圖來消氣球,還蠻好玩的!

1.使用模擬器的Gesture Tool或是用手機前往Google Play下載Gesture Tool
安裝完成後,打開輸入對應的文字與繪圖

2.將該成是儲存的gestures檔案複製,建立一個新專案,在res資料夾建立raw資料加,丟到raw資料夾裡面

3./res/layout/activity_main.xml fadeDuration為繪圖顯示停留時間(毫秒)


    
    


4.MainActivity.java
package tw.android;

import java.util.ArrayList;

import android.app.Activity;
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.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity implements
  OnGesturePerformedListener {

 private GestureOverlayView gesture;
 private GestureLibrary gLibrary;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  gesture = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);

  // GestureOverlayView監聽事件
  gesture.addOnGesturePerformedListener(this);

  // 讀取製作的gestures檔案
  gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
  gLibrary.load();

 }

 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  
  ArrayList<Prediction> predictions = gLibrary.recognize(gesture);

  if (predictions.size() > 0) {

   Prediction prediction = predictions.get(0);
   Log.i("1111111", predictions.get(1) + "");
   if (prediction.score > 1.5) {// 符合 顯示對應訊息
    Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
   } else { // 不符合
    Toast.makeText(this, "無法識別", Toast.LENGTH_SHORT).show();
   }
  }
 }
}



檔案下載:
https://github.com/terryyamg/GuestureOverlayViewTest
參考連結:
http://www.tutorialeshtml5.com/2013/02/tutorial-crear-y-reconocer-gestos-con.html
http://developer.android.com/reference/android/gesture/GestureOverlayView.html

沒有留言 :

張貼留言