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

排名第一的 android-gif-drawable 庫 start 有 4.8K , 這個應該不錯,試試吧。
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 " />
運行起來看看,果然很好啊,播放的很流暢,果斷采用此方案。
探尋其他的屬性:
GifImageView gifImageView = (GifImageView) findViewById(R.id.gifImageView);GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
通過 GifImageView 對象獲取到 GifDrawable 對象。
gifDrawable.start(); //開始播放gifDrawable.stop(); //停止播放gifDrawable.reset(); //復位,重新開始播放gifDrawable.isRunning(); //是否正在播放gifDrawable.setLoopCount( 2 ); //設置播放的次數,播放完了就自動停止gifDrawable.getCurrentLoop(); //獲取正在播放的次數gifDrawable.getCurrentPosition ; //獲取現在到從開始播放所經歷的時間gifDrawable.getDuration() ; //獲取播放一次所需要的時間
總結
以上所述是小編給大家介紹的在Android 加載GIF圖最佳實踐方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答