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