如果您将单词写为单独的字母(即不是草书),如问题中给出的图像所示。然后简单地这样做 -
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gLib.recognize(gesture);
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
String letter = prediction.name;
if (prediction.score > 1.0) {
edittext.setText(edittext.getText().toString() + letter);
}
}
}
这实质上是将新字母附加到现有的 edittext 字符串中。
但是,如果您在谈论草书,那就很复杂了。这是一些可以跟踪草书书写的代码。
public class MainActivity extends Activity {
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
super.onResume();
Tracker t = new Tracker();
t.start();
}
@Override
protected void onPause() {
if (mHandler != null)
mHandler.getLooper().quit();
super.onPause();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (mHandler != null) {
Message msg = Message.obtain();
msg.obj = event.getX() + "," + event.getY();
mHandler.sendMessage(msg);
}
break;
}
return true;
}
private class Tracker extends Thread {
private static final int LETTER_SIZE = 30;
private GestureLibrary gLib;
private ArrayList<GesturePoint> points;
public Tracker() {
points = new ArrayList<GesturePoint>();
gLib = GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
gLib.load();
}
@Override
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
String[] pos = String.valueOf(msg.obj).split(",");
points.add(new GesturePoint(Float.parseFloat(pos[0]), Float.parseFloat(pos[1]), System.currentTimeMillis()));
if (points.size() < LETTER_SIZE) return;
GestureStroke stroke = new GestureStroke(points);
Gesture gesture = new Gesture();
gesture.addStroke(stroke);
ArrayList<Prediction> predictions = gLib.recognize(gesture);
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
String letter = prediction.name;
if (prediction.score > 1.0) {
Log.e("Found", letter);
points.clear();
}
}
}
};
Looper.loop();
}
}
}
所以基本上我们捕获触摸位置并从中创建一个手势,传递给 GestureLibrary 的识别()方法。如果一个手势被识别,那么我们打印它并清除触摸位置,以便可以识别一个新字母。
示例项目:Cursive_eclipse_project.zip