简体   繁体   中英

Glide 4: get GifDecoder of GifDrawable

I'm looking to upgrade my app from Glide v3 to Glide v4. I need to know how long loop is of a gif that is loaded through Glide.

v3 Code:

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. 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:

 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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