简体   繁体   English

Glide 4:获取GifDrawable的GifDecoder

[英]Glide 4: get GifDecoder of GifDrawable

I'm looking to upgrade my app from Glide v3 to Glide v4. 我正在寻求将我的应用程序从Glide v3升级到Glide v4。 I need to know how long loop is of a gif that is loaded through Glide. 我需要知道通过Glide加载的gif循环有多长。

v3 Code: v3代码:

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

It looks like the GifDecoder is no longer exposed with Glide v4. 看起来GifDecoder不再受Glide v4的影响。 How do I go about calculating this without it, or how do I obtain the decoder now? 如何在没有它的情况下计算这个,或者我现在如何获得解码器?

Here's a nasty way to acquire GifDecoder using reflection: 这是使用反射获取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);
 }

This should not be considered as a stable/reliable solution as long as the API might change. 只要API可能发生变化, 就不应将其视为稳定/可靠的解决方案。 Nevertheless, to quick solve the issue this will certainly work. 然而,要快速解决问题,这肯定会奏效。

I can see an appropriate issue opened , will update the answer as soon as something changes. 我可以看到打开了一个合适的问题 ,一旦有变化就会更新答案。

Please check below link hope this helps: 请检查以下链接希望这有助于:

https://github.com/bumptech/glide/blob/master/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java https://github.com/bumptech/glide/blob/master/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java

Please add below dependency: 请添加以下依赖项:

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.4.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'
}

In above Gradle file decoder file is there and do your operations. 在上面的Gradle文件中,解码器文件就在那里并进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM