ImageView:动画gif预览 [英] ImageView: animated gif preview

查看:276
本文介绍了ImageView:动画gif预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一个显示图像预览的图库。对于大多数图像,这没有问题,只有当图像是动画gif时,图像也是动画的,在这种情况下不是必需的。
我想为动画gif做的只是显示动画的一帧。

I am putting together an image gallery that displays a preview of the image. For most of the images this is no problem, only when the image is an animated gif the image is animated as well, which in this case is not necessary. What I would like to do for an animated gif is to just display one frame of the animation.

在搜索解决方案时我遇到了这个

While searching for a solution I have come across this.

GifDecoder d = new GifDecoder();
d.read( filename);
WritableImage wimg = null;
Image img = SwingFXUtils.toFXImage(d.getFrame(0), wimg);

但首先,这似乎是过多的开销,到目前为止快速测试结束于 NullPointerException 由于 d.getFrame(0)返回 null

But first of all this seems to be too much overhead and a quick test so far ended in a NullPointerException due to the fact that d.getFrame(0) returns null.

我认为必须有一种更简单的方法,比如 Image 上的某些属性要避免动画图像,或作为不在 ImageView 中显示图像的替代方法。

I was thinking that there must be an easier way, like some property on the Image to avoid animating the image, or as an alternative approach not displaying the image in an ImageView.

推荐答案

有两个原因 GifDecoder 不起作用。

There were two reasons the GifDecoder did not work.

第一个真的是我的错误,因为传递给read方法的String应该是URI:

The first one was really my mistake, as the String passed to the read method should be an URI:

File f = new File(filename);
d.read(f.toURI().toString());

第二个问题在于 GifDecoder :要检查输入URI,它将转换为小写,然后使用转换后的String在 InputStream 中创建。这适用于不区分大小写的操作系统(如Windows),但不适用于基于NIX的系统。因此,我必须解决这个问题,现在它正在发挥作用。

The second issue lies with the GifDecoder: To check the input URI it is converted to lower case and that converted String is then used to create in InputStream. This works on an OS that is not case sensitive (like Windows) but not on NIX based systems. I therefore had to fix that and now it is working.

这篇关于ImageView:动画gif预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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