Android - 手势不想工作

发布于 2024-11-19 12:54:22 字数 1308 浏览 3 评论 0原文

我运行了 Gestures Builder 应用程序,为向左/向右滑动创建了手势文件,并编写了以下代码:

public class MainActivity extends Activity implements OnGesturePerformedListener {

    private GestureLibrary mGestureLibrary;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
        View inflate = getLayoutInflater().inflate(R.layout.main, null);
        gestureOverlayView.addView(inflate);
        gestureOverlayView.addOnGesturePerformedListener(this);
        mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (mGestureLibrary == null) {
            finish();
        }

        setContentView(gestureOverlayView);
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);

        for (Prediction prediction : predictions) {
            if (prediction.score > 1.0) {
                Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

手势位于 /raw/ 中,但当我尝试测试它时,应用程序什么也没说(它成功加载手势,调用事件 onGesturePerformed,但是手势无法识别)。手势在 Gestures Buileder 中完美运行,那么我的错误在哪里?

I ran Gestures Builder app, created gestures file for slide left/right and wrote this code:

public class MainActivity extends Activity implements OnGesturePerformedListener {

    private GestureLibrary mGestureLibrary;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
        View inflate = getLayoutInflater().inflate(R.layout.main, null);
        gestureOverlayView.addView(inflate);
        gestureOverlayView.addOnGesturePerformedListener(this);
        mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (mGestureLibrary == null) {
            finish();
        }

        setContentView(gestureOverlayView);
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);

        for (Prediction prediction : predictions) {
            if (prediction.score > 1.0) {
                Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

gestures are in /raw/, but the app says nothing when I try to test it (it loads gesture successfully, the event onGesturePerformed is called, but the gestures are not recognized). The gestures work perfectly in Gestures Buileder, so where is my mistake?

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

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

发布评论

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

评论(2

恍梦境° 2024-11-26 12:54:22

您仍然需要检查预测的名称,该名称应与您的操作之一的名称相匹配。测试相等性,然后执行您的逻辑:

String action = predictions.get(0).name;
 if("right".equals(action){
}

You need to still check the name of the prediction which should match the name of one of your actions. Test for equality and then perform your logic:

String action = predictions.get(0).name;
 if("right".equals(action){
}
无法回应 2024-11-26 12:54:22

在使用 mGestureLibrary 之前,您可能需要调用 load() 。根本没有记录它,但这就是 Lars 在本示例中所做的,并且它对我有用: http://www.vogella.com/articles/AndroidGestures/article.html

在 IDE 中,您应该能够看到 GestureStore HashMap (mNamedGestures) 中的条目。

You may need to call load() on mGestureLibrary before using it. Not that it's documented at all, but that's what Lars does in this example and it works for me: http://www.vogella.com/articles/AndroidGestures/article.html

In the IDE you should be able to see entries in the GestureStore HashMap (mNamedGestures).

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