2011-11-18 20 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