ANDROID:将OpenGL onDraw转换为位图,然后在ImageView中显示位图

时间:2013-12-16 03:03:19

标签: android opengl-es bitmap

好日子先生。

我在自己的imageView中显示来自openGL的位图时遇到问题。

首先,我正在创建一个内部有3D对象的openGL(像立方体,球体等)

然后,我将openGL onDraw视图转换为位图(我使用此代码)

public static Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
        throws OutOfMemoryError {
    int bitmapBuffer[] = new int[w * h];
    int bitmapSource[] = new int[w * h];
    IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
    intBuffer.position(0);

    try {
        gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
        int offset1, offset2;
        for (int i = 0; i < h; i++) {
            offset1 = i * w;
            offset2 = (h - i - 1) * w;
            for (int j = 0; j < w; j++) {
                int texturePixel = bitmapBuffer[offset1 + j];
                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;
                bitmapSource[offset2 + j] = pixel;
            }
        }
    } catch (GLException e) {
        return null;
    }

    return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}

嗯,它捕获onDraw的视图并将其转换为位图,但我需要的是使openGL成为后台进程,将其视图转换为位图,在主要活动中的imageview中显示位图。

我删除了

setContentView(ourSurfaceView)

在我的mainActivity中,但在删除它并使用我的layout.xml更改之后 我从openGL创建的位图无法正常运行。 (对不起我的英文)

先进的谢谢。 :)

1 个答案:

答案 0 :(得分:0)

4.2(API Level 17)EGL被引入android sdk。 EGL使得屏幕外渲染成为可能。 看看这个答案:https://stackoverflow.com/a/13504396/1404216