Glide在4.0版本以上设置Gif播放次数

Glide在4.0以前的版本,可以直接通过GlideDrawableImageViewTarget()来控制Gif的播放次数。
Glide.with(mMainActivity).load(R.drawable.gif_drawable).into(new GlideDrawableImageViewTarget(imageview,2));

在4.0版本后,会发现GlideDrawableImageViewTarget()这个类没有了,然后我们去看GifDrawable这个类;


可以发现是有start(),stop(),setLoopCount()等方法的,我们要做的就是调用GIFDrawable所提供的方法就可以。

我们再来看原来的into(new GlideDrawableImageViewTarget()),他传进去的就是一个Target对象,我们自定义一个Target,在这里设置GIFDrawable的属性即可。

最终代码如下:

Glide.with(mMainActivity).load(R.drawable.gif_drawable).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                        if (drawable instanceof GifDrawable) {
                            GifDrawable gifDrawable = (GifDrawable) drawable;
                            gifDrawable.setLoopCount(2);
                            imageView.setImageDrawable(drawable);
                            gifDrawable.start();
                        }
                    }
                });
当然也可以通过设置listener等多种方法来做,这里就不给出代码了。

猜你喜欢

转载自blog.csdn.net/h15620535082/article/details/80867965