How to write and read text from gesture input in android?

拥有回忆 提交于 2019-12-03 21:46:56

In your activity:

GestureLibrary gLibrary;
GestureOverlayView mView;
String txtToDisplay="";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (gLibrary != null) {
            if (!gLibrary.load()) {
                Log.e("GestureSample", "Gesture library was not loaded…");
                finish();
            } else {
                mView = (GestureOverlayView) findViewById(R.id.gestures);
                mView.addOnGesturePerformedListener(this);
            }
        }
    }

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
    // one prediction needed
    if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);
        // checking prediction
        if (prediction.score > 1.0) {
                txtToDisplay+=prediction.name;
            yourTextView.setText(txtToDisplay);
        }
    }
}
}

In the XML you need to put this where you want to get the gestures recognized:

 <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gestureColor="#2a69a5"
        android:uncertainGestureColor="#e2e2e2" >
    </android.gesture.GestureOverlayView>

AND you need the library of gestures. Follow the first step of this tutorial: Android Gestures Tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!