我导入了手势示例并创建了自己的应用。布局中有一个按钮和gestureoverlayview。该按钮启动GestureBuilderActivity.class,我可以在其中添加或删除手势(这是示例)。在按钮下,在GestureOverlayView中我可以绘制手势。布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to see gestures"
android:id="@+id/Button01"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0" />
</LinearLayout>
从示例中我知道这是我找到手势的地方:
final String path = new File(Environment.getExternalStorageDirectory(),
"gestures").getAbsolutePath();
并且toast msg显示(仍然在示例中)手势保存在/ mnt / sdcard / gestures中:
Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();
如何让应用识别我绘制的手势,并在祝酒词中向我显示其名称?
答案 0 :(得分:0)
首先,我会阅读这篇解释如何使用手势的short article。
要识别手势,您需要向GestureOverlayView提供OnGesturePerformedListener。在这个例子中,我只是让Activity实现了OnGesturePerformedListener。
public class MyActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary mGestures;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// I use resources but you may want to use GestureLibraries.fromFile(path)
mGestures = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mGestures.load()) {
showDialog(DIALOG_LOAD_FAIL);
finish();
}
// register the OnGesturePerformedListener (i.e. this activity)
GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
gesturesView.addOnGesturePerformedListener(this);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mGestures.recognize(gesture);
// Determine if a gesture was performed.
// This can be tricky, here is a simple approach.
Prediction prediction = (predictions.size() != 0) ? predictions.get(0) : null;
prediction = (prediction.score >= 1.0) ? prediction: null;
if (prediction != null) {
// do something with the prediction
Toast.makeText(this, prediction.name + "(" + prediction.score + ")", Toast.LENGTH_LONG).show();
}
}