Glide: Stop GIF animation after 1 or 2 loops

Created on 28 Jan 2017  路  5Comments  路  Source: bumptech/glide

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?

question v4

Most helpful comment

I wrote a GifDrawableImageViewTarget to implement this requirement.

Usage:

Glide.with(getContext())
        .load(R.drawable.some_gif)
        .into(new GifDrawableImageViewTarget(mGifView, 1));

All 5 comments

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.
Was this page helpful?
0 / 5 - 0 ratings