Google推薦的圖片加載庫Glide介紹

英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google

首發地址  http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html

在泰國舉行的谷歌開發者論壇上,谷歌爲我們介紹了一個名叫 Glide 的圖片加載庫,作者是bumptech。這個庫被廣泛的運用在google的開源項目中,包括2014年google I/O大會上發佈的官方app。

它的成功讓我非常感興趣。我花了一整晚的時間把玩,決定分享一些自己的經驗。在開始之前我想說,Glide和Picasso有90%的相似度,準確的說,就是Picasso的克隆版本。但是在細節上還是有不少區別的。

導入庫

Picasso和Glide都在jcenter上。在項目中添加依賴非常簡單:

Picasso

[js] view plain copy
  1. dependencies {  
  2.     compile 'com.squareup.picasso:picasso:2.5.1'  
  3. }  

Glide

[js] view plain copy
  1. dependencies {  
  2.     compile 'com.github.bumptech.glide:glide:3.5.2'  
  3.     compile 'com.android.support:support-v4:22.0.0'  
  4. }  

Glide需要依賴Support Library v4,別忘了。其實Support Library v4已經是應用程序的標配了,這不是什麼問題。

基礎

就如我所說的Glide和Picasso非常相似,Glide加載圖片的方法和Picasso如出一轍。

Picasso

[js] view plain copy
  1. Picasso.with(context)  
  2.     .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")  
  3.     .into(ivImg);  

Glide

[js] view plain copy
  1. Glide.with(context)  
  2.     .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")  
  3.     .into(ivImg);  

雖然兩者看起來一樣,但是Glide更易用,因爲Glide的with方法不光接受Context,還接受Activity 和 Fragment,Context會自動的從他們獲取。

with

同時將Activity/Fragment作爲with()參數的好處是:圖片加載會和Activity/Fragment的生命週期保持一致,比如Paused狀態在暫停加載,在Resumed的時候又自動重新加載。所以我建議傳參的時候傳遞Activity 和 Fragment給Glide,而不是Context。

默認Bitmap格式是RGB_565

下面是加載圖片時和Picasso的比較(1920x1080 像素的圖片加載到768x432的ImageView中)

firstload

可以看到Glide加載的圖片質量要差於Picasso(ps:我看不出來哈),爲什麼?這是因爲Glide默認的Bitmap格式是RGB_565 ,比ARGB_8888格式的內存開銷要小一半。下面是Picasso在ARGB8888下與Glide在RGB565下的內存開銷圖(應用自身佔用了8m,因此以8爲基準線比較):

ram1_1

如果你對默認的RGB_565效果還比較滿意,可以不做任何事,但是如果你覺得難以接受,可以創建一個新的GlideModule將Bitmap格式轉換到ARGB_8888

[js] view plain copy
  1. public class GlideConfiguration implements GlideModule {  
  2.    
  3.     @Override  
  4.     public void applyOptions(Context context, GlideBuilder builder) {  
  5.         // Apply options to the builder here.  
  6.         builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);  
  7.     }  
  8.    
  9.     @Override  
  10.     public void registerComponents(Context context, Glide glide) {  
  11.         // register ModelLoaders here.  
  12.     }  
  13. }  

同時在AndroidManifest.xml中將GlideModule定義爲meta-data

