起因
最近在項(xiàng)目中遇到需要在界面上顯示一個(gè)本地的 GIF 圖。按照慣例我直接用了 Glide 框架來(lái)實(shí)現(xiàn)。
Glide 地址: https://github.com/bumptech/glide
我用的 Glide版本為 4.0.0-RC1 , 具體的實(shí)現(xiàn)代碼如下:
Glide.with( this ).asGif().load( R.drawable.yiba_location ).into( location_image ) ;
運(yùn)行的效果很卡頓,我懷疑是不是方法沒(méi)有用對(duì),調(diào)了壓縮模式,還是卡頓;調(diào)了緩存模式,還是卡頓。看了一下我的 gif 圖,大小還是 800K ,是不是圖片太大了,換了一張 100K 的 gif 圖,這次顯示的效果很好,gif 圖播放的很流暢。至此,得出結(jié)論:Glide 框架自身的原因,播放大尺寸的 Gif 圖的效果不是很理想。
方案
Glide 不行,那么就要另想其他方案,就去 github 上找一下。

排名第一的 android-gif-drawable 庫(kù) start 有 4.8K , 這個(gè)應(yīng)該不錯(cuò),試試吧。
android-gif-drawable : https://github.com/koral--/android-gif-drawable
引用:
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
直接把布局文件中的 ImageView 替換為 GifImageView
<pl.droidsonroids.gif.GifImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/yiba_location " />
運(yùn)行起來(lái)看看,果然很好啊,播放的很流暢,果斷采用此方案。
探尋其他的屬性:
GifImageView gifImageView = (GifImageView) findViewById(R.id.gifImageView);GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
通過(guò) GifImageView 對(duì)象獲取到 GifDrawable 對(duì)象。
gifDrawable.start(); //開(kāi)始播放gifDrawable.stop(); //停止播放gifDrawable.reset(); //復(fù)位,重新開(kāi)始播放gifDrawable.isRunning(); //是否正在播放gifDrawable.setLoopCount( 2 ); //設(shè)置播放的次數(shù),播放完了就自動(dòng)停止gifDrawable.getCurrentLoop(); //獲取正在播放的次數(shù)gifDrawable.getCurrentPosition ; //獲取現(xiàn)在到從開(kāi)始播放所經(jīng)歷的時(shí)間gifDrawable.getDuration() ; //獲取播放一次所需要的時(shí)間
總結(jié)
以上所述是小編給大家介紹的在Android 加載GIF圖最佳實(shí)踐方案,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注