1

+ - × /

在按照此处的手势教程进行操作时,我遇到了一个非常奇怪的问题:http: //developer.android.com/resources/articles/gestures.html

在 Gesture Builder 中创建了 4 个独特的手势:+ - × /

加法和乘法是多笔画。减法和除法是单笔画法。

Activity 加载预构建的 GestureLibrary (R.raw.gestures),将 OnGesturePerformedListener 添加到 GestureOverlayView,并在检测到并执行手势时以 onGesturePerformed() 结束。

XML 中的活动布局

<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"

android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical"
/>

位于 onCreate()

    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mLibrary.load()) {
        finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);

位于 onGesturePerformed()

    ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

    // We want at least one prediction
    if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);

        // We want at least some confidence in the result
        if (prediction.score > 1.0) {
            // Show the spell
            Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
        }
    }

主要问题是未正确识别预构建的手势。例如,如果水平手势后跟垂直(加法)手势,则永远不会执行 onGesturePerformed()。如果垂直手势后面跟着水平手势,调用该方法。这也是 GesturesListDemo 的行为方式 ( GesturesDemos.zip @ code.google.com )。

此外,预测的手势最终成为不正确的手势。加法被识别为减法;乘除;减为加。这是完全不一致的。

最后,加法和减法手势通常共享相似的 Prediction.score,这很奇怪,因为它们在整个笔划上有所不同。

很抱歉这篇长篇文章 - 想要彻底。任何建议将不胜感激,谢谢大家。

4

3 回答 3

1

我意识到这是一个非常古老的问题,但尚未得到解答,这可能对某人有所帮助。我刚刚遇到了一个类似的问题,我的解决方案是用来gesture.getStrokesCount()区分单笔手势和多笔手势。

例子:

ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

// We want at least one prediction
if (predictions.size() > 0) {
    Prediction prediction = predictions.get(0);

    // We want at least some confidence in the result
    if (prediction.score > 1.0) {
        if (gesture.getStrokesCount() > 1) {
            // This is either add or multiply
        }
        else {
            // This is either subtract or divide
        }
    }
}
于 2014-05-22T15:44:03.317 回答
1

基于 Drew Lederman 他的回答,您可以使用 onGesturePerformed 的这种实现来始终获得最佳结果:

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {

        ArrayList<Prediction> predictions = store.recognize(gesture);
        double highScore = 0;
        String gestureName = "";

        for (Prediction prediction : predictions) {

            if (prediction.score > SCORE && prediction.score > highScore && 
                    store.getGestures(prediction.name).get(0).getStrokesCount() == gesture.getStrokesCount()) {

                highScore = prediction.score;
                gestureName = prediction.name;

            }
        }
        // Do something with gestureName
    }
于 2014-11-25T23:50:16.740 回答
0

是的,至少从 Android 2.3.3 开始受支持。但它非常不精确。检查第二个例子

于 2012-04-20T17:42:53.243 回答