[js] view plain copy
  1. <meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"  
  2.             android:value="GlideModule"/>  
  3.   
  4. <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294447874.jpg" alt="quality2" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">這樣看起來就會好很多。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">我們再來看看內存開銷圖,這次貌似Glide花費了兩倍於上次的內存,但是Picasso的內存開銷仍然遠大於Glide。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294918728.png" alt="ram2_1" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">原因在於Picasso是加載了全尺寸的圖片到內存,然後讓GPU來實時重繪大小。而Glide加載的大小和ImageView的大小是一致的,因此更小。當然,Picasso也可以指定加載的圖片大小的:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Picasso.with(this)  
  5.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  6.     .resize(768, 432)  
  7.     .into(ivImgPicasso);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">但是問題在於你需要主動計算ImageView的大小,或者說你的ImageView大小是具體的值(而不是wrap_content),你也可以這樣:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Picasso.with(this)  
  8.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  9.     .fit()  
  10.     .centerCrop()  
  11.     .into(ivImgPicasso);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">現在Picasso的內存開銷就和Glide差不多了。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294433243.png" alt="memory3" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">雖然內存開銷差距不到,但是在這個問題上Glide完勝Picasso。因爲Glide可以自動計算出任意情況下的ImageView大小。</p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t3"></a><span class="section-heading">Image質量的細節</span></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">這是將ImageView還原到真實大小時的比較。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294704430.png" alt="quality3" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">你可以看到,Glide加載的圖片沒有Picasso那麼平滑,我還沒有找到一個可以直觀改變圖片大小調整<a href="http://lib.csdn.net/base/datastructure" class="replace_word" title="算法與數據結構知識庫" target="_blank" style="color:#df3434; font-weight:bold;">算法</a>的方法。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">但是這並不算什麼壞事,因爲很難察覺。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t4"></a>磁盤緩存</h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso和Glide在磁盤緩存策略上有很大的不同。Picasso緩存的是全尺寸的,而Glide緩存的是跟ImageView尺寸相同的。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294110987.jpg" alt="cache" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">上面提到的平滑度的問題依然存在,而且如果加載的是RGB565圖片,那麼緩存中的圖片也是RGB565。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> </p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">我嘗試將ImageView調整成不同大小,但不管大小如何Picasso只緩存一個全尺寸的。Glide則不同,它會爲每種大小的ImageView緩存一次。儘管一張圖片已經緩存了一次,但是假如你要在另外一個地方再次以不同尺寸顯示,需要重新下載,調整成新尺寸的大小,然後將這個尺寸的也緩存起來。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">具體說來就是:假如在第一個頁面有一個200x200的ImageView,在第二個頁面有一個100x100的ImageView,這兩個ImageView本來是要顯示同一張圖片,卻需要下載兩次。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">不過,你可以改變這種行爲,讓<span style="color:rgb(41,128,185)">Glide</span>既緩存全尺寸又緩存其他尺寸:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Glide.with(this)  
  12.      .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  13.      .diskCacheStrategy(DiskCacheStrategy.ALL)  
  14.      .into(ivImgGlide);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">下次在任何ImageView中加載圖片的時候,全尺寸的圖片將從緩存中取出,重新調整大小,然後緩存。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Glide的這種方式優點是加載顯示非常快。而Picasso的方式則因爲需要在顯示之前重新調整大小而導致一些延遲,即便你添加了這段代碼來讓其立即顯示:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">//Picasso  
  15. .noFade();</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img src="http://jcodecraeer.com/uploads/allimg/150327/163Aa632-0.gif" alt="loading3" style="border:none; max-width:100%; display:block; margin-left:auto; margin-right:auto"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso和Glide各有所長,你根據自己的需求選擇合適的。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">對我而言,我更喜歡Glide,因爲它遠比Picasso快,雖然需要更大的空間來緩存。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294447874.jpg" alt="quality2" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">這樣看起來就會好很多。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">我們再來看看內存開銷圖,這次貌似Glide花費了兩倍於上次的內存,但是Picasso的內存開銷仍然遠大於Glide。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294918728.png" alt="ram2_1" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">原因在於Picasso是加載了全尺寸的圖片到內存,然後讓GPU來實時重繪大小。而Glide加載的大小和ImageView的大小是一致的,因此更小。當然,Picasso也可以指定加載的圖片大小的:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Picasso.with(this)  
  16.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  17.     .resize(768, 432)  
  18.     .into(ivImgPicasso);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">但是問題在於你需要主動計算ImageView的大小,或者說你的ImageView大小是具體的值(而不是wrap_content),你也可以這樣:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Picasso.with(this)  
  19.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  20.     .fit()  
  21.     .centerCrop()  
  22.     .into(ivImgPicasso);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">現在Picasso的內存開銷就和Glide差不多了。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294433243.png" alt="memory3" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">雖然內存開銷差距不到,但是在這個問題上Glide完勝Picasso。因爲Glide可以自動計算出任意情況下的ImageView大小。</p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t5"></a><span class="section-heading">Image質量的細節</span></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">這是將ImageView還原到真實大小時的比較。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294704430.png" alt="quality3" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">你可以看到,Glide加載的圖片沒有Picasso那麼平滑,我還沒有找到一個可以直觀改變圖片大小調整算法的方法。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">但是這並不算什麼壞事,因爲很難察覺。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t6"></a>磁盤緩存</h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso和Glide在磁盤緩存策略上有很大的不同。Picasso緩存的是全尺寸的,而Glide緩存的是跟ImageView尺寸相同的。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427445294110987.jpg" alt="cache" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">上面提到的平滑度的問題依然存在,而且如果加載的是RGB565圖片,那麼緩存中的圖片也是RGB565。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> </p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">我嘗試將ImageView調整成不同大小,但不管大小如何Picasso只緩存一個全尺寸的。Glide則不同,它會爲每種大小的ImageView緩存一次。儘管一張圖片已經緩存了一次,但是假如你要在另外一個地方再次以不同尺寸顯示,需要重新下載,調整成新尺寸的大小,然後將這個尺寸的也緩存起來。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">具體說來就是:假如在第一個頁面有一個200x200的ImageView,在第二個頁面有一個100x100的ImageView,這兩個ImageView本來是要顯示同一張圖片,卻需要下載兩次。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">不過,你可以改變這種行爲,讓<span style="color:rgb(41,128,185)">Glide</span>既緩存全尺寸又緩存其他尺寸:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">Glide.with(this)  
  23.      .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  24.      .diskCacheStrategy(DiskCacheStrategy.ALL)  
  25.      .into(ivImgGlide);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">下次在任何ImageView中加載圖片的時候,全尺寸的圖片將從緩存中取出,重新調整大小,然後緩存。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Glide的這種方式優點是加載顯示非常快。而Picasso的方式則因爲需要在顯示之前重新調整大小而導致一些延遲,即便你添加了這段代碼來讓其立即顯示:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">//Picasso  
  26. .noFade();</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img src="http://jcodecraeer.com/uploads/allimg/150327/163Aa632-0.gif" alt="loading3" style="border:none; max-width:100%; display:block; margin-left:auto; margin-right:auto"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso和Glide各有所長,你根據自己的需求選擇合適的。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">對我而言,我更喜歡Glide,因爲它遠比Picasso快,雖然需要更大的空間來緩存。</p><br><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t7"></a><span class="section-heading">特性<br></span></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">你可以做到幾乎和Picasso一樣多的事情,代碼也幾乎一樣。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><strong>Image Resizing</strong></p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">// Picasso  
  27. .resize(300, 200);  
  28.    
  29. // Glide  
  30. .override(300, 200);</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><strong>Center Cropping</strong></p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">// Picasso  
  31. .centerCrop();  
  32.    
  33. // Glide  
  34. .centerCrop();</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><strong>Transforming</strong></p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">// Picasso  
  35. .transform(new CircleTransform())  
  36.    
  37. // Glide  
  38. .transform(new CircleTransform(context))</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">設置佔位圖或者加載錯誤圖:</p><pre class="js" style="white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51); font-size:14px; line-height:26px; background-color:rgb(255,255,255)" name="code">// Picasso  
  39. .placeholder(R.drawable.placeholder)  
  40. .error(R.drawable.imagenotfound)  
  41.    
  42. // Glide  
  43. .placeholder(R.drawable.placeholder)  
  44. .error(R.drawable.imagenotfound)</pre><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">幾乎和Picasso一樣,從Picasso轉換到Glide對你來說就是小菜一碟。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span class="section-heading"> </span></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t8"></a><span class="section-heading">有什麼Glide可以做而<span class="section-heading">Picasso</span>做不到</span></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Glide可以加在GIF動態圖,而Picasso不能。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img src="http://jcodecraeer.com/uploads/20150327/1427445366503084.gif" alt="gifanimation2" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">同時因爲Glide和Activity/Fragment的生命週期是一致的,因此gif的動畫也會自動的隨着Activity/Fragment的狀態暫停、重放。Glide 的緩存在gif這裏也是一樣,調整大小然後緩存。<br></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">但是從我的一次<a href="http://lib.csdn.net/base/softwaretest" class="replace_word" title="軟件測試知識庫" target="_blank" style="color:#df3434; font-weight:bold;">測試</a>結果來看Glide 動畫會消費太多的內存,因此謹慎使用。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">除了gif動畫之外,Glide還可以將任何的本地視頻解碼成一張靜態圖片。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">還有一個特性是你可以配置圖片顯示的動畫,而Picasso只有一種動畫:fading in。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">最後一個是可以使用<code>thumbnail()產生一個你所加載圖片的<span style="color:rgb(22,160,133)">thumbnail</span>。</code></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><code>其實還有一些特性,不過不是非常重要,比如將圖像轉換成字節數組等。</code></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t9"></a><code>配置</code></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">有許多可以配置的選項,比如大小,緩存的磁盤位置,最大緩存空間,位圖格式等等。可以在這個頁面查看這些配置 <code><a target="_blank" href="https://github.com/bumptech/glide/wiki/Configuration" style="color:rgb(51,102,153); text-decoration:none">Configuration</a></code>。<br></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t10"></a>庫的大小</h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso (v2.5.1)的大小約118kb,而Glide (v3.5.2)的大小約430kb。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427453389115686.png" alt="librarysize" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Anyway 312KB difference might not be that significant.</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">不過312kb的差距並不是很重要。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Picasso和Glide的方法個數分別是840和2678個。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img class="width-100percent" src="http://jcodecraeer.com/uploads/20150327/1427453390188737.png" alt="methodcount" style="border:none; max-width:100%"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">必須指出,對於DEX文件65535個方法的限制來說,2678是一個相當大的數字了。建議在使用Glide的時候開啓ProGuard。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t11"></a><span class="section-heading">總結<br></span></h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Glide和Picasso都是非常完美的庫。Glide加載圖像以及磁盤緩存的方式都要優於Picasso,速度更快,並且Glide更有利於減少OutOfMemoryError的發生,GIF動畫是Glide的殺手鐗。不過Picasso的圖片質量更高。你更喜歡哪個呢?</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">雖然我使用了很長事件的Picasso,但是我得承認現在我更喜歡Glide。我的建議是使用Glide,但是將Bitmap格式換成 ARGB_8888、讓Glide緩存同時緩存全尺寸和改變尺寸兩種。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> </p><h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t12"></a>相關資源</h2><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">- <a target="_blank" href="http://google-opensource.blogspot.com/2014/09/glide-30-media-management-library-for.html" style="color:rgb(51,102,153); text-decoration:none">Glide 3.0: a media management library for Android</a></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">- <a target="_blank" href="https://github.com/bumptech/glide/wiki" style="color:rgb(51,102,153); text-decoration:none">Glide Wiki</a></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">- <a target="_blank" href="http://pluu.github.io/android%20study/2015/01/15/android-glide-picasso/" style="color:rgb(51,102,153); text-decoration:none">Android Picasso vs Glide</a></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">- <a target="_blank" href="http://vardhan-justlikethat.blogspot.com/2014/09/android-image-loading-libraries-picasso.html" style="color:rgb(51,102,153); text-decoration:none">Android: Image loading libraries Picasso vs Glide</a></p><br>  

