Libgdx屏幕截图方法不起作用
几周前,我实现了这个方法Libgdx屏幕截图方法不起作用,libgdx,screenshot,Libgdx,Screenshot,几周前,我实现了这个方法 它在libgdx1.3.1上运行得非常好。现在虽然我升级到了1.6.0,但它已经停止工作了。 当执行该方法时,它将冻结。我在一个按钮上实现了它,它被卡在了“向下点击”中,什么也没有发生 private void saveScreenshot() { try{ FileHandle fh; do{ fh = new FileHandle(files.getLocalStoragePath() + "scree
它在libgdx1.3.1上运行得非常好。现在虽然我升级到了1.6.0,但它已经停止工作了。 当执行该方法时,它将冻结。我在一个按钮上实现了它,它被卡在了“向下点击”中,什么也没有发生
private void saveScreenshot() {
try{
FileHandle fh;
do{
fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");
}while(fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 130, true);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
System.out.println("Path:" + fh.toString());
}catch(Exception e) {
}
}
private Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
w = pixmap.getWidth();
h = pixmap.getHeight();
if(yDown) {
ByteBuffer pixels = pixmap.getPixels();
int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++) {
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
}
pixels.clear();
pixels.put(lines);
}
return pixmap;
}
btnArrow.addListener(new ChangeListener() {
//photoshop "save" and "back" on arrow/back image to clarify.
@Override
public void changed(ChangeEvent event, Actor actor) {
saveScreenshot();
sharePhoto();
}
});
所以我认为可能的问题是libgdx对Pixmap类或位图类做了某种更改。因为通过facebook在该按钮上共享链接效果很好
我还打印了您在saveScreenshot()中看到的路径,它会返回以下内容
selinux_android_setcategory: no category for userid: 0, path: /data/data/com.sparc.tormt.android/lib
如果文件已经存在,是否因为这是一个无限循环而被卡住:
do {
fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");
} while(fh.exists());
是的,它卡住了。我读了文档,如果有,它应该覆盖已经存在的图像。但是你建议怎么做呢?因为它就像你说的那样被卡住了。把这个环去掉好吗?我认为这个循环是试图为文件找到一个“唯一”名称的失败尝试,应该做如下操作:
var name=files.getLocalStoragePath()+“screenshot”+(I++)+“.png”代码>(注意“i++”使循环每次迭代的名称都不同。但我只是猜测。)
do {
fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");
} while(fh.exists());