輸入法手勢識別(Gestures Builder)

1. 開啟Android模擬器>開啟Gestures Builder

2.選擇Add gesture

3.輸入Name > 輸入手勢辨別圖形 > Done

4. 產生gestures檔案,路徑在/storage/sdcard/gestures,匯出到你要的路徑,在專案res之下建立       raw資料夾,把匯出的檔匯入到raw資料夾

以下為程式碼

1. activity_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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:gestureColor="#00ff00"/>


</LinearLayout>

2. MainActivity.java

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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity implements
OnGesturePerformedListener {
GestureLibrary mLibrary;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
finish();
}
}

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList predictions = mLibrary.recognize(gesture);
// We want at least one prediction
if (predictions.size() > 0) {
Prediction prediction = (Prediction) predictions.get(0);
// We want at least some confidence in the result
if (prediction.score > 3) {
// Show the spell
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
.show();
}
}
}
}

沒有留言: