Reputation: 2655
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?
Upvotes: 4
Views: 1745
Reputation: 62209
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.
Upvotes: 4
Reputation: 1373
Please check below link hope this helps:
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.
Upvotes: 0