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 , 歡迎關注


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM