Android:GLSurfaceView 在恢复应用后是黑色的

标签 android opengl-es onresume glsurfaceview

根据标题,从暂停状态恢复后 GLSurfaceView 为空白。 Renderer的onSurfaceCreatedonSurfaceChangedonDrawFrame在恢复后被调用,但是屏幕还是空白!

相关代码如下:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);

    boxWidth = 0.5f;
    boxHeight = 0.5f;
    boxCenter = new float[] { 0.5f, 0.5f };

    Log.v("resume", "onSurfaceCreated");
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {      
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0, 1, 0, 1, -5, 5);
    gl.glMatrixMode(GL10.GL_MODELVIEW);

    viewHeight = height;
    viewWidth = width;

    Log.v("resume", "onSurfaceChanged");
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glLoadIdentity();

    float halfW = boxWidth/2.0f;
    float halfH = boxHeight/2.0f;

    float left = Math.max(boxCenter[0]-halfW, 0.001f);
    float right = Math.min(boxCenter[0]+halfW, 0.999f);
    float top = Math.min(boxCenter[1]+halfH, 0.999f);
    float bottom = Math.max(boxCenter[1]-halfH, 0.001f);

    boxHeight = Math.max(top - bottom, 0.05f);
    boxWidth = Math.max(right - left, 0.05f);
    boxCenter[0] = left + boxWidth/2.0f;
    boxCenter[1] = bottom + boxHeight/2.0f;

    float vertices[] = {
            left, top, 0.0f,
            left, bottom, 0.0f,
            right, bottom, 0.0f,
            right, top, 0.0f
    };

    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuf.asFloatBuffer();
    vertexBuffer.put(vertices);

    gl.glTranslatef(0.001f, -0.001f, -3.0f);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | 
            GL10.GL_DEPTH_BUFFER_BIT);

    vertexBuffer.position(0);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, 
            vertexBuffer);

    gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

    gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    Log.v("resume", "drawing");
}

我读到您需要在 onSurfaceChanged 中重新创建 GL 上下文,但我不确定如何,或者我是否已经在调用 onSurfaceCreated 时这样做了。

请帮忙!

编辑: 这是包含 GLSurfaceView 的 Activity 中的 onResume 代码:(此处称为 GLSurface)

public void onResume() {
   super.onResume();
   if (mGLSurface != null) {
   if (mRenderer != null) {
      float[] center = new float[2];
      center[0] = settings.getFloat("renderer_boxCenter0", 0.5f);
      center[1] = settings.getFloat("renderer_boxCenter1", 0.5f);
      mRenderer.setBoxCenter(center);
      mRenderer.setBoxWidth(settings.getFloat("renderer_boxWidth", 0.5f));
      mRenderer.setBoxHeight(settings.getFloat("renderer_boxHeight", 0.5f));
   }
   mGLSurface.onResume();
}

我没有在 GLSurfaceView 的 onResume 中做太多事情,我也没有找到任何文档表明我需要做任何特别的事情来恢复我的 EGL 上下文。

编辑 2: 我还想指出,不幸的是,在我的 GLSurfaceView 的构造函数中调用 setPreserveEGLContextOnPause(true) 并没有解决我的问题。

最佳答案

setPreserveEGLContextOnPause(真) 仅在 Android 3.X 及更高版本的设备上受支持。 (即便如此,是否支持也是设备特定的)

您要寻找的解决方案是在 GlSurfaceView 的父 Activity 中,您在 Activity 的 onPause 方法中调用 GlSurfaceView.onPause() 并在 Activity 的 onResume 方法中调用 GlSurfaceView.onResume()。

请注意,这会导致所有纹理和缓冲区丢失。此时您需要重新加载它们。

关于Android:GLSurfaceView 在恢复应用后是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8177576/

相关文章:

iphone - 如何在 cocos2d Xcode 中平滑移动按钮

c++ - 重用 opengl 纹理

android - 如何仅在最顶部的 fragment 上触发 onResume(),在后退时

android - 如何在其他 Activity 的 onResume 之后更新 ListView?

android - MPAndroidChart:如何隐藏图表背景?

android - Firebase 自动登录 - 需要过期/刷新吗?

android - 在 textView 中显示短信并且不使用 Toast

android - 无法在设备 '**' 上安装 Hello.apk : timeout

android - 如何在 opengl es 3+ 中获取屏幕外帧缓冲区内底层对象的像素颜色?

java - Android Canvas onDraw onResume时出现问题