onSurfaceChanged调用了两次 [英] onSurfaceChanged called twice

查看:579
本文介绍了onSurfaceChanged调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openGL ES创建一个Android应用程序,并且遇到以下问题:

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

当我最小化应用程序时,然后重新打开它,我的GLSurfaceView.Renderer中的onSurfaceChanged被调用2次.

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

我在onSurfaceChanged中有以下代码(以及在onSurfaceCreated,onDrawFrame中类似的代码):

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

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

所以我得到以下日志:


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

当我最小化时,将屏幕方向从纵向更改为横向,然后重新打开,onSurfaceChanged被调用了3次.日志是:

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
(...)

我的问题:这是一个错误吗?在我的应用程序中还是Android错误?有一种方法可以仅一次调用onSurfaceChanged吗? (因为在onSurfaceCreated中,我正在执行使应用程序变慢的代码)

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)

预先感谢

推荐答案

这不是错误. 就我个人而言,我在从GLSurfaceView继承的类中对onSurfaceChanged所做的工作并不多. 我仅根据新的分辨率设置视口,并将当前的分辨率尺寸保存为变量.

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.

不需要在onSurfaceChanged上重新创建诸如纹理之类的OpenGL资产,而是在ActivityCycle上选择正确的位置来创建和销毁OpenGL资产.

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.

我正在Android,Amazon Fire TV和OUYA等多个平台上运行我的游戏.我发现在所有这些平台上均可使用的功能是销毁onPause,onStop和onRestart上的GL资产,但我还保留一个标志以查看资产是否已被销毁,因此我不会在原始状态下将其销毁两次.

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.

我在onCreate,onRestart和onResume上创建了OpenGL资产(也使用一个标志来不创建它两次). 如果您不希望游戏在暂停时丢失其状态,则必须对游戏状态进行序列化并将其写入文件,并在以后恢复时加载它.

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.

希望这会有所帮助.

同样,您无法真正预测会收到多少onSurfaceCreated,尤其是在不同设备上的行为可能不同. 但是,您始终可以相信ActivityCycle的行为符合Google的描述.

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.

这篇关于onSurfaceChanged调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