GLSurfaceView:我需要调用onPause/onResume吗?

ADB*_*ADB 5 android opengl-es

我在RENDERMODE_WHEN_DIRTY中使用GLSurfaceView(sdk版本7).文档说我需要调用onPause/onResume,但没有它就可以正常工作,所以我很想知道.需要吗?如果我不这样做会怎么样?

Lio*_*ior 11

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();
}
Run Code Online (Sandbox Code Playgroud)

您可以看到(以及文档说明)它暂停渲染线程.这会导致GLThread中的内部调用stopEglLocked,如下所示:

 private void stopEglLocked() {
        if (mHaveEgl) {
            mHaveEgl = false;
            mEglHelper.destroySurface();
            mEglHelper.finish();
            sGLThreadManager.releaseEglSurface(this);
        }
 }
Run Code Online (Sandbox Code Playgroud)

所以你可以看到它破坏了一个昂贵的系统资源的表面,并导致线程wait(),这也节省了系统资源,CPU,baterry等.

因此,确实需要调用GLSurfaceView的onPause和onResume.