原文地址:https://github.com/libgdx/libgdx/wiki/Taking-a-Screenshot
之前在网上找到些关于libgdx截屏功能的代码,但是保存下来的图片跟手机上显示的有点不一样,截屏保存下来的图片感觉是透明度丢失了一样,图片偏灰色,具体原因不清楚。
这是libgdx 截屏功能本身的一个bug,参考以上连接,官方已经修复了该功能。
byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true); // this loop makes sure the whole screenshot is opaque and looks exactly like what the user is seeing for(int i = 4; i < pixels.length; i += 4) { pixels[i - 1] = (byte) 255; } Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888); BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); PixmapIO.writePNG(Gdx.files.external("mypixmap.png"), pixmap); pixmap.dispose();