简体   繁体   中英

Play GIF image in glide once

I'm trying to find how can I set a GIF image in image button and set it to play only once.

SetContentView(Resource.Layout.activity_main);
        var ib_main_home = FindViewById<ImageButton>(Resource.Id.ds);
        Glide.With(this).AsGif()
 .Load(Resource.Mipmap.giphy)
 .Into(ib_main_home);

Please try to add a RequestListener . And set the loop count in OnResourceReady() . For example:

Glide.With(this).AsGif().Load(Resource.Mipmap.giphy).Listener(new MyRequestListener()).Into(ib_main_home);

And the RequestListener :

public class MyRequestListener :Java.Lang.Object, IRequestListener
{
    public bool OnLoadFailed(GlideException p0, Java.Lang.Object p1, ITarget p2, bool p3)
    {
        return true;
    }

    public bool OnResourceReady(Java.Lang.Object p0, Java.Lang.Object p1, ITarget p2, DataSource p3, bool p4)
    {
        if (p0 is GifDrawable)
        {
            ((GifDrawable)p0).SetLoopCount(1);
        }
        return false;
    }
}

Please check the following link for more information: https://github.com/bumptech/glide/issues/1706#issuecomment-282827419

Java version to the Billy's answer to load gif into image view and control how many loops you want:

Glide.with(this)
            .asGif()
            .load(R.raw.onboarding_layers) //Your gif resource
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
            .listener(new RequestListener<GifDrawable>() {
                @Override
                public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.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);
                    return false;
                }
            })
            .into((ImageView) view.findViewById(R.id.layer_icons));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM