我需要能够在某个时刻捕捉到 GLSurfaceView
的图像。我有以下代码:
relative.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(relative.getDrawingCache());
relative.setDrawingCacheEnabled(false);
Log.v(TAG, "Screenshot height: " + screenshot.getHeight());
image.setImageBitmap(screenshot);
GLSurfaceView
包含在 RelativeLayout
中,但我也尝试直接使用 GLSurfaceView
来 try catch 图像。有了这个,我认为屏幕捕获了一个透明的图像,即那里什么都没有。任何帮助将不胜感激。
最佳答案
SurfaceView
和 GLSurfaceView
在它们的窗口中打洞以显示它们的表面。换句话说,它们具有透明区域。
因此,您无法通过调用 GLSurfaceView.getDrawingCache()
来捕获图像。
如果你想从 GLSurfaceView
获取图像,你应该在 GLSurfaceView.onDrawFrame()
中调用 gl.glReadPixels()
。
我修补了 createBitmapFromGLSurface
方法并在 onDrawFrame()
中调用它。
(原始代码可能来自 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);
}
关于java - 将 GLSurfaceView 的屏幕捕获到位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5514149/