我已经使用image
软件包,从存储器临时目录上的一组图像中生成了一个动画GIF图像。但是我不知道是否可以自定义生成的GIF图像的帧速率。
这是我当前一代的代码:
void _mergeScreenshotsIntoGif({
required int gameStepsCount,
required String tempStorageDirPath,
required String baseFilename,
}) async {
final animation = image.Animation();
final imageDecoder = image.PngDecoder();
for (var stepIndex = 1; stepIndex <= gameStepsCount; stepIndex++) {
final currentFile = File(
'$tempStorageDirPath${Platform.pathSeparator}${baseFilename}_$stepIndex.png');
final currentImageBytes = await currentFile.readAsBytes();
final currentImage = imageDecoder.decodeImage(currentImageBytes)!;
animation.addFrame(currentImage);
}
final gifData = image.encodeGifAnimation(animation);
if (gifData == null) {
//TODO handle gif generation error
return;
}
final destinationFile =
File('$tempStorageDirPath${Platform.pathSeparator}$baseFilename.gif');
await destinationFile.writeAsBytes(gifData);
}
我看了这个包的文档,但没能满足我的需求。
那么这有可能吗?
也许这是一个天真的实现,但我设法做到了"慢";动画。通过多次添加不同的图像。
更换
animation.addFrame(currentImage);
带有
for (var frameRepetitionIndex = 0;
frameRepetitionIndex < 10;
frameRepetitionIndex++) {
animation.addFrame(currentImage);
}