【问题标题】:Loading animated gif from JAR file into ImageIcon将 JAR 文件中的动画 gif 加载到 ImageIcon
【发布时间】:2008-09-29 15:23:12
【问题描述】:

我正在尝试从存储在 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 swing animated-gif javax.imageio


    【解决方案1】:

    这会从 inputStream 中读取 gif 动画

    InputStream in = ...;
    Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(in));
    

    【讨论】:

    • 这是确定的答案。在研究这个问题几个小时后感谢您。这也解决了在 JAR 中从 Eclipse 和 Maven 加载资源的问题。非常感谢!
    【解决方案2】:

    你必须使用 getClass().getResource(imgName);获取图像文件的 URL。查看 Real's HowTo 中的 this tutorial

    编辑:加载图像后,您必须set the ImageObserver property 才能运行动画。

    【讨论】:

    • 我使用第一部分代码从 jar 文件中加载 png 图像。并且 gif 加载 - 它只是没有动画。
    • 设置 ImageObserver 对我没有帮助。似乎 ImageIO 没有正确读取动画 gif。如果我为 ImageIcon 使用不同的构造函数,它可以工作。我用一个完整的代码示例更新了这个问题。你的类路径中需要一个 animated.gif。
    【解决方案3】:

    由于这个线程只是从一个更新的线程链接到的,这个线程与动画 GIF 几乎没有关系,但被拖到了 OT,我想我应该添加这个“对我有用”的琐碎资源。

    import javax.swing.*;
    import java.net.URL;
    
    class AnimatedGifInLabel {
    
        public static void main(String[] args) throws Exception {
            final URL url = new URL("http://i.stack.imgur.com/OtTIY.gif");
            Runnable r = new Runnable() {
                public void run() {
                    ImageIcon ii = new ImageIcon(url);
                    JLabel label = new JLabel(ii);
                    JOptionPane.showMessageDialog(null, label);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    【讨论】:

    • 如果有人想知道如何从文件中加载图像,方法如下: url = new URL("file", "localhost", "resources/image.gif");
    【解决方案4】:

    希望现在还为时不晚。

    我设法通过这种方式在我的 JPanel 中获取了动画 gif:

    private JPanel loadingPanel() {
        JPanel panel = new JPanel();
        BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
        panel.setLayout(layoutMgr);
    
        ClassLoader cldr = this.getClass().getClassLoader();
        java.net.URL imageURL   = cldr.getResource("img/spinner.gif");
        ImageIcon imageIcon = new ImageIcon(imageURL);
        JLabel iconLabel = new JLabel();
        iconLabel.setIcon(imageIcon);
        imageIcon.setImageObserver(iconLabel);
    
        JLabel label = new JLabel("Loading...");
        panel.add(iconLabel);
        panel.add(label);
        return panel;
    }
    

    这种方法的一些要点:
    1.图像文件在jar内;
    2、ImageIO.read()返回一个BufferedImage,不更新ImageObserver;
    3. 另一种查找捆绑在 jar 文件中的图像的方法是询问 Java 类加载器(加载程序的代码)来获取文件。它知道东西在哪里。

    因此,通过这样做,我能够在我的 JPanel 中获得我的动画 gif,它就像一个魅力。

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 2012-02-10
      • 1970-01-01
      • 2019-10-16
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多