java – 将动画GIF从JAR文件加载到ImageIcon中
发布时间:2020-12-14 05:22:21 所属栏目:Java 来源:网络整理
导读:我正在尝试从存储在jar文件中的动画GIF创建一个 ImageIcon. ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif"))); 图像加载,但只有动画GIF的第一帧.动画无法播放. 如果我从文件系统上
我正在尝试从存储在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(); } } } 解决方法
这从inputStream读取gif动画
InputStream in = ...; Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(in)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |