JLabel如何有效地显示GIF动画?

时间:2017-06-28 09:08:58

标签: java swing jlabel gif

以下代码让我展示10个大型gif图像,其CPU性能大约为5%

ImageIcon icon = new ImageIcon("target.gif");
JLabel jp = new JLabel(image);

如果我使用DhyanB(https://github.com/DhyanB/Open-Imaging)的开放式成像库手动实现类似功能,则会导致性能下降一些。 我查看了JLabel,ImageIcon和JComponent源代码,但找不到负责显示.gif文件的部分。 我想弄清楚如何在绘制gif动画之前做一些有效的图像处理。

GifImage gifImage = GifDecoder.read(new FileInputStream(f));
final int frameCount = gifImage.getFrameCount();

//Very basic gif playback
do{
    long lastUpdate = 0;

    for(int i = 0; i < frameCount; i++){
        BufferedImage frame = gifImage.getFrame(i);
        final int delay = (gifImage.getDelay(i))*10;

        //TODO Alpha masking and rescaling

        final long timePassedSinceLastFrame = (System.currentTimeMillis() - lastUpdate); //Change to nanao if necessary

        final long sleep =  delay - timePassedSinceLastFrame;

        if(sleep > 0)
            Thread.sleep(sleep);

        //Display

        targetImage = frame;
        self.repaint();
        lastUpdate = System.currentTimeMillis();
    }
}while(loop);


@Override
public void paintComponent(Graphics g){
    if(targetImage != null){
        g.drawImage(targetImage, 0, 0, null);
    }
}

0 个答案:

没有答案