2012年8月21日 星期二

Android:手勢(Gestrue)相關應用

自從SDK1.6之後,可以從模擬器內建之Gesture Builder建立來建立自己的手勢
可以選擇將模擬器建立好隻手勢資料庫放進專案 raw中,則可以發佈到任何Android



XML Layout


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

           <!-- 建立GestureOverlayView 用來呈現手勢-->
           <android.gesture.GestureOverlayView
            android:id="@+id/gestureOverlayView1"
            android:layout_width="match_parent"
            android:layout_height="250dp"    
            android:background="#88333333" >
        </android.gesture.GestureOverlayView>

           <TextView
               android:id="@+id/textView1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="TextView" />


    </LinearLayout>

JAVA code


package com.example.mygestures;

import...

public class MainActivity extends Activity {
private GestureLibrary lib;

private TextView msg;
        //建立一個File物件變數,主要是要呈現函式資料庫於SD卡的位置
private final File PATH = new File(Environment.getExternalStorageDirectory(),
"gestures");
private final float SCORE = 3.0f;// 自訂比對成功分數基本值

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msg = (TextView) findViewById(R.id.textView1);
GestureOverlayView gestures = (GestureOverlayView)        
                                                                   findViewById(R.id.gestureOverlayView1);

//登記監聽物件
gestures.addOnGesturePerformedListener(new MyGestureListener());

//兩種取得手勢函式庫的方法,從SD卡取得或是內建於專案res/raw資料夾中
/*GestureLibraries為類別GestureLibrary之工具類別,用來進行多種操作,
EX:幫助GestureLibrary建立並取得對應之手勢函式庫*/
lib = GestureLibraries.fromFile(PATH);
//lib=GestureLibraries.fromRawResource(this, R.raw.gestures);//從專案Raw中取得
//若手勢函式庫載入失敗,則直接結束活動
if (!lib.load()) {
finish();
}

}

// 內部監聽類別, 實作OnGesturePerformedListener介面
private class MyGestureListener implements OnGesturePerformedListener {

public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
// TODO Auto-generated method stub
// 比對gesture取得可能吻合之手勢資料,並存入list中
ArrayList<Prediction> predictions = lib.recognize(gesture);
// 判斷gesture資料庫是否有資料
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);// 通常index=0代表是集合中分數
                                                                                                   最高的項目

                                        //當分數大於我們預先設定的分數,則辨識成功
                                        if (prediction.score > SCORE) {
/*Prediction類別的變數name,便是當初在模擬器中建立欲辨別
                                           的字串,之後若是要加以延伸之範例,主要便是利用此字串
                                           來判斷要 進行 何種活動
*/

msg.setText("比對到了\n比對資料:" + prediction.name + "\n比對分數:"
+ prediction.score);
} else {
msg.setText("比對不到" + "\n比對分數:" + prediction.score);
}
} else {
msg.setText("無資料");
}

}

}

}


沒有留言:

張貼留言