第一步:建立手勢庫
使用SDK自帶例子GestureBuilder建立手勢庫(位置:android-sdk-windows\samples\android-8\GestureBuilder)。使用GestureBuilder之前,你需要恢復其到開發環境,然後進行編繹並部署到手機上。此時,就可以使用GestureBuilder建立手勢庫,生成的手勢庫文件在SCDard上,默認文件名稱爲:gestures
第二步:在應用中加載手勢庫文件,然後開發手勢識別代碼。
把手勢庫文件gestures文件拷貝到項目的res/raw目錄下。然後在佈局文件中添加用於手勢繪製的View:
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent“ android:layout_height="0dip"
android:layout_weight="1.0" />
爲View添加手勢監聽事件:gestureOverlayView.addOnGesturePerformedListener();
得到手勢庫:mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
加載手勢庫:mLibrary.load();
List<Prediction> predictions = mLibrary.recognize(gesture);//從手勢庫中查詢匹配的內容,匹配的結果可能包括多個相似的內容,匹配度高的結果放在最前面
大多數情況下,手勢都是通過一筆完成。然而有一些特別的需求就需要通過多個筆畫來實現,這時可以使用gestureStrokeType屬性進行設置:android:gestureStrokeType="multiple"
String文件:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">GestureTest</string>
- <string name="notrecognize">不能識別該手勢</string>
- <string name="noprediction">手勢識別率太低,請重新輸入</string>
- </resources>
佈局文件:
- <?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/myGestureView"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1.0"
- />
- </LinearLayout>
Java文件:
- package cn.class3g.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 getstureView;
- GestureLibrary gLibrary;
- boolean loadState = true;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init() {
- getstureView = (GestureOverlayView) this
- .findViewById(R.id.myGestureView);
- getstureView
- .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) {
- 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的話太容易失敗,本人測試5比較合適
- if ("close".equals(prediction.name)) {
- finish();//實現關閉功能,用的是activity自帶的
- } else if ("dialto".equals(prediction.name)) {
- //實現播出固定電話的功能,記得添加權限
- Intent intent = new Intent(Intent.ACTION_CALL,
- Uri.parse("tel:13333333333"));
- startActivity(intent);
- }
- }
- } else {
- showToast(R.string.notrecognize);
- }
- } else {
- showToast(R.string.noprediction);
- }
- }
- }
- private void showToast(int resId) {
- Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
- }
- }