quality2

這樣看起來就會好很多。

我們再來看看內存開銷圖,這次貌似Glide花費了兩倍於上次的內存,但是Picasso的內存開銷仍然遠大於Glide。

ram2_1

原因在於Picasso是加載了全尺寸的圖片到內存,然後讓GPU來實時重繪大小。而Glide加載的大小和ImageView的大小是一致的,因此更小。當然,Picasso也可以指定加載的圖片大小的:

[js] view plain copy
  1. Picasso.with(this)  
  2.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  3.     .resize(768, 432)  
  4.     .into(ivImgPicasso);  

但是問題在於你需要主動計算ImageView的大小,或者說你的ImageView大小是具體的值(而不是wrap_content),你也可以這樣:

[js] view plain copy
  1. Picasso.with(this)  
  2.     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  3.     .fit()  
  4.     .centerCrop()  
  5.     .into(ivImgPicasso);  

現在Picasso的內存開銷就和Glide差不多了。

memory3

雖然內存開銷差距不到,但是在這個問題上Glide完勝Picasso。因爲Glide可以自動計算出任意情況下的ImageView大小。

Image質量的細節

這是將ImageView還原到真實大小時的比較。

quality3

你可以看到,Glide加載的圖片沒有Picasso那麼平滑,我還沒有找到一個可以直觀改變圖片大小調整算法的方法。

