SurfaceView
and GLSurfaceView
在窗户上打孔以显示其表面。换句话说,它们有透明的区域。
所以你不能通过调用来捕获图像GLSurfaceView.getDrawingCache()
.
如果你想从以下位置获取图像GLSurfaceView
,你应该调用gl.glReadPixels()
in GLSurfaceView.onDrawFrame()
.
我打了补丁createBitmapFromGLSurface
方法并调用它onDrawFrame()
.
(原始代码可能来自skuld https://stackoverflow.com/users/839992/skuld的代码。)
private 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);
}