Android 手势、单笔划和多次冲程产生不一致的结果

发布于 2024-11-16 00:23:57 字数 2164 浏览 3 评论 0原文

+ - × /

我在遵循此处的手势教程时遇到了一个非常奇怪的问题: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,这很奇怪,因为它们在整个笔画上有所不同。

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

+ - × /

I'm having a really weird problem while following the Gestures tutorial here: http://developer.android.com/resources/articles/gestures.html.

4 unique gestures are created in Gesture Builder: + - × /

Add and multiply are multi-stroke. Subtract and divide are single stroke.

The Activity loads the pre-built GestureLibrary (R.raw.gestures), adds an OnGesturePerformedListener to the GestureOverlayView, and ends with onGesturePerformed() when a gesture is detected & performed.

Activity layout in 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"
/>

Located in onCreate()

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

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

Located in 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();
        }
    }

The main problem is the pre-built gestures are not being recognized correctly. For example, onGesturePerformed() is never performed if a horizontal gesture is followed by a vertical (addition). The method is called if a vertical gesture is followed by a horizontal. This is how GesturesListDemo behaves too (GesturesDemos.zip @ code.google.com).

Furthermore, the predicted gesture ends up being the incorrect gesture. Add is recognized as subtract; multiply as divide; Subtract as add. It's completely inconsistent.

Finally, add and subtract gestures typically share similar Prediction.score's, which is weird since they differ by an entire stroke.

Sorry about the long post -- wanted to be thorough. Any advice would be greatly appreciated, thanks all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-11-23 00:23:57

我意识到这是一个非常古老的问题,但尚未得到解答,这可能会对某人有所帮助。我刚刚遇到了类似的问题,我的解决方案是使用 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
        }
    }
}

I realize this is a really old question, but it hasn't been answered yet and this might help someone. I just came across a similar problem and my solution was to use gesture.getStrokesCount() to differentiate between single- and multi-stroke gestures.

Example:

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
        }
    }
}
默嘫て 2024-11-23 00:23:57

基于 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
    }

Building on Drew Lederman his answer, you can use this implementation of onGesturePerformed to always get the best result:

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
    }
半城柳色半声笛 2024-11-23 00:23:57

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

Yes, are supported, at least from Android 2.3.3. But it is heavily imprecise. Check the second example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文