我正在尝试从存储在jar文件中的动画GIF创建一个
ImageIcon.
ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));
图像加载,但只有动画GIF的第一帧.动画无法播放.
如果我从文件系统上的文件加载动画GIF,一切都按预期方式运行.动画播放所有帧.所以这样做:
ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");
如何将动画GIF从jar文件加载到ImageIcon中?
编辑:这是一个完整的测试用例,为什么不显示动画?
import javax.imageio.ImageIO;
import javax.swing.*;
public class AnimationTest extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AnimationTest test = new AnimationTest();
test.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});
}
public AnimationTest() {
super();
try {
JLabel label = new JLabel();
ImageIcon imageIcon = new ImageIcon(ImageIO.read(AnimationTest.class.getClassLoader().getResourceAsStream("animated.gif")));
label.setIcon(imageIcon);
imageIcon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这篇博客探讨了在Java中如何从JAR文件正确加载并显示动画GIF的问题。作者遇到的问题是,使用`ImageIO.read()`从JAR读取GIF时,只能显示第一帧,而无法播放动画。通过对比从文件系统加载GIF的情况,作者寻求解决方案,以确保在ImageIcon中能完整播放从JAR加载的动画GIF。
389

被折叠的 条评论
为什么被折叠?



