簡體   English   中英

將JAR文件中的動畫gif加載到ImageIcon中

[英]Loading animated gif from JAR file into ImageIcon

我正在嘗試從存儲在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");

如何從jar文件中將動畫gif加載到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));

你必須使用getClass()。getResource(imgName); 獲取圖像文件的URL。 從Real的HowTo查看本教程

編輯:加載圖像后,您必須設置ImageObserver屬性以使動畫運行。

由於這個線程只是從一個與動畫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);
    }
}

希望現在還不算太晚。

我設法通過這種方式在我的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,它就像一個魅力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM