如果我理解正确,您想用新手势替换现有手势吗?
因此,当用户输入库中没有的手势时,您的应用会要求用户选择他们希望替换的手势?根据您的问题,我假设当用户绘制小写字母时a
(如果a
不在库中),用户会看到您的应用当前支持的所有可用手势/字母的列表。然后,用户选择大写A
,现在A
必须将大写替换为小写a
。在下面的代码中,oldGesture 是对应的 Gesture A
。并且newGesture
是刚刚绘制的手势。
其过程是:删除旧手势,使用旧手势名称添加新手势。要删除手势,请使用 GestureLibrary.removeGesture(String, Gesture):
public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
ArrayList<Prediction> predictions = gesturelib.recognize(gesture);
if (predictions.size() > 1) {
for(Prediction prediction: predictions){
if (prediction.score > ...) {
} else {
if (user wants to replace) {
showListWithAllGestures(gesture);
}
}
}
}
}
public void showListWithAllGestures(Gesture newGesture) {
....
....
// User picks a gesture
Gesture oldGesture = userPickedItem.gesture;
String gestureName = userPickedItem.name;
// delete the gesture
gesturelib.removeGesture(gestureName, oldGesture);
gesturelib.save();
// add gesture
gesturelib.addGesture(gestureName, newGesture);
gesturelib.save();
}
要获取所有可用手势的列表:
// Wrapper to hold a gesture
static class GestureHolder {
String name;
Gesture gesture;
}
使用 GestureLibrary.load() 加载手势:
if (gesturelib.load()) {
for (String name : gesturelib.getGestureEntries()) {
for (Gesture gesture : gesturelib.getGestures(name)) {
final GestureHolder gestureHolder = new GestureHolder();
gestureHolder.gesture = gesture;
gestureHolder.name = name;
// Add `gestureHolder` to a list
}
}
// Return the list that holds GestureHolder objects
}
编辑:
抱歉,但我建议的检查:if (wants to replace)
在代码中的错误位置执行。
if (predictions.size() > 1) {
// To check whether a match was found
boolean gotAMatch = false;
for(int i = 0; i < predictions.size() && !gotAMatch; i++){
if (prediction.score > ... ) {
....
....
// Found a match, look no more
gotAMatch = true;
}
}
// If a match wasn't found, ask the user s/he wants to add it
if (!gotAMatch) {
if (user wants to replace) {
showListWithAllGestures(gesture);
}
}
}