onSurfaceChanged called twice

别来无恙 提交于 2019-12-07 05:23:43

问题


I'm creating an Android application using openGL ES, and I have following problem:

When I minimize the application, then reopen it, onSurfaceChanged in my GLSurfaceView.Renderer is called 2 times.

I have the following code in onSurfaceChanged (and similar code in onSurfaceCreated,onDrawFrame):

Log.e("onSurfaceChanged",Integer.toString(width)+" "+Integer.toString(height));

So I get the following log:

onSurfaceCreated
onSurfaceChanged 480 800
onDrawFrame
onSurfaceChanged 480 800
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
(...)

When I minimize, change screen orientation from portrait to landscape, then reopen, onSurfaceChanged is called 3 times. And the log is:

onSurfaceCreated
onSurfaceChanged 480 800
onDrawFrame
onSurfaceChanged 480 800
onDrawFrame
onDrawFrame
onDrawFrame
onSurfaceChanged 800 480
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
onDrawFrame
(...)

My question: is this a bug? In my application or is an Android bug? There is a way to onSurfaceChanged being called just once? (because in onSurfaceCreated I'm executing code that slow down the application)

Thanks in advance


回答1:


It's not a bug. Personally I don't do much on onSurfaceChanged in my class which inherits from GLSurfaceView. I only set the viewport according to the new resolution and save the current resolution dimensions to a variable.

OpenGL assets such as textures don't need to be recreated at onSurfaceChanged, instead choose the right place on the ActivityCycle to create and destroy your OpenGL assets.

I am running my game on several platforms such as Android, Amazon Fire TV and OUYA. What I found that works on all these platforms is to destroy the GL assets on onPause, onStop and onRestart but I also keep a flag to see if the assets are already destroyed so I won't destroy it twice in a raw.

I create the OpenGL assets at onCreate, onRestart and onResume(also using a flag to not create it twice). If you don't want the game to lose it's state while pausing you will have to serialize the game state and write it to a file and load it later when you resume.

Hope this helps.

Again, you can't really predict how many onSurfaceCreated you will receive especially that it might behave differently on different devices. However, you can always trust the ActivityCycle to behave as described by Google.



来源:https://stackoverflow.com/questions/30034277/onsurfacechanged-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!