Glide版本3.7 目前不清楚4及以上版本是否有同樣的問題
今天使用glide播放gif發(fā)現(xiàn)播放的速度很慢览濒,在ios或者其他地方都沒有這種問題缝驳。
跟蹤源碼查找發(fā)現(xiàn)涮俄,與播放速度相關(guān)的是每一幀的延時時間:
private void loadNextFrame() {
if (!isRunning || isLoadPending) {
return;
}
isLoadPending = true;
gifDecoder.advance();
long targetTime = SystemClock.uptimeMillis() + gifDecoder.getNextDelay();//獲取一幀的延時時間
DelayTarget next = new DelayTarget(handler, gifDecoder.getCurrentFrameIndex(), targetTime);
requestBuilder
.signature(new FrameSignature())
.into(next);
}
public int getDelay(int n) {
int delay = -1;
if ((n >= 0) && (n < header.frameCount)) {
delay = header.frames.get(n).delay;//從解析出的gifheader中獲取此幀延時時間
}
return delay;
}
而在解析gif的過程中:
int delayInHundredthsOfASecond = readShort();
// TODO: consider allowing -1 to indicate show forever.
if (delayInHundredthsOfASecond < MIN_FRAME_DELAY) { //這個值為3
delayInHundredthsOfASecond = DEFAULT_FRAME_DELAY;//這個值為10
}
header.currentFrame.delay = delayInHundredthsOfASecond * 10;
當解析每幀數(shù)據(jù)時通砍,如果此幀延時<30毫秒卑硫,則會被賦值為100毫秒球昨。debug發(fā)現(xiàn)ui小哥哥給我的gif正好是20毫秒每幀延遲尔店,所以播放很慢。這就尷尬了主慰。
因為不太想下載源碼修改嚣州,這里偷懶直接用了反射去修改。
Glide.with(this)
.load(lotteryBean.getBanner_pic())
.asGif()
.into(new SimpleTarget<GifDrawable>(){
@Override
public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) {
try {
Field field = GifDecoder.class.getDeclaredField("header");
field.setAccessible(true);
GifHeader header = (GifHeader) field.get(resource.getDecoder());
Field field2 = GifHeader.class.getDeclaredField("frames");
field2.setAccessible(true);
List frames = (List) field2.get(header);
if (frames.size()>0){
Field delay = frames.get(0).getClass().getDeclaredField("delay");
delay.setAccessible(true);
for (Object frame : frames) {
delay.set(frame,20);//這里直接給修改成了20
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
mBannerImage.setImageDrawable(resource);
resource.setLoopCount(Integer.MAX_VALUE);
resource.start();
}
});
不過這種方式不太靈活共螺,只能算是應(yīng)急吧该肴。嘗試了反射替換decoder相關(guān)類,感覺比較麻煩藐不,Glide的很多類都無法在包外調(diào)用匀哄,暫時不了了之TT。
歡迎有好的方案的老哥留言~