2011-03-17 27 views
0

我正在運行帶有SurfaceView的應用程序。當表面視圖可見時,我按下「電源」按鈕鎖定手機,然後返回到應用程序,視角關閉。它似乎在我的「短而寬」的觀點左邊呈現出一個「高大而瘦」的觀點。手機鎖定後Open GL SurfaceView的佈局問題

當我看着onSurfaceChanged時,它將在手機啓動時調用'width = 480 height = 800',然後在手機解鎖後再次調用'width = 800 height = 480'。我使用從onSurfaceChanged收集的正確的新寬度/高度數據調用gl.glFrustumf()每個幀,但是仍然使我的窗口看起來很瘦,很高,有什麼想法?

編輯: 也許它與我的視圖結構有關。

活動內容查看稱爲的MainView ..

  MainView 
     /  \ 
    UIView  SurfaceView (I look wrong) 
    /
UI Elements 
(These all look correct) 

編輯#2:

截圖:

LockScreenFail

的全球和 '下載更多' 按鈕被描繪在3D。 Square in是UI視圖的子項(UIView的其他子項也正確顯示)。看起來好像SurfaceView認爲寬度是高度,高度是寬度。我做了表面觀的寬度和高度的更多的打印,並獲得該輸出:

--Phone On Unlock Screen 
--Activity onWindowFocusChanged called 
I/System.out(8817): ***********UI Width: 480 UI Height: 800 
I/System.out(8817): ***********Main Width: 480 Main Height: 800 
I/System.out(8817): ***********GL Width: 480 GL Height: 800 

--App continues to run. These are triggered in GLSurfaceView onLayout 
I/System.out(8061): ***********UI Width: 800 UI Height: 480 
I/System.out(8061): ***********Main Width: 800 Main Height: 480 
I/System.out(8061): ***********GL Width: 800 GL Height: 480 

I/System.out(8061): ***********UI Width: 800 UI Height: 480 
I/System.out(8061): ***********Main Width: 800 Main Height: 480 
I/System.out(8061): ***********GL Width: 800 GL Height: 480 

所以寬度和高度出現onWindowFocusChanged後的某個時間來修復自己,但圖形它永遠不會看起來是正確的。

編輯#3:

public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

} 

:)

public void onSurfaceChanged(GL10 gl, int width, int height) { 
    mWidth = width; 
    mHeight = height; 

    mAspectRatio = (float) width/height; 
    mSurfaceChangedID++; 
} 

然後我,因爲我有3D渲染的東西的想法和其他人作爲2D UI元素我設置投影在的onDraw

每一幀
public void onDrawFrame(GL10 gl) { 

    gl.glClearColor(mClearColorR, mClearColorG, mClearColorB, 1.0f); 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    for(each item) { 

     if(item.Is3d()) { 
      //For 3d Elements: 
      gl.glMatrixMode(GL10.GL_PROJECTION); 
      gl.glLoadIdentity();      
      gl.glFrustumf(mAspectRatio*-mHalfViewAngleTan, mAspectRatio*mHalfViewAngleTan, -mHalfViewAngleTan, mHalfViewAngleTan, mNearZClip, mFarZClip);     

      //Enable Lighting, Setup fog 
      ... 

     } else { 
      //For UI Elements: 

      gl.glMatrixMode(GL10.GL_PROJECTION); 
      gl.glLoadIdentity(); 
      gl.glOrthof(-0.5f*mAspectRatio, 0.5f*mAspectRatio, -0.5f, 0.5f, -1, 1); 

      //Disable Lighting, Disable fog, Setup blending 
      ... 
     } 

     //Setup client state and vertex buffers 
     ... 

     gl.glDrawArrays(GL10.GL_TRIANGLES, 0, mdl.getVtxCount()); 
    } 
}   

編輯#4:

原來這也有景觀做的,我改變了SurfaceView是如何加入到其父...

這是...

getMainView().addView(m3DView); 

現在是...

// add to parent view 
    { 
     Display display = getWindow().getWindowManager().getDefaultDisplay(); 
     android.widget.AbsoluteLayout absLayout = new android.widget.AbsoluteLayout(this); 
     absLayout.setLayoutParams(new android.view.ViewGroup.LayoutParams(display.getWidth(), display.getHeight())); 
     absLayout.addView(m3DView, new android.view.ViewGroup.LayoutParams(display.getWidth(), display.getHeight())); 
     getMainView().addView(absLayout); 
    } 

回答

2

當活動暫停並恢復時,必須通知GLSurfaceView。 GLSurfaceView客戶端需要在活動暫停時調用onPause(),並在活動恢復時調用onResume()。這些調用允許GLSurfaceView暫停並恢復渲染線程,並允許GLSurfaceView釋放並重新創建OpenGL顯示。

這應該解決您的問題。

+0

我已經調用onPause和onResume,似乎沒有太大的改變: - 我確實發現,如果在解鎖手機之後,我打印SurfaceView的getWidth和getHeight,它返回爲w = 480 h = 800,而不是反過來,所以我覺得我正在管理視圖不正確。 – TurqMage 2011-03-18 15:48:03

+0

啊..我真的錯過了這一點。你可以張貼截圖來顯示你的意思是「瘦而高」 – Yuriy 2011-03-18 19:30:38

+0

編輯更多信息 – TurqMage 2011-03-18 20:42:26