Glide在许多方面和Picasso都非常的相似。
工程导入:
Picasso:
Picasso.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);
dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:22.0.0'
}
基本使用:
Picasso.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);
Glide.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);
默认的Bitmap格式:
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Apply options to the builder here.
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
@Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
并且在清单文件中配置:<meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"
android:value="GlideModule"/>
Picasso.with(this)
.load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
.resize(768, 432)
.into(ivImgPicasso);
问题是这样你就需要手动的测量ImageView的尺寸,或者说你的ImageView必须要有准确的尺寸(不能够设置成wrap_content),你也可以这样简单的设置:
Picasso.with(this)
.load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
.fit()
.centerCrop()
.into(ivImgPicasso);
磁盘缓存:
Glide.with(this)
.load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivImgGlide);
这样下次无论怎么改变ImageView的大小,它都不会再去重新加载不同的图片了,而是会从缓存中拿到full-size的图片改变它的大小然后展示。Glide可以做到Picasso不能做到的:
Glide可以加载GIF动画到ImageView,而Picasso不能。
上文提到过,Glide可以随Activity或者Fragment的生命周期暂停或者销毁对象,而Picasso不行。(不过我发现GIF的使用还是很占用内存的,谨慎使用)
Library的大小:
总结:
没有谁优谁劣,两者各有千秋。Glide的内存缓存机制相对比Picasso更好,它让图片加载显示的速度更快,也在一定程度上避免了OutOfMemoryError。在GIF动画的加载方面也是优于Picasso。但是Picasso在图片质量方面是远优于Gilde的。
附上原文地址:(应该是要科学上网的)
https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
(英语4级刚过,翻译的有点烂,主要是为了自己学习,有错误欢迎指出,有条件的自己去看原文更更好)