我有一个程序,可以加载 spritesheet,在表单上创建每个帧的 BufferedImages,并将其写入动画 gif。使用 Elliot Kroo 在 使用 ImageIO 创建动画 GIF? 上提供的课程,我能够成功输出文件。然而,gif 动画效果不太正确。我提供的工作表是具有透明背景的 .png,因此每个后续帧都放在最后帧的顶部,通过透明背景显示差异(示例)。这是使用 gif writer 的代码:
ImageOutputStream output = new FileImageOutputStream(new File(savePath));
GifSequenceWriter writer = new GifSequenceWriter(output, data[0].getType(), delay, true);
for(BufferedImage bi:data)
{
writer.writeToSequence(bi);
}
writer.close();
output.close();
其中 data 是每个帧作为 BufferedImage 的数组(我也检查过,似乎不是问题)。这是 .gif 或 Java ImageWriter 的限制吗?或者我可以在某处编辑设置来防止这种情况发生吗?如果不需要的话我宁愿不放背景。
I have a program that loads a spritesheet, creates BufferedImages of each frame on the sheet, and writes it to an animated gif. Using the class provided by Elliot Kroo on Creating animated GIF with ImageIO?, I was able to successfully output a file. However, the gif doesn't animate quite right. The sheet I provided was a .png with a transparent background, so every subsequent frame is just put on top of the last frames, with differences showing through the transparent background (Example). Here's the code that uses the gif writer:
ImageOutputStream output = new FileImageOutputStream(new File(savePath));
GifSequenceWriter writer = new GifSequenceWriter(output, data[0].getType(), delay, true);
for(BufferedImage bi:data)
{
writer.writeToSequence(bi);
}
writer.close();
output.close();
Where data is an array of each frame as a BufferedImage (which I have also checked, and don't seem to be the problem). Is this a limitation of .gifs or the Java ImageWriters? Or can I edit a setting somewhere to prevent this? I'd rather not put a background if I don't have to.
发布评论
评论(1)
假设
data
是BufferedImage
,的imageType
参数href="http://elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java" rel="nofollow noreferrer">GifSequenceWriter
构造函数可能是TYPE_INT_ARGB
用于从.png
文件读取的数据。我希望transparentColorFlag
为true
,但您必须凭经验确定transparentColorIndex
。另请参阅此答案。
Assuming
data
is an array ofBufferedImage
, theimageType
parameter to theGifSequenceWriter
constructor would likely beTYPE_INT_ARGB
for data read from a.png
file. I would expecttransparentColorFlag
to betrue
, but you'd have to determine thetransparentColorIndex
empirically.See also this answer.