java gifimage,java – 从BufferedImages编写动画gif
我有一个加载spritesheet的程序,在工作表上创建每个帧的BufferedImages,并将其写入动画gif.使用Elliot Kroo在Creating animated GIF with ImageIO?提供的课程,我能够成功输出文件.但是,gif没有正确的动画效果.我提供的图纸是带有透明背景的.png,因此每个后续帧都放在最后一帧的顶部,透过背景显示差异(Example).这是使用gif编写器的代码:
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();
数据是每个帧的数组作为BufferedImage(我也检查过,似乎没有问题).这是.gifs或Java ImageWriters的限制吗?或者我可以在某处编辑设置以防止这种情况发生吗?如果我不需要,我宁愿不提供背景.
解决方法:
假设data是一个BufferedImage的数组,对于从.png文件读取的数据,GifSequenceWriter构造函数的imageType参数可能是TYPE_INT_ARGB.我希望transparentColorFlag是真的,但你必须根据经验确定transparentColorIndex.
graphicsControlExtensionNode.setAttribute("transparentColorFlag", "TRUE");
graphicsControlExtensionNode.setAttribute("transparentColorIndex", ???);
标签:animated-gif,java,bufferedimage
我有一个加载spritesheet的程序,在工作表上创建每个帧的BufferedImages,并将其写入动画gif.使用Elliot Kroo在Creating animated GIF with ImageIO?提供的课程,我能够成功输出文件.但是,gif没有正确的动画效果.我提供的图纸是带有透明背景的.png,因此每个后续帧都放在最后一帧的顶部,透过背景显示差异(Example).这是使用gif编写器的代码: 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(); 数据是每个帧的数组作为BufferedImage(我也检查过,似乎没有问题).这是.gifs或Java ImageWriters的限制吗?或者我可以在某处编辑设置以防止这种情况发生吗?如果我不需要,我宁愿不提供背景. 解决方法: 假设data是一个BufferedImage的数组,对于从.png文件读取的数据,GifSequenceWriter构造函数的imageType参数可能是TYPE_INT_ARGB.我希望transparentColorFlag是真的,但你必须根据经验确定transparentColorIndex. graphicsControlExtensionNode.setAttribute("transparentColorFlag", "TRUE"); graphicsControlExtensionNode.setAttribute("transparentColorIndex", ???); 标签:animated-gif,java,bufferedimage