Libgdx截图方法不起作用

时间:2015-05-23 11:09:54

标签: libgdx screenshot

几周前,我实施了这种方法https://github.com/libgdx/libgdx/wiki/Take-a-Screenshot 它与libgdx 1.3.1配合得很好。现在虽然我升级到1.6.0但它已停止工作。 执行该方法时,它会冻结。我把它实现在一个按钮上,它被卡在" downclick"没有更多的事情发生。

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();

        }
    });

我也将图像分享给facebook。当然这个方法在AndroidLauncher中并通过接口传递。在这里我获取截图:

public void sharePhoto() {
    Matrix matrix = new Matrix();

    String filePath = (files.getLocalStoragePath() + "screenshot" + ".png");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    Bitmap rotateBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);


    //Starts sharing process
    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(rotateBit)
            .build();
    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();
    share.show(content);

}

所以我认为可能是问题是libgdx对Pixmap类或某种Bitmap类进行了更改。由于在该按钮上通过Facebook共享链接工作正常。

我还在saveScreenshot()中看到了路径,并且它返回了

selinux_android_setcategory: no category for userid: 0, path: /data/data/com.sparc.tormt.android/lib

1 个答案:

答案 0 :(得分:2)

是否卡住,因为如果文件已经存在,这是一个无限循环:

do {
   fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");
} while(fh.exists());