使用 Fresco 展示 Gif,控制播放次数,拿着就能用

  我们在开发中经常会做一些 Gif 展示,而一些开源的图片框架如 Glide、Fresco、GifImageView 也能够展示 Gif,GifImageView 可以直接设置播放次数,而 Fresco 好像并没有设置播放次数的,就需要我们自己来处理。

  首先,要使用 Fresco 就得先依赖,注意两个版本得一致:

implementation 'com.facebook.fresco:fresco:1.5.0'
implementation 'com.facebook.fresco:animated-gif:1.5.0'

那我们一般来说使用 Fresco 展示 Gif 是怎么操作呢?

DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                // 这个是设置自动播放动画,在源码中就是当图片加载完毕后会调用 anmi.start();
                .setAutoPlayAnimations(true)
                .build();
        simpleDraweeView.setController(controller);

  那我们如果要给 Gif 设置播放次数呢?好像 Fresco 并没有提供类似的 API,那么就需要我们自己去处理了,这里笔者封装好了一个,可以直接复制过去使用:

public class GifController {

    private SimpleDraweeView view;
    private Uri uri;
    private GifListener listener;
    private int loopCount = -1;
    private int currentCount = 0;
    private boolean visibleShowAgain = false;
    private boolean hasShow = false;

    public abstract static class GifListener{
        public abstract void onStart(Animatable anim);
        public abstract void onStop(Animatable anim);
        public abstract void onRepeat(Animatable anim,int currentCount);
    }


    public GifController setView(SimpleDraweeView view){
        this.view = view;
        return this;
    }

    public GifController setGif(int resId){
        Uri uri = new Uri.Builder()
                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                .path(String.valueOf(resId))
                .build();
        this.uri = uri;
        return this;
    }

    public GifController setGif(String url){
        this.uri = Uri.parse(url);
        return this;
    }

    public GifController setListener(GifListener listener){
        this.listener = listener;
        return this;
    }

    public GifController setLoopCount(int loopCount){
        this.loopCount = loopCount;
        return this;
    }

    public GifController setVisibleShowAgain(boolean visibleShowAgain){
        this.visibleShowAgain = visibleShowAgain;
        return this;
    }

    public void showGif(Context context){
        if (view == null || uri == null || context == null){
            return;
        }

        if (!Fresco.hasBeenInitialized()){
            Fresco.initialize(context);
        }

        final DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setControllerListener(new BaseControllerListener<Object>() {
                    @Override
                    public void onFinalImageSet(String id, @Nullable Object info, @Nullable final Animatable anim) {
                        if (anim != null) {
                            try {
                                AnimatedDrawable2 drawable = (AnimatedDrawable2) anim;
                                drawable.setAnimationListener(new BaseAnimationListener() {
                                    @Override
                                    public void onAnimationStart(AnimatedDrawable2 drawable) {
                                        if (listener != null){
                                            listener.onStart(anim);
                                        }
                                    }

                                    @Override
                                    public void onAnimationStop(AnimatedDrawable2 drawable) {
                                        currentCount = 0;
                                        if (listener != null){
                                            listener.onStop(anim);
                                        }
                                    }

                                    @Override
                                    public void onAnimationRepeat(AnimatedDrawable2 drawable) {
                                        currentCount++;
                                        if (listener != null){
                                            listener.onRepeat(anim,currentCount);
                                        }
                                        if (currentCount >= loopCount){
                                            anim.stop();
                                        }
                                    }

                                });
                            }catch (Throwable e){
                                LogUtil.logE(e.getMessage());
                            }
                            if (!hasShow || visibleShowAgain){
                                anim.start();
                            }
                            hasShow = true;
                        }

                    }
                })
                .build();
        view.setController(controller);

    }

}

怎么使用呢?我们直接在 Activity 中调用:

new GifController()
                        // 设置 Gif 资源,也可以传入 url
                        .setGif(R.drawable.gif_xxx)
                        // 设置 Gif 播放回调,包括开始、结束、第几次重复完成
                        .setListener(new GifController.GifListener() {
                            @Override
                            public void onStart(Animatable anim) {
                                System.out.println("Gif Start");
                            }

                            @Override
                            public void onStop(Animatable anim) {
                                System.out.println("Gif stop");
                            }

                            @Override
                            public void onRepeat(Animatable anim,int currentCount) {
                                System.out.println("Gif Repeat"+currentCount);
                            }

                        })
                        // 设置 SimpleDraweeView
                        .setView(view)
                        // 设置重复次数
                        .setLoopCount(1)
                        // 设置 Gif 在屏幕上再次可见是否再次播放,默认 false
                        .setVisibleShowAgain(true)
                        // 展示
                        .showGif(this);

  好了,完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android官方的ImageView控件并不支持直接加载GIF动画,但是可以通过使用第三方库来实现在XML布局中显示GIF动画的效果。 目前比较常用的第三方库有: 1. Glide:Glide是一个功能强大、灵活且易于使用的图片加载库,支持GIF动画的加载和显示。 2. FrescoFresco是Facebook开源的一个功能强大、灵活且易于使用的图片加载库,支持GIF动画的加载和显示。 下面以Glide为例,演示如何在XML布局中显示GIF动画: 1.在build.gradle文件中添加依赖: ``` dependencies { implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' } ``` 2.在XML布局文件中使用如下代码: ``` <com.bumptech.glide.load.resource.gif.GifDrawableImageView android:id="@+id/iv_gif" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_gif"/> ``` 其中,com.bumptech.glide.load.resource.gif.GifDrawableImageView是Glide库自带的一个支持GIF动画的ImageView控件。 3.在Java代码中使用如下代码: ``` ImageView imageView = findViewById(R.id.iv_gif); Glide.with(this).asGif().load(R.drawable.my_gif).into(imageView); ``` 其中,asGif()方法表示要加载的是GIF动画,R.drawable.my_gif是要加载的GIF动画资源ID。 需要注意的是,使用Glide加载GIF动画时,需要添加asGif()方法来告诉Glide要加载的是GIF动画,否则默认情况下会当作静态图片处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值