Replay a non-looping gif image

2019-02-18 21:31发布

I've got a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.

My problem is that when I set the second JLabel's icon to be the gif, the animation has already been played, so it only show the last frame of it.

Do you know a way to get the animation when the gif is set to the second JLabel?

2条回答
混吃等死
2楼-- · 2019-02-18 21:46

On the newly created icon try using:

icon.getImage().flush();
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-18 22:00

Ok,

discarded old answer. After some more searching around I found the way to do it.

ImageIcon icon = ..[the animated gif without looping]..
....
label1.setIcon(icon); //animation plays once
....
// now time to remove icon from label1 and add it to label2
label1.setIcon(null);
icon.getImage().flush(); //reset resource used by the image
label2.setIcon(icon);
....

Java Api: Image#flush()

btw. if you leave the line label1.setIcon(null); out it will still repeat the animation of the icon only for label2. label1 stays at the last frame.

查看更多
登录 后发表回答