2011-11-18 208 views
4

按照标题,GLSurfaceView在从暂停状态恢复后为空白。渲染器的onSurfaceCreatedonSurfaceChanged和在恢复后会被调用,但屏幕仍然为空!Android:GLSurfaceView在恢复应用后为黑色

下面是相关代码:

@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调用时。

请帮忙!

编辑: 下面是活动的onResume代码包含GLSurfaceView:(称为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(); 
} 

我没有做太多的的onResume为GLSurfaceView,和我没有”找到任何文件,建议我需要做任何事情,特别是要让我的EGL上下文恢复。

编辑2: 我也想指出,在我GLSurfaceView的构造函数调用setPreserveEGLContextOnPause(true)没有,遗憾的是,解决我的问题。

+0

也许发布您的onResume()代码,以及您在哪里初始化和设置GLSurfaceView视图 –

+0

已将其添加到我的文章。 – confusedKid

+0

尝试在onSurfaceCreated的结尾处设置'gl.glOrthof(0,1,0,1,-5,5);'并查看当您第一次运行应用程序时是否显示任何内容。如果没有什么区别,可以尝试在onDraw中设置'gl.glClearColor(1.0f,0.0f,0.0f,0.0f);'(即设置背景色为红色),然后将其更改为'gl.glClearColor(0.0f ,0.0f,1.0f,0.0f);调用onResume()后的'(即蓝色)。这样你就可以知道openGL是否对你正在做的事情做出反应 –

回答

0

这有点远,但是您是否尝试控制方向或配置更改(即,您是否有public void onConfigurationChanged(Configuration config)覆盖您的活动)?

我发现,如果我有onConfigurationChanges()在我的活动和android:screenOrientation="landscape"在我的清单文件覆盖,但是不android:configChanges="orientation"我得到的东西像你描述

+0

嗨,我强迫我的应用程序只加载横向方向,虽然我实际上没有重写onConfigurationChanges()。我只是把'android:screenOrientation =“landscape”'放在我的清单文件中。 – confusedKid

+0

尝试在清单文件中覆盖'onConfigurationChanges()'并添加相应的'configChanges =“orientation”'并查看是否修复了它,即使您只是从onConfigurationChanges调用super并且实际上没有做任何事情。它至少解决了我自己的应用程序的问题。不足之处在于,它会产生相当恼人的副作用,在较慢的设备上,应用程序会在正确切换到横向之前恢复之后,在您的应用程序的几分之一秒内显示一张混乱的纵向视图。 –

+0

你的意思是'onConfigurationChanged()'?我试图覆盖它,并按照您的建议添加了'configChanges',但问题依然存在。 – confusedKid

3

setPreserveEGLContextOnPause(真) 只支持使用Android 3.x设备和。 (即使支持,它也是设备特定的)

您正在寻找的解决方案是在GlSurfaceView的父活动中,您可以在活动的onPause方法中调用GlSurfaceView.onPause(),而在GlSurfaceView.onResume活动的onResume方法。

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

+0

你救了我的命...... – Dhrupal