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í)候。