让 Android 应用程序在按下主页按钮后保留其 OpenGL 上下文
首先介绍一下背景:我正在为 Android 和 iPhone 平台开发一款视频游戏。根据 iPhone 的工作方式,当用户按下主页按钮并稍后返回游戏时,在大多数情况下,游戏会从上次中断的地方继续进行,不会出现任何卡顿。我立即跳回渲染和游戏循环。对我来说,在 iOS 平台上进行设置非常容易。在与谷歌结果进行了数小时的角力后,在 Android 上实现这一目标让我非常愤怒 :P
我有自己的 iPhone 和 Android OpenGL 设置,一切都运行良好。我认为问题的根源在于我需要一个 SurfaceHolder 来创建 OpenGL 上下文。这是糟糕的部分,当屏幕失去游戏焦点时(即按下主页按钮),Android 会在我的 SurfaceView 类中调用 surfaceDestroyed 并基本上杀死我的 opengl 上下文。当调用 surfaceCreated 时,我可以使用新的 SurfaceHolder 重新创建一个新的,但随后我需要重新加载我的所有艺术资源,这违背了我想要完成的一切目的。
我可以以某种方式阻止 Android 操作系统杀死我的 Surface Holder,是否有某种自定义视图可以用来让它工作?清单中是否有一些设置可以帮助我(我对此表示怀疑,因为我已经彻底测试了大部分标志)?我知道这是可能的,因为《愤怒的小鸟》在 Android 操作系统上完美地做到了这一点。
First a bit of context: I'm developing a video game for both the Android and iPhone platforms. The way the iPhone works, when a user hits the home button and returns to the game later, in most circumstances the game will pick up RIGHT where it left off with no hicups. I immediately jump back into my rendering and game loop. Setting this up on the iOS platform was absolutely easy for me to do. Accomplishing this on the Android has left me in a fit of rage after hours of wrestling with google results :P
I have my own OpenGL setup for both the iPhone and Android, and everything has been working great. The root of the problem, I believe, is that I need a SurfaceHolder to create an OpenGL Context. Here's the sucky part, when the screen loses focus of the game (ie the home button was hit), Android calls surfaceDestroyed in my SurfaceView class and basically KILLS my opengl context. I could recreate a new one with a new SurfaceHolder when surfaceCreated is called, but then I need to reload all of my art assets which defeats the purpose of everything I'm trying to accomplish.
Can I somehow prevent the Android OS from killing my surface holder, is there some sort of custom view I can use to get this to work? Is there some setting in the Manifest that can help me out here (I doubt it as I have thoroughly tested most of the flags)? I know this is possible because Angry Birds does this perfectly on the Android OS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论