Fresco设置GIF只播放一次,播放完后显示其他view

Fresco对gif可以设置自动播放,但是播放次数没有设置播放的方法,只有get方法,在GifImage

 public int getLoopCount() {
    // If a GIF image has no Netscape 2.0 loop extension, it is meant to play once and then stop. A
    // loop count of 0 indicates an endless looping of the animation. Any loop count X>0 indicates
    // that the animation shall be repeated X times, resulting in the animation to play X+1 times.
    final int loopCount = nativeGetLoopCount();
    switch (loopCount) {
      case LOOP_COUNT_FOREVER:
        return AnimatedImage.LOOP_COUNT_INFINITE;
      case LOOP_COUNT_MISSING:
        return 1;
      default:
        return loopCount + 1;
    }
  }

这个通过获取gif中设置的循环次数来获取播放次数
而官网上面对于gif播放只给出了简单的使用方法
手动控制动画图播放,监听图片是否加载完毕,然后才能控制动画的播放:

ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
    @Override
    public void onFinalImageSet(
        String id,
        @Nullable ImageInfo imageInfo,
        @Nullable Animatable anim) {
        if (anim != null) {
          // 其他控制逻辑
          anim.start();
        }
    }
};

Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setControllerListener(controllerListener)
    // 其他设置(如果有的话)
    .build();
mSimpleDraweeView.setController(controller);

自动播放设置,图片下载完之后自动播放,同时,当View从屏幕移除时,停止播放,只需要在 image request 中简单设置

Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    . // 其他设置(如果有的话)
    .build();
mSimpleDraweeView.setController(controller);

gif只播放一次可以将gif中播放次数设置为一次,但是播放后如何替换呢,我百度后看到https://www.imooc.com/article/15274
这个文章很久了,使用的是fresco很早的版本,可以获取循环次数和播放时间,我找了下,没有AbstractAnimatedDrawable,也没有AnimatedDrawable了,只有AnimatedDrawable2,里面也没有次数设置了,但是我看了下代码有个

private static final AnimationListener NO_OP_LISTENER = new BaseAnimationListener();
public void setAnimationListener(@Nullable AnimationListener animationListener) {
    mAnimationListener = animationListener != null
        ? animationListener
        : NO_OP_LISTENER;
  }

这个AnimationListener可以实现了对动画的监听
于是监听动画播放结束时,替换相应的图片,就实现了

fun SimpleDraweeView?.loadGifOnce(aniImageUrl: String?, staticImageUrl: String?) {
    if (this == null) return
    val uri = if (aniImageUrl.isNullOrEmpty()) null else Uri.parse(aniImageUrl)
    controller = Fresco.newDraweeControllerBuilder()
        .setUri(uri)
        .setOldController(controller)
        .setAutoPlayAnimations(false)
        .setControllerListener(object : BaseControllerListener<ImageInfo>(){
            override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
                if (animatable != null && !animatable.isRunning){
                    animatable.start()
                    val animatedDrawable2 = animatable as AnimatedDrawable2
                    animatedDrawable2.setAnimationListener(object : AnimationListener {
                        override fun onAnimationRepeat(drawable: AnimatedDrawable2?) {
                        }

                        override fun onAnimationStart(drawable: AnimatedDrawable2?) {
                        }

                        override fun onAnimationFrame(drawable: AnimatedDrawable2?, frameNumber: Int) {
                        }

                        override fun onAnimationStop(drawable: AnimatedDrawable2?) {
                            setImageURI(staticImageUrl)
                        }

                        override fun onAnimationReset(drawable: AnimatedDrawable2?) {
                        }

                    })
                }
            }
        })
        .build()
}

这个listener简直打开了gif功能的另一个天地

public interface AnimationListener {

  /**
   * Called when the animation is started for the given drawable.
   *
   * @param drawable the affected drawable
   */
  void onAnimationStart(AnimatedDrawable2 drawable);

  /**
   * Called when the animation is stopped for the given drawable.
   *
   * @param drawable the affected drawable
   */
  void onAnimationStop(AnimatedDrawable2 drawable);

  /**
   * Called when the animation is reset for the given drawable.
   *
   * @param drawable the affected drawable
   */
  void onAnimationReset(AnimatedDrawable2 drawable);

  /**
   * Called when the animation is repeated for the given drawable.
   * Animations have a loop count, and frame count, so this is called when
   * the frame count is 0 and the loop count is increased.
   *
   * @param drawable the affected drawable
   */
  void onAnimationRepeat(AnimatedDrawable2 drawable);

  /**
   * Called when a frame of the animation is about to be rendered.
   *
   * @param drawable the affected drawable
   * @param frameNumber the frame number to be rendered
   */
  void onAnimationFrame(AnimatedDrawable2 drawable, int frameNumber);
}

若gif的循环播放次数只有一次,可以在onAnimationStop进行操作,如果是循环,那可以在onAnimationRepeat进行设置,repeat是帧数为0,播放次数加1的时候。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,440评论 4 61
  • 就是觉得很烦吧 无语伦比的烦
    左半边翅膀91阅读 157评论 0 0
  • 它用灿烂的金色装点世界 同一切伟大的颜色一样 褐色泥土养育着它 没有形状的风吹成爱情的模样 一粒种子在花蕊里试探温...
    金水L阅读 118评论 0 0
  • 看了那么多家居美图,依然装不好自己的家,因为你在乎的只是它的美,却忘记了自己房屋的特点。给大家介绍15个牛逼的家居...
    安厦阅读 365评论 0 2
  • 窃怜颛帝孙,抱石负君恩。 胡弃此躯老,不教湘水浑? 余既阅《渔父》,杂然有所感,非独哀斯时灵均之不遇,乃复叹古今功...
    姑射阅读 368评论 0 5

友情链接更多精彩内容