Fresco設置GIF只播放一次,播放完后顯示其他view

Fresco對gif可以設置自動播放,但是播放次數(shù)沒有設置播放的方法,只有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中設置的循環(huán)次數(shù)來獲取播放次數(shù)
而官網上面對于gif播放只給出了簡單的使用方法
手動控制動畫圖播放,監(jiān)聽圖片是否加載完畢,然后才能控制動畫的播放:

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中播放次數(shù)設置為一次,但是播放后如何替換呢,我百度后看到https://www.imooc.com/article/15274
這個文章很久了,使用的是fresco很早的版本,可以獲取循環(huán)次數(shù)和播放時間,我找了下,沒有AbstractAnimatedDrawable,也沒有AnimatedDrawable了,只有AnimatedDrawable2,里面也沒有次數(shù)設置了,但是我看了下代碼有個

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

這個AnimationListener可以實現(xiàn)了對動畫的監(jiān)聽
于是監(jiān)聽動畫播放結束時,替換相應的圖片,就實現(xiàn)了

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的循環(huán)播放次數(shù)只有一次,可以在onAnimationStop進行操作,如果是循環(huán),那可以在onAnimationRepeat進行設置,repeat是幀數(shù)為0,播放次數(shù)加1的時候。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,141評論 4 61
  • 就是覺得很煩吧 無語倫比的煩
    左半邊翅膀91閱讀 163評論 0 0
  • 它用燦爛的金色裝點世界 同一切偉大的顏色一樣 褐色泥土養(yǎng)育著它 沒有形狀的風吹成愛情的模樣 一粒種子在花蕊里試探溫...
    金水L閱讀 118評論 0 0
  • 看了那么多家居美圖,依然裝不好自己的家,因為你在乎的只是它的美,卻忘記了自己房屋的特點。給大家介紹15個牛逼的家居...
    安廈閱讀 379評論 0 2
  • 竊憐顓帝孫,抱石負君恩。 胡棄此軀老,不教湘水渾? 余既閱《漁父》,雜然有所感,非獨哀斯時靈均之不遇,乃復嘆古今功...
    姑射閱讀 374評論 0 5

友情鏈接更多精彩內容