Fresco設(shè)置GIF只播放一次趟佃,播放完后顯示其他view

Fresco對(duì)gif可以設(shè)置自動(dòng)播放,但是播放次數(shù)沒有設(shè)置播放的方法锣尉,只有g(shù)et方法,在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;
    }
  }

這個(gè)通過獲取gif中設(shè)置的循環(huán)次數(shù)來獲取播放次數(shù)
而官網(wǎng)上面對(duì)于gif播放只給出了簡(jiǎn)單的使用方法
手動(dòng)控制動(dòng)畫圖播放决采,監(jiān)聽圖片是否加載完畢自沧,然后才能控制動(dòng)畫的播放:

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)
    // 其他設(shè)置(如果有的話)
    .build();
mSimpleDraweeView.setController(controller);

自動(dòng)播放設(shè)置,圖片下載完之后自動(dòng)播放树瞭,同時(shí)拇厢,當(dāng)View從屏幕移除時(shí),停止播放晒喷,只需要在 image request 中簡(jiǎn)單設(shè)置

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

gif只播放一次可以將gif中播放次數(shù)設(shè)置為一次孝偎,但是播放后如何替換呢,我百度后看到https://www.imooc.com/article/15274
這個(gè)文章很久了凉敲,使用的是fresco很早的版本衣盾,可以獲取循環(huán)次數(shù)和播放時(shí)間,我找了下爷抓,沒有AbstractAnimatedDrawable势决,也沒有AnimatedDrawable了,只有AnimatedDrawable2蓝撇,里面也沒有次數(shù)設(shè)置了果复,但是我看了下代碼有個(gè)

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

這個(gè)AnimationListener可以實(shí)現(xiàn)了對(duì)動(dòng)畫的監(jiān)聽
于是監(jiān)聽動(dòng)畫播放結(jié)束時(shí),替換相應(yīng)的圖片渤昌,就實(shí)現(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()
}

這個(gè)listener簡(jiǎn)直打開了gif功能的另一個(gè)天地

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進(jìn)行操作传透,如果是循環(huán),那可以在onAnimationRepeat進(jìn)行設(shè)置极颓,repeat是幀數(shù)為0朱盐,播放次數(shù)加1的時(shí)候。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末菠隆,一起剝皮案震驚了整個(gè)濱河市兵琳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌骇径,老刑警劉巖躯肌,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異破衔,居然都是意外死亡清女,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門晰筛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嫡丙,“玉大人,你說我怎么就攤上這事读第∈锊” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵怜瞒,是天一觀的道長(zhǎng)父泳。 經(jīng)常有香客問我,道長(zhǎng)吴汪,這世上最難降的妖魔是什么惠窄? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮漾橙,結(jié)果婚禮上睬捶,老公的妹妹穿的比我還像新娘。我一直安慰自己近刘,他們只是感情好擒贸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著觉渴,像睡著了一般介劫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上案淋,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天座韵,我揣著相機(jī)與錄音,去河邊找鬼。 笑死誉碴,一個(gè)胖子當(dāng)著我的面吹牛宦棺,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播黔帕,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼代咸,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了成黄?” 一聲冷哼從身側(cè)響起呐芥,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奋岁,沒想到半個(gè)月后思瘟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡闻伶,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年滨攻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓝翰。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡光绕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出霎箍,到底是詐尸還是另有隱情,我是刑警寧澤澡为,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布漂坏,位于F島的核電站,受9級(jí)特大地震影響媒至,放射性物質(zhì)發(fā)生泄漏顶别。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一拒啰、第九天 我趴在偏房一處隱蔽的房頂上張望驯绎。 院中可真熱鬧,春花似錦谋旦、人聲如沸剩失。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拴孤。三九已至,卻和暖如春甲捏,著一層夾襖步出監(jiān)牢的瞬間演熟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留芒粹,地道東北人兄纺。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像化漆,于是被迫代替她去往敵國(guó)和親估脆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫获三、插件旁蔼、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,107評(píng)論 4 62
  • 就是覺得很煩吧 無語倫比的煩
    左半邊翅膀91閱讀 128評(píng)論 0 0
  • 它用燦爛的金色裝點(diǎn)世界 同一切偉大的顏色一樣 褐色泥土養(yǎng)育著它 沒有形狀的風(fēng)吹成愛情的模樣 一粒種子在花蕊里試探溫...
    金水L閱讀 106評(píng)論 0 0
  • 看了那么多家居美圖,依然裝不好自己的家疙教,因?yàn)槟阍诤醯闹皇撬拿拦琢模瑓s忘記了自己房屋的特點(diǎn)。給大家介紹15個(gè)牛逼的家居...
    安廈閱讀 327評(píng)論 0 2
  • 竊憐顓帝孫贞谓,抱石負(fù)君恩限佩。 胡棄此軀老,不教湘水渾裸弦? 余既閱《漁父》祟同,雜然有所感,非獨(dú)哀斯時(shí)靈均之不遇理疙,乃復(fù)嘆古今功...
    姑射閱讀 339評(píng)論 0 5