美文网首页
Fresco设置GIF只播放一次,播放完后显示其他view

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

作者: Sunny君907 | 来源:发表于2019-02-19 15:16 被阅读0次

    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的时候。

    相关文章

      网友评论

          本文标题:Fresco设置GIF只播放一次,播放完后显示其他view

          本文链接:https://www.haomeiwen.com/subject/sjtfyqtx.html