5

我目前正在为我的一个客户编写一个 Android 应用程序,该应用程序有一个带有 GLSurfaceView.Renderer 的 GLSurfaceView。

我的整个 OpenGL 东西都工作得很好(它基本上是另一个开发人员首先在 iOS 上编写的一个端口)。除了一件事。当视图被加载并因此 OpenGL 的东西被加载时,我的背景快速闪烁黑色,然后 OpenGL 开始正确渲染(使用我的背景)。所以我要做的是:

在 onSurfaceCreated 我从这个开始:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.9f + 0.1f * (21.0f / 255),
            0.9f + 0.1f * (36.0f / 255),
            0.9f + 0.1f * (31.0f / 255), 1.0f);

    // Here goes my other stuff, if I comment all my other stuff out I still get the flash at startup
}

在我的 onDrawFrame 方法中,我这样做:

@Override
public void onDrawFrame(GL10 gl) {

    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    // My stuff, again, if I comment all this stuff out I still get the flash...
}

因此,如果我删除除 onSurfaceCreated 中的 glClearColor(..) 之外的所有代码行,在设置实际背景颜色之前,我仍然会看到黑色闪烁。如果我只从我的代码中删除 glClearColor(..) (并因此保留所有其他 OpenGL 内容),那么所有内容都将呈现在黑色背景上。

我想看到的是我只是摆脱了黑色闪光,因此我的背景颜色在启动时被正确初始化......

有什么想法可以实现吗?

短剑

4

1 回答 1

5

我刚刚遇到了同样的问题,并通过使用 GLSurfaceView 的背景属性来解决它。这是我的 XML:

<view class="android.opengl.GLSurfaceView"
    android:id="@+id/glview"
    android:background="@drawable/window_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

其中 window_bg 与活动主题中指定的窗口背景相同。

完成此操作后,您可以在第一次调用 onDrawFrame() 时删除背景可绘制对象,使用布尔值来跟踪它是否已经完成:

boolean initialRenderHack;

//
// GLSurfaceView.Renderer
//
@Override
public void onDrawFrame(GL10 gl10) {

       // ... drawing code goes here ...

       // Remove the initial background
       if (!initialRenderHack) {
       initialRenderHack = true;
       view.post(new Runnable() {
           @Override
           public void run() {
               view.setBackgroundResource(0);
           }                
       });
    }

请注意,您只能从 UI 线程触摸 View 的背景属性,而不是运行 onDrawFrame() 的渲染线程,因此需要发布可运行文件。

在 Android 4.4 上,这提供了一个完美流畅的启动,没有可怕的不和谐的黑框。我还没有在较旧的 Android 上尝试过。

于 2013-12-02T11:55:42.333 回答