在 Android 的主线程中使用 OpenGL [英] Using OpenGL from the main thread on Android

查看:17
本文介绍了在 Android 的主线程中使用 OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择选项菜单中的项目时调用 gles20 方法.

I would like to call a GLES20 method when an item from the options menu is selected.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            break;
        // ...
    }
}

这不起作用,因为我在 main 线程中,而不是在 GLThread 中.它说:

This does not work since I am in the main thread and not in GLThread. It says:

在没有当前的情况下调用 OpenGL ES API上下文(每个线程记录一次)

call to OpenGL ES API with no current context (logged once per thread)

但是我必须做些什么才能让事情正常进行?

But what do I have to do to get things working?

推荐答案

我自己找到了答案:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            // GLSurfaceView.queueEvent
            surface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                }
            });
            break;
        // ...
    }
}

这篇关于在 Android 的主线程中使用 OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