Domanda

Sto cercando di creare un ImageIcon da una gif animata memorizzata in un file jar.

ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));

L'immagine viene caricata, ma solo il primo fotogramma della gif animata. L'animazione non viene riprodotta.

Se carico la GIF animata da un file sul filesystem, tutto funziona come previsto. L'animazione viene riprodotta attraverso tutti i frame. Quindi funziona:

ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");

Come posso caricare una GIF animata in un ImageIcon da un file jar?

EDIT: ecco un caso di test completo, perché questo non mostra l'animazione?

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();
        }
    }
}
È stato utile?

Soluzione

Legge l'animazione gif da inputStream

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

Altri suggerimenti

Devi usare getClass (). getResource (imgName); per ottenere un URL per il file immagine. Consulta questo tutorial da Real's HowTo.

EDIT: una volta caricata l'immagine, devi imposta la proprietà ImageObserver per avviare l'esecuzione dell'animazione.

Dato che questo thread era appena stato collegato da un thread più attuale che aveva poco a che fare con le GIF animate ma è stato trascinato OT, ho pensato di aggiungere questa banale fonte che "funziona per me".

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);
    }
}

Speriamo non sia troppo tardi per questo.

Sono riuscito a ottenere la gif animata nel mio JPanel in questo modo:

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;
}

Alcuni punti di questo approccio:
1. Il file immagine si trova nel vaso;
2. ImageIO.read () restituisce un BufferedImage, che non aggiorna ImageObserver;
3. Un'altra alternativa per trovare immagini raggruppate nel file jar è chiedere al caricatore di classi Java, il codice che ha caricato il programma, di ottenere i file. Sa dove sono le cose.

In questo modo sono stato in grado di ottenere la mia gif animata all'interno del mio JPanel e ha funzionato come un fascino.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top