Glide 4:获取GifDrawable的GifDecoder

4

我能帮助您将应用程序从 Glide v3 升级到 Glide v4。我需要知道通过 Glide 加载的 gif 循环时间长度。

v3 代码:

int duration = 0;
GifDecoder decoder = gifDrawable.getDecoder();
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
    duration += decoder.getDelay(i);
}

看起来在Glide v4中不再使用GifDecoder,那么我该如何在没有它的情况下进行计算,或者现在该如何获取解码器呢?


如果我理解正确的话,你的意思是Glide v4中没有GifDecoder,对吗?如果是的话,那就是错误的信息;你仍然可以使用解码器,没有任何改变。请查看发布更改 - Dipali Shah
据我所知,仍然存在一个“GifDecoder”,只是不再通过“Glide v4 API”公开。这意味着我无法访问该对象以获取帧信息。 - Milk
2个回答

4
这是使用反射获取 GifDecoder 的一种不好的方法:
 Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
 Field frameLoaderField = gifStateClass.getDeclaredField("frameLoader");
 frameLoaderField.setAccessible(true);
 Object frameLoader = frameLoaderField.get(gifDrawable.getConstantState());

 Class frameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
 Field gifDecoderField = frameLoaderClass.getDeclaredField("gifDecoder");
 gifDecoderField.setAccessible(true);
 GifDecoder gifDecoder = (GifDecoder) gifDecoderField.get(frameLoader);

 int duration = 0;
 for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
     duration += gifDecoder.getDelay(i);
 }

这不应被视为一个稳定/可靠的解决方案,因为API可能会发生变化。尽管如此,为了快速解决问题,这肯定会起作用。
我看到一个适当的问题已打开,一旦有任何变化就会更新答案。

那很糟糕...但这是唯一有效的答案,所以您赢得了奖励 :) - Milk

0

2
Glide内部使用了GifDecoder的实现(https://github.com/bumptech/glide/blob/master/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java)。它在v3中曾经被公开,但在v4中不再公开。你的答案并没有解释如何访问解码器。 - Milk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接