prevent的onPause从捣毁OpenGL上下文上下文、prevent、onPause、OpenGL

由网友(骨子里の殇)分享简介:我在写使用OpenGL ES(GLSurfaceView和GLSurfaceView.Renderer)Android应用程序。问题是,当用户切换应用程序,然后切换回我的应用程序中,GLSurfaceView破坏并重新创建GL上下文。这是它应该根据文档做的,但有什么办法,以prevent这种情况的发生?I'm wri...

我在写使用OpenGL ES(GLSurfaceView和GLSurfaceView.Renderer)Android应用程序。问题是,当用户切换应用程序,然后切换回我的应用程序中,GLSurfaceView破坏并重新创建GL上下文。这是它应该根据文档做的,但有什么办法,以prevent这种情况的发生?

I'm writing an Android application that uses OpenGL ES (GLSurfaceView and GLSurfaceView.Renderer). The problem is that when the user switches applications and then switches back to my app, the GLSurfaceView destroys and recreates the GL context. This is what it's supposed to do according to the documentation but is there a way to prevent this from happening?

这需要很长的时间来加载到纹理的背景下,我想以prevent无需重新加载它们。

It takes a long time to load textures into the context and i'd like to prevent having to reload them.

推荐答案

我觉得你在找什么的GLSurfaceView文档中讨论的:

I think what you are looking for is discussed in the GLSurfaceView documentation:

一个GLSurfaceView必须通知时   该活动被暂停和恢复。   GLSurfaceView客户端需要   调用的onPause()当活动   暂停和onResume()时,   活动恢复。这些调用允许   GLSurfaceView暂停和恢复   渲染线程,并且还允许   GLSurfaceView释放并重新创建   OpenGL的显示。

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

在使用标准的Andr​​oid SDK,您必须释放/重新创建你的背景下,只要该活动被暂停/恢复(包括屏幕方向变化)。不这样做将导致GL上下文被释放,当活动被装载回内存无法恢复。请记住,我们正在处理的非常有限的资源(尤其是在低规格的设备)。所以,简单的答案是:你不能prevent它不会破坏你的应用程序

When using the standard Android SDK, you must release/recreate your context whenever the activity is paused/resumed (including screen orientation changes). Not doing so will cause the GL context to be release and not restored when the activity is loaded back into memory. Remember that we are dealing with very limited resources (particularly on low-spec devices). So the short answer is: you can't prevent it without breaking your app.

假设你使用的是标准的Andr​​oid / OpenGL的框架,你需要做以下...

Assuming you are using the standard Android/OpenGL framework, you need to do the following...

在你的活动,确保您有以下覆盖的方法:

In your activity, ensure you have the following overridden methods:

public void onPause() {
    myGlSurfaceView.onPause();
}

public void onResume() {
    myGlSurfaceView.onResume();
}

任何你在GL环境之外持有仍然需要为preserved和手动但是恢复(位图,游戏状态等),对于这些你需要使用静态字段或类似共享$ P $的机制pferences。

Anything you hold outside the GL environment will still need to be preserved and restored manually however (bitmaps, game state, etc), for these you'll need to use static fields or a mechanism like SharedPreferences.

安卓3.x提供一个功能$p$pserve在GL上下文上暂停,而无需重新创建。然而,有几个注意事项:

Android 3.x provides a function to preserve the GL context on pause without needing to be recreated. However, there are several caveats:

的Andr​​oid 3.X功能不可用约。 90%的设备的市场上,此时 在该设备还必须支持多种EGL环境,目前还不清楚市场上有许多设备如何支持此。 Android 3.x features are not available to approx. 90% of devices on the market at this time The devices must also support multiple EGL contexts, it is unclear how many devices on the market currently support this.

使用一些API反射来检查的能力,有可能利用这一功能在支持的设备。但是,你仍然需要回落到重新创建上下文休息。在我看来,除非有更多的设备上运行的Andr​​oid 3.0倒不如使用设置preserveEGLContextOnPause暂缓和重点确保上下文的娱乐方式是充分的测试。

Using some API reflection to check capabilities, it may be possible to make use of this function on supporting devices. However, you would still need to fall back to recreating the context for the rest. In my opinion, until more devices run Android 3 it would be better to hold off using setPreserveEGLContextOnPause and focus on ensuring the context recreation approach is sufficiently tested.

阅读全文

相关推荐

最新文章