但是這並不算什麼壞事,因爲很難察覺。


磁盤緩存

Picasso和Glide在磁盤緩存策略上有很大的不同。Picasso緩存的是全尺寸的,而Glide緩存的是跟ImageView尺寸相同的。


cache

上面提到的平滑度的問題依然存在,而且如果加載的是RGB565圖片,那麼緩存中的圖片也是RGB565。

 

我嘗試將ImageView調整成不同大小,但不管大小如何Picasso只緩存一個全尺寸的。Glide則不同,它會爲每種大小的ImageView緩存一次。儘管一張圖片已經緩存了一次,但是假如你要在另外一個地方再次以不同尺寸顯示,需要重新下載,調整成新尺寸的大小,然後將這個尺寸的也緩存起來。

具體說來就是:假如在第一個頁面有一個200x200的ImageView,在第二個頁面有一個100x100的ImageView,這兩個ImageView本來是要顯示同一張圖片,卻需要下載兩次。

不過,你可以改變這種行爲,讓Glide既緩存全尺寸又緩存其他尺寸:

[js] view plain copy
  1. Glide.with(this)  
  2.      .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")  
  3.      .diskCacheStrategy(DiskCacheStrategy.ALL)  
  4.      .into(ivImgGlide);  

下次在任何ImageView中加載圖片的時候,全尺寸的圖片將從緩存中取出,重新調整大小,然後緩存。

Glide的這種方式優點是加載顯示非常快。而Picasso的方式則因爲需要在顯示之前重新調整大小而導致一些延遲,即便你添加了這段代碼來讓其立即顯示:

[js] view plain copy
  1. //Picasso  
  2. .noFade();  

loading3


Picasso和Glide各有所長,你根據自己的需求選擇合適的。

對我而言,我更喜歡Glide,因爲它遠比Picasso快,雖然需要更大的空間來緩存。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章