0

基本上,我正在为安卓手机开发 OpenGL 动态壁纸。这是我的壁纸服务类。

public class Wallpaper extends GLWallpaperService {

private class MyEngine extends Engine {

    private GLRenderer glRenderer;
    private GL10 gl;
    private EGL10 egl;
    private EGLContext glc;
    private EGLDisplay glDisplay;
    private EGLSurface glSurface;

    private ExecutorService executor;
    private Runnable drawCommand;

    public void onCreate(final SurfaceHolder holder) {
        super.onCreate(holder);


        executor = Executors.newScheduledThreadPool(BIND_AUTO_CREATE);

        drawCommand = new Runnable() {
            public void run() {
                glRenderer.onDrawFrame(gl);
                egl.eglSwapBuffers(glDisplay, glSurface);
                if (isVisible()
                        && egl.eglGetError() != EGL11.EGL_CONTEXT_LOST) {
                    executor.execute(drawCommand);
                }
            }
        };

    }

    public void onSurfaceCreated(final SurfaceHolder holder) {
        super.onSurfaceCreated(holder);

        Runnable surfaceCreatedCommand = new Runnable() {
            public void run() {
                // Initialize OpenGL
                egl = (EGL10) EGLContext.getEGL();
                glDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
                int[] version = new int[2];
                egl.eglInitialize(glDisplay, version);
                int[] configSpec = { EGL10.EGL_RED_SIZE, 5,
                        EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5,
                        EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
                EGLConfig[] configs = new EGLConfig[1];
                int[] numConfig = new int[1];
                egl.eglChooseConfig(glDisplay, configSpec, configs, 1,
                        numConfig);
                EGLConfig config = configs[0];

                glc = egl.eglCreateContext(glDisplay, config,
                        EGL10.EGL_NO_CONTEXT, null);

                glSurface = egl.eglCreateWindowSurface(glDisplay, config,
                        holder, null);
                egl.eglMakeCurrent(glDisplay, glSurface, glSurface, glc);
                gl = (GL10) (glc.getGL());

                // Initialize Renderer
                glRenderer = new GLRenderer(Wallpaper.this);
                glRenderer.onSurfaceCreated(gl, config);
            }
        };
        executor.execute(surfaceCreatedCommand);

    }


    public void onSurfaceDestroyed(final SurfaceHolder holder) {

        Runnable surfaceDestroyedCommand = new Runnable() {
            public void run() {
                // Free OpenGL resources
                egl.eglMakeCurrent(glDisplay, EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
                egl.eglDestroySurface(glDisplay, glSurface);
                egl.eglDestroyContext(glDisplay, glc);
                egl.eglTerminate(glDisplay);

            }
        };
        executor.execute(surfaceDestroyedCommand);

        super.onSurfaceDestroyed(holder);
    }


    public void onSurfaceChanged(final SurfaceHolder holder,
            final int format, final int width, final int height) {
        super.onSurfaceChanged(holder, format, width, height);

        Runnable surfaceChangedCommand = new Runnable() {
            public void run() {
                glRenderer.onSurfaceChanged(gl, width, height);
            }
        };
        executor.execute(surfaceChangedCommand);

    }

    public void onDestroy() {

        executor.shutdownNow();

        super.onDestroy();
    }


    public void onVisibilityChanged(final boolean visible) {
        super.onVisibilityChanged(visible);

        if (visible) {
            executor.execute(drawCommand);
        }

    }


    public void onOffsetsChanged(final float xOffset, final float yOffset,
            final float xOffsetStep, final float yOffsetStep,
            final int xPixelOffset, final int yPixelOffset) {
        super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);

        Runnable offsetsChangedCommand = new Runnable() {
            public void run() {
                if (1 / xOffsetStep + 1 != 0f) {
                    glRenderer.setParallax(xOffset - 1f);
                }
            }
        };
        executor.execute(offsetsChangedCommand);

    }

}


public Engine onCreateEngine() {
    return new MyEngine();
}

}

它可以在我的手机(三星 Galaxy S2)和其他几款手机上完美运行,但有报道称它无法加载。动态壁纸根本无法预览,即使是 Galaxy S1 机型!由于这适用于我的手机,我无法弄清楚它有什么问题。我没有无法运行该程序的手机,所以我无法解决问题。


谢谢回复。但我使用的是 OpenGL ES 1.0,正如您在 EGL10 中看到的那样……导入的 GL10。哦,但我看到我确实导入了一个 GL11(OpenGL 1.1),这可能导致了我想的问题??

4

2 回答 2

0

尝试executor.shutdownNow();onDestroy(). onDestroy一些三星手机有调用过早的倾向,这会关闭线程。

于 2011-10-22T00:45:22.797 回答
0

Android 2.2 及更高版本支持 OpenGL ES 2.0。

目前可用的三星 Galaxy S1 和许多其他手机的运行版本早于 2.2。

我建议您仔细检查您的 API 级别并考虑回滚到 OpenGL ES 1.0 或编写一个包装器以将其用于不同的 API 级别。

于 2011-09-13T18:41:53.253 回答