Couldn't find anything in the glide API to stop GIF animation after 1 or 2 loops similar to GIF animation in Google Allo to reduce processing, power consumption. Is there any method available?
Look around GifDrawable, I think you can do something like .into(new GlideImageViewTarget(iv, 2)) to limit repeats. If you use explicit targets, don't forget to add .fitCenter()/.centerCrop().
Ya thanks for that but I'm using Glide 4.0.0 Snapshot. How to implement this?
If I use DrawableImageViewTarget or ImageViewTarget there is no option of repeat integer.
Heh... that's exactly why we have the issue template ;)
It looks like the target constructor param was removed when Sam simplified the drawable hierarchy (3be40cf73c2330a414d3e9c3097b2b5ce7fb89ef). The only ways I found to do it right now is these:
.listener(new RequestListener<Drawable>() {
@Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (resource instanceof GifDrawable) {
((GifDrawable)resource).setLoopCount(2);
}
return false;
}
})
or the equivalent custom Target override if you want it more reusable (extract anon class to reuse):
.into(new DrawableImageViewTarget(imageView) {
@Override public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) {
if (resource instanceof GifDrawable) {
((GifDrawable)resource).setLoopCount(2);
}
super.onResourceReady(resource, transition);
}
});
I wrote a GifDrawableImageViewTarget to implement this requirement.
Usage:
Glide.with(getContext())
.load(R.drawable.some_gif)
.into(new GifDrawableImageViewTarget(mGifView, 1));
You can try this way.
Glide.with(getContext())
.asGif()
.load("URL_HERE") // Replace with a valid url
.addListener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1); // Place your loop count here.
return false;
}
})
.into(findViewById(R.id.your_image_view)); // Replace with your ImageView id.
Most helpful comment
I wrote a GifDrawableImageViewTarget to implement this requirement.
Usage: