英文原文 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会自动的从他们获取。

同时将Activity/Fragment作为with()参数的好处是:图片加载会和Activity/Fragment的生命周期保持一致,比如Paused状态在暂停加载,在Resumed的时候又自动重新加载。所以我建议传参的时候传递Activity 和 Fragment给Glide,而不是Context。

默认Bitmap格式是RGB_565

下面是加载图片时和Picasso的比较(1920x1080 像素的图片加载到768x432的ImageView中)

可以看到Glide加载的图片质量要差于Picasso(ps:我看不出来哈),为什么?这是因为Glide默认的Bitmap格式是RGB_565 ,比ARGB_8888格式的内存开销要小一半。下面是Picasso在ARGB8888下与Glide在RGB565下的内存开销图(应用自身占用了8m,因此以8为基准线比较):

如果你对默认的RGB_565效果还比较满意,可以不做任何事,但是如果你觉得难以接受,可以创建一个新的GlideModule将Bitmap格式转换到ARGB_8888

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

同时在AndroidManifest.xml中将GlideModule定义为meta-data

[js]  view plain copy
  1. <meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"
  2. android:value="GlideModule"/>
  3. <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)
  4. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  5. .resize(768, 432)
  6. .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)
  7. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  8. .fit()
  9. .centerCrop()
  10. .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那么平滑,我还没有找到一个可以直观改变图片大小调整算法的方法。</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)
  11. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  12. .diskCacheStrategy(DiskCacheStrategy.ALL)
  13. .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
  14. .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)
  15. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  16. .resize(768, 432)
  17. .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)
  18. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  19. .fit()
  20. .centerCrop()
  21. .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)
  22. .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
  23. .diskCacheStrategy(DiskCacheStrategy.ALL)
  24. .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
  25. .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
  26. .resize(300, 200);
  27. // Glide
  28. .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
  29. .centerCrop();
  30. // Glide
  31. .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
  32. .transform(new CircleTransform())
  33. // Glide
  34. .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
  35. .placeholder(R.drawable.placeholder)
  36. .error(R.drawable.imagenotfound)
  37. // Glide
  38. .placeholder(R.drawable.placeholder)
  39. .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">但是从我的一次测试结果来看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>

这样看起来就会好很多。

我们再来看看内存开销图,这次貌似Glide花费了两倍于上次的内存,但是Picasso的内存开销仍然远大于Glide。

原因在于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差不多了。

虽然内存开销差距不到,但是在这个问题上Glide完胜Picasso。因为Glide可以自动计算出任意情况下的ImageView大小。

Image质量的细节

这是将ImageView还原到真实大小时的比较。

你可以看到,Glide加载的图片没有Picasso那么平滑,我还没有找到一个可以直观改变图片大小调整算法的方法。

但是这并不算什么坏事,因为很难察觉。

磁盘缓存

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

上面提到的平滑度的问题依然存在,而且如果加载的是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();

Picasso和Glide各有所长,你根据自己的需求选择合适的。

对我而言,我更喜欢Glide,因为它远比Picasso快,虽然需要更大的空间来缓存。

Android Google推荐的图片加载库Glide介绍相关推荐

  1. Android之Google推荐的图片加载库Glide介绍

    原文链接:Google推荐的图片加载库Glide介绍 作者 : nuuneoi 译者 : jianghejie 校对者 :

  2. Google推荐的图片加载库Glide介绍(与Picasso比较)

    在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官 ...

  3. Google推荐的图片加载库Glide于Picasso比较

    在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官 ...

  4. 图片加载库Glide介绍

    英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google 首发地址   http://jc ...

  5. Android开源框架——图片加载库Glide

    Glide是有google开发的图片加载库,支持图片加载与处理,包括动态图片的加载,以及视频的解码. 开源地址:https://github.com/bumptech/glide build.grad ...

  6. Google图片加载库Glide的简单封装GlideUtils

    Google图片加载库Glide的简单封装GlideUtils  

  7. Carson带你学Android:主流开源图片加载库对比(UIL、Picasso、Glide、Fresco)

    前言 图片加载在 Android开发项目中十分常见 为了降低开发周期 & 难度,我们经常会选用一些图片加载的开源库,而现在图片加载开源库越来越多,我们应该选用哪种呢? 今天.我就给大家介绍 & ...

  8. Google推荐图片加载库Glide使用总结

    在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫Glide的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官方a ...

  9. android 图片加载库 Glide 的使用介绍

    一:简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会 ...

最新文章

  1. 爆款入门 | 微生物组-扩增子16S分析和可视化(线上/线下同时开课,2022.4)
  2. 转载 .net面试题大全(有答案)
  3. 美好生活从java开始
  4. HotSpot源码(一):Docker与虚拟机的区别,class字节码解析,linux内核源码下载地址,Yacc与Lex快速入门
  5. Apache Camel请向我解释这些端点选项的含义
  6. 前端学习(1942)vue之电商管理系统电商系统之创建商品分类的分支
  7. HTML DOM content 属性
  8. 【项目管理】CMM能力成熟度模型
  9. 力扣232. 用栈实现队列(JavaScript)
  10. 数据库增删改查工具类 以及C3P0开源的JDBC连接池操作
  11. java将汉字字符串转换为拼音(包含多音字)
  12. 微信扫一扫门禁开门小程序开发制作
  13. 微软雅黑与等宽字体Source Code Pro下载
  14. 饮用水公司配送管理系统可行性报告
  15. 爆火的羊了个羊背后暗含的广告变现逻辑是什么?
  16. bzoj 2794: Cloakroom dp
  17. [Win32] 窗体暗色模式, C++, WinForm, WPF 使用方法, 判断颜色模式, 响应颜色变更消息, 设置标题栏暗色.
  18. C语言编程编制职工档案管理程序,C语言课程设计--职工档案及简明信息生成.doc...
  19. python 拼音地名对应关系,使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务...
  20. C语言打印乘法口诀表

热门文章

  1. 08 excel vba 根据学生学号输入成绩
  2. css2选择器写法大全
  3. 循环语句中指针赋值出错
  4. 不仅清晰还要美 OPPO臻美自拍2000万像素再升级
  5. 微信惊现任意代码执行漏洞 360手机卫士提供自检方案
  6. 运动耳机什么牌子的好、运动耳机排行榜10强
  7. 鼎盛合|磁吸式移动电源充气泵方案
  8. iPhone 4S不给力 iPhone 4层层加价
  9. xamarin android框架,Xamarin使用的架构
  10. js 二叉树图形_JS实现二叉树 - 山海散客 - OSCHINA - 中文开源技术交流社区