知道何时播放GIF [英] Knowing when GIF is done playing

查看:86
本文介绍了知道何时播放GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向JLabel显示GIF文件。没问题,我加载了GIF并将其设置为标签的图标。但是现在我真的需要知道何时结束,因为它必须具有过场动画的功能。我已经尝试过的是这个(GameScene是一个自定义对象,但是我知道setIcon可以工作,相信我):

I'm trying to display a GIF file to a JLabel. No problem I load the GIF and set it as the icon of the label. But now i really need to know when this ends, because it has to function as a cutscene. What i already tried is this (GameScene is a custom object but i know setIcon works, trust me) :

private GameScene scene;
private ImageIcon _temp;
private ImageIcon toplay;
private Thread anim;

public Cutscene(ImageIcon gif, GameScene scene, int length){
    this._temp = scene.getIcon();
    this.toplay = gif;
    this.scene = scene;
    anim = new Thread(){
        @Override
        public void run(){
            scene.setIcon(gif);
            scene.repaint();
            try {
                Thread.sleep(length);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
}

private void _s(){
    scene.setSolidColor(false);
    scene.setIcon(toplay);
    scene.repaint();
}

public void play(){
    _s();
    anim.start();
}

public void await(){
    try {
        anim.join();
        scene.setIcon(_temp);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

现在,有没有我可以实现的目标,只是让整个程序在给定的时间内休眠,然后继续其余的代码,而完全不绘制GIF文件?

Now, Is there anyway I can achieve this without, just letting the whole program sleep for the given amount of time and then continue with the rest of the code and completely not drawing the GIF file?

推荐答案

这是一个非常简单的示例,它仅适用于非循环gif,如果您的gif重复出现,则您必须测试两次请求 FRAMEBITS 的时间并尝试确定是否可以检测到循环点。

This is a really simply example, it will only work with non-looping gifs, if your gif repeats you'll have to test the time between requests for FRAMEBITS and try and determine if you can detect the point at which loops.

public class MonitoringLabel extends JLabel {

    public MonitoringLabel(Icon image) {
        super(image);
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        listenerList.remove(ActionListener.class, listener);
    }

    protected void fireActionPerformed() {
        ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
        if (listeners.length > 0) {
            ActionEvent evt = new ActionEvent(this, 0, "stopped");
            for (ActionListener listener : listeners) {
                listener.actionPerformed(evt);
            }
        }
    }

    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        boolean finished = super.imageUpdate(img, infoflags, x, y, w, h);
        if (!finished) {
            fireActionPerformed();
        }
        return finished;
    }

}

以上类提供了 ActionListener ,当图像完成加载时触发(当它们不是要播放的动画中的新帧时触发-重复gif从不返回 false

The above class provides an ActionListener which is triggered when the image "finishes" loading (this is triggered when their are no new frames in the animation to be played - repeating gifs never return false)

如果需要,可以使用其他侦听器界面,我只是将其作为示例。

You can use a different listener interface if you want, I just threw it together as an example.

我选择覆盖 imageUpdate 的原因是因为 JLabel 已经充当了 ImageObserver 绘制图像时的这种方式,这样我们就可以使用 JLabel 的功能,而无需手动进行操作画一个gif ...是的,我已经完成了,很痛苦

The reason I choose to override imageUpdate is because the JLabel is already acting as the ImageObserver to the image when it paints it, this way we can use the power of the JLabel without fussing about with manually painting a gif ... yes I've done it, it's pain

这篇关于知道何时播放GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