源码地址 : https://github.com/MasDennis/Rajawali/issues/478
有人提供了一些代码从一个opengl渲染器中提取截图
http://stackoverflow.com/questions/3310990/taking-screenshot-of-android-opengl
改变场景渲染器中 onDrawFrame即
public void onDrawFrame(GL10 gl) {
super.onDrawFrame(gl);
然后你可以粘贴以下代码:
if(screenshot){
int screenshotSize = mViewportWidth * mViewportHeight;
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
bb.order(ByteOrder.nativeOrder());
gl.glReadPixels(0, 0, mViewportWidth, mViewportHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
Bitmap bitmap = Bitmap.createBitmap(mViewportWidth, mViewportHeight, Bitmap.Config.RGB_565);
bitmap.setPixels(pixelsBuffer, screenshotSize-mViewportWidth, -mViewportWidth, 0, 0, mViewportWidth, mViewportHeight);
pixelsBuffer = null;
short sBuffer[] = new short[screenshotSize];
ShortBuffer sb = ShortBuffer.wrap(sBuffer);
bitmap.copyPixelsToBuffer(sb);
//Making created bitmap (from OpenGL points) compatible with Android bitmap
for (int i = 0; i < screenshotSize; ++i) {
short v = sBuffer[i];
sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
}
sb.rewind();
bitmap.copyPixelsFromBuffer(sb);
lastScreenshot = bitmap;
saveScreenshot(lastScreenshot);
screenshot = false;
}
就在您的渲染器创建下列方法:
public void takeScreenshot() {
screenshot = true;
}
public void saveScreenshot(Bitmap screenshot){
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "openglscreenshots");
file.mkdirs();
String path = file.toString();
String frametime = new DecimalFormat("##########").format(currentFrameTime);
FileOutputStream out = new FileOutputStream(path + "/" + frametime + ".png");
screenshot.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
你可以从Activity中调用mRenderer.takeScreenshot()进行截图。
在此存储使用系统时间文件名,您可以使用自定义计数器等等。