4

我有一个GLSurfaceView渲染器,并且方法onSurfaceCreated只被调用一次(基本上只有在创建或重新创建活动时)。

当 EGL 上下文丢失并在正常活动生命周期中重新创建表面时,我需要测试行为,但是无论我做什么,我都无法将应用程序置于这种情况下。

我可以做些什么来模拟onSurfaceCreated再次调用的情况?如何让我的应用失去 EGL 上下文?

编辑:几天后,我注意到问题消失了,应用程序开始正常运行,如规范中所述:

public abstract void onSurfaceCreated (GL10 gl, EGLConfig config)
Called when the surface is created or recreated.
Called when the rendering thread starts and whenever the EGL context is lost.
The EGL context will typically be lost when the Android device awakes 
after going to sleep

在此之前,每次设备进入睡眠状态时,活动都会被破坏并在唤醒时重新创建,这使我无法测试代码是否OnSurfaceCreated运行良好。

现在,当睡眠/唤醒发生时,它的表现要好得多——它通常不会破坏活动,只是调用OnSurfaceCreated方法,这是我的目标。

4

4 回答 4

2

I'm not sure but the only thing that i can think of is to switch the GLSUrfaceView with a new one using the same GLSurfaceView.Renderer. That should create a new EGL context and call the onSurfaceCreated() a second time.

于 2013-04-24T15:38:49.363 回答
1

根据文档,在创建或重新创建表面时调用GLSurfaceView.Renderer.onSurfaceCreated 。

当渲染线程启动和 EGL 上下文丢失时调用。当 Android 设备在进入睡眠状态后醒来时,EGL 上下文通常会丢失。

您是否尝试过锁定设备的屏幕并解锁?

于 2013-04-22T15:53:15.547 回答
1

也许您可以查看onScreenStateChanged方法,但它适用于 API 16 或更高版本。如果您的意思是“正常活动生命周期”的“暂停和恢复活动”,也许您可​​以使用onPauseonResume跟踪它。

你到底想做什么onSurfaceCreated()requestRender()您可以通过方法强制渲染SurfaceView并在其中进行必要的onDrawFrame()计算GLSurfaceView.Renderer

于 2013-04-25T19:02:01.267 回答
0

如果您的视图位于片段中,您可以分离并重新附加片段。

于 2015-04-30T13:35:00.233 回答