天天看點

Android 加載GIF圖最佳實踐

轉載請标明出處:http://blog.csdn.net/zhaoyanjun6/article/details/75578109

本文出自【趙彥軍的部落格】

起因

最近在項目中遇到需要在界面上顯示一個本地的 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() ; //擷取播放一次所需要的時間

           

個人微信号:

zhaoyanjun125

, 歡迎關注