2011-02-06 61 views
5

我在RENDERMODE_WHEN_DIRTY中使用了GLSurfaceView(sdk版本7)。文檔說我需要調用onPause/onResume,但沒有它就可以正常工作,所以我想知道。需要嗎?如果我不這樣會發生什麼?GLSurfaceView:我需要調用onPause/onResume嗎?

+0

即使離開應用程序並返回,它也能正常工作嗎?我一直需要實現onPause和onResume,以便在離開和返回應用程序後保持GlSurfaceView正常工作。 – Nitrex88 2011-02-06 16:31:07

回答

10

GLSurfaceView的的onPause的實現看起來是這樣的:

/** 
* Inform the view that the activity is paused. The owner of this view must 
* call this method when the activity is paused. Calling this method will 
* pause the rendering thread. 
* Must not be called before a renderer has been set. 
*/ 
public void onPause() { 
    mGLThread.onPause(); 
} 

你可以看到(和文檔的狀態),它暫停渲染線程。這導致在GLThread到stopEglLocked內部調用,它看起來是這樣的:

private void stopEglLocked() { 
     if (mHaveEgl) { 
      mHaveEgl = false; 
      mEglHelper.destroySurface(); 
      mEglHelper.finish(); 
      sGLThreadManager.releaseEglSurface(this); 
     } 
} 

所以你可以看到它破壞了表面,其昂貴的系統資源,並導致線程等待(),這也節省了系統資源,cpu,baterry等

因此,調用GLSurfaceView的onPause和onResume是絕對必需的。