代码为:
package com.example.bird;
import java.io.File;
import java.nio.ByteBuffer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.utils.ScreenUtils;
public class ScreenshotFactory {
private static int counter = 1;
private static int time = 50;// 次数
private static float df = 0.1f;// 间隔
private static void saveScreenshot_ex() {
try {
FileHandle fh;
do {
new File(Gdx.files.getExternalStoragePath() + "save").mkdir();
fh = new FileHandle(Gdx.files.getExternalStoragePath()
+ "save/" + "screenshot" + counter++ + ".png");
} while (fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), true);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
} catch (Exception e) {
}
}
private static Pixmap getScreenshot(int x, int y, int w, int h,
boolean yDown) {
final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
if (yDown) {
// Flip the pixmap upside down
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;
}
public static void saveScreenshot(float delta) {
if (time < 50) {
if (df > 0.1f) {
time += 1;
df = 0;
saveScreenshot_ex();
}
}
}
}
使用方法:(需要注意这个,最后一个参数true,翻转屏幕使用的)
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), true);
ScreenshotFactory.saveScreenshot(delta) 使用,这里将存储路径指向了T卡的save文件夹。
在游戏的渲染里面,每隔0.1s存储一下,存储50次即可。
制作gif:
下载EASY GIF Animator 工具,中文说明,调整下图片大小以及帧率,保存gif即可了。