Glide 4: Get GifDecoder GifDrawable

I want to upgrade my application from Glide v3 to Glide v4. I need to know how long the loop has gif, loaded via 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 it GifDecoderno longer displays with Glide v4. How do I start calculating this without it, or how do I get a decoder now?

+4
source share
2 answers

Here's an unpleasant way to get GifDecoderwith 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 a stable / reliable solution, as long as the API can change. However, to quickly solve the problem, this will certainly work.

, , , - .

+3

, , , :

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

:

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

Gradle .

0

Source: https://habr.com/ru/post/1690206/


All Articles