在Android上的主线程中使用OpenGL

Mat*_*ias 9 multithreading android opengl-es

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

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

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

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

但是我需要做些什么才能让事情发挥作用?

Mat*_*ias 17

我自己找到了答案:

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;
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)