4

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?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Milk
  • 2,469
  • 5
  • 31
  • 54
  • If i understood it correctly, you mean there is no `GifDecoder` with `Glide v4` right? If yes then it's wrong information; you can still use decoder there is no change in that. [Check the release changes](https://github.com/bumptech/glide/releases) – Dipali Shah Dec 16 '17 at 05:58
  • @Dipalis. AFAIK there is still a `GifDecoder`, it's just no longer exposed via the `Glide v4 API`. Meaning I can't access the object to get the frame information. – Milk Dec 17 '17 at 18:32

2 Answers2

4

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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
0

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.

Jyubin Patel
  • 1,373
  • 7
  • 17
  • 2
    There is an implementation of the `GifDecoder` that is used internally by Glide (https://github.com/bumptech/glide/blob/master/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java). It used to be exposed in v3 but is no longer exposed in v4. Your answer doesn't explain how to access the decoder. – Milk Dec 18 '17 at 19:44