2011-04-18 104 views
2

我有一个android全屏开启的应用程序。android:在设备旋转时关闭opengl ES上下文切换

当设备从纵向旋转到横向时,gl背景被破坏并重新创建。

有没有办法避免这种情况?即始终保持纵向还是横向?

编辑:我已经有这个代码在我的活动:

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    mGLSurfaceView.onResume();  
} 

@Override 
protected void onPause() 
{  
    super.onPause(); 
    mGLSurfaceView.onPause(); 
} 

回答

1

如果你想保持你的GL上下文安全而不被破坏,那么你需要重写你的Activity类的onPause()和的onResume功能()通过调用你的GLSurfaceView()。OnPause和GLSurfaceView().Resume()。

@Override 
protected void onPause() 
{ 
super.onPause(); 
GLSurfaceView_Class.OnPause(); 
} 

//对于onResume也是一样。

如果你想限制你的应用程序在potrait或landscape中,那么你可以在清单文件中定义它。

android:screenOrientation =“landscape”in your activity tag。

我希望这有助于

+0

谢谢,我已经在glsurfaceview上调用onpause和onresume,但它仍然每次都重新创建glcontext。 – clamp 2011-08-25 08:40:12

1

这是可能的,当你的应用程序正在旋转,因此不会被摧毁

而不是重写整个GlSurfaceView你可以只提供一个EGLContextFactory改变GlContext如何创建/销毁,以保留您的GlContext。

public class ConfigChangesGlSurfaceView extends GLSurfaceView { 

    private static final int EGL_CONTEXT_CLIENT_VERSION_VALUE = 2; 
    private static EGLContext retainedGlContext = null; 
    private boolean changingConfigurations = false; 

    public ConfigChangesGlSurfaceView(Context context) { 
     super(context); 
     init(); 
    } 

    public ConfigChangesGlSurfaceView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    private void init() { 
     changingConfigurations = false; 
     setEGLContextClientVersion(EGL_CONTEXT_CLIENT_VERSION_VALUE); 
     setEGLContextFactory(new GLSurfaceView.EGLContextFactory() { 
      private final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; 

      public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) { 
       if (retainedGlContext != null) { 
        // Return retained context 
        final EGLContext eglContext = retainedGlContext; 
        retainedGlContext = null; 
        return eglContext; 
       } 

       int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_VALUE, EGL10.EGL_NONE}; 
       return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, attrib_list); 
      } 

      public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) { 
       if (changingConfigurations) { 
        // Don't destroy and retain 
        retainedGlContext = context; 
        return; 
       } 

       if (!egl.eglDestroyContext(display, context)) { 
        throw new RuntimeException("eglDestroyContext failed: error " + egl.eglGetError()); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onPause() { 
     changingConfigurations = getActivity().isChangingConfigurations(); 
     super.onPause(); 
    } 

    private Activity getActivity() { 
     Context context = getContext(); 
     while (!(context instanceof Activity) && context instanceof ContextWrapper) { 
      context = ((ContextWrapper) context).getBaseContext(); 
     } 
     if (context instanceof Activity) { 
      return (Activity) context; 
     } 
     throw new IllegalStateException("Unable to find an activity: " + context); 
    } 
}