Glide使用

依赖导入

compile 'com.github.bumptech.glide:glide:3.7.0'

Glide和Picasso

  • Glide默认采用RGB_565的bitmap格式,因此加载的图片质量要比Picasso差,内存开销比Picasso小。
  • image
  • Picasso会加载全尺寸的图片到内存;而Glide加载的大小与ImageView的大小是一致的。
  • Picasso也可以指定加载的图片大小
Picasso.with(this)
    .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
    .resize(768, 432)
    .into(ivImgPicasso);
  • Picasso和Glide在磁盘缓存策略上有很大的不同。Picasso缓存的是全尺寸的,而Glide缓存的是跟ImageView尺寸相同的。
  • Glide对于不同大小的ImageView会缓存多次不同尺寸的图片,以下方式可以使得在任何ImageView中加载图片的时候,全尺寸的图片将从缓存中取出,重新调整大小,然后缓存
Glide.with(this)
     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")
     .diskCacheStrategy(DiskCacheStrategy.ALL)
     .into(ivImgGlide);
  • 总体来说,Glide比Picasso快,内存开销小,但需要的缓存空间大。

Glide的其他用法

  • Image Resizing
// Picasso
.centerCrop();
// Glide
.override(300, 200);
  • Transforming
// Picasso
.transform(new CircleTransform())

// Glide
.transform(new CircleTransform(context))
  • 设置占位
// Picasso
.placeholder(R.drawable.placeholder)
.error(R.drawable.imagenotfound)

// Glide
.placeholder(R.drawable.placeholder)
.error(R.drawable.imagenotfound)
  • 因为Glide加载图片时自动识别gif,通过下面代码使得Glide只加载静态图片或gif
//静态
.asBitmap()
//Gif
.asGif()

Glide可以做到,而Picasso不能的

  • Glide可以加载Gif图
    image
    同时因为Glide和Activity/Fragment的生命周期是一致的,因此gif的动画也会自动的随着Activity/Fragment的状态暂停、重放。Glide 的缓存在gif这里也是一样,调整大小然后缓存。

好用功能

  • 缩略图
   //用原图的1/10作为缩略图
Glide
    .with(this)
    .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
    .thumbnail(0.1f)
    .into(iv_0);
//用其它图片作为缩略图
DrawableRequestBuilder<Integer> thumbnailRequest = Glide
    .with(this)
    .load(R.drawable.news);
Glide.with(this)
    .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
    .thumbnail(thumbnailRequest)
    .into(iv_0);
  • 清楚缓存
// 必须在UI线程中调用
Glide.get(context).clearMemory();
  • 淡入淡出
  .crossFade() //设置淡入淡出效果,默认300ms,可以传参
  • 缓存策略
Glide支持多种磁盘缓存策略:

DiskCacheStrategy.NONE :不缓存图片
DiskCacheStrategy.SOURCE :缓存图片源文件
DiskCacheStrategy.RESULT:缓存修改过的图片(缓存最终图片) 默认是这个
DiskCacheStrategy.ALL:缓存所有的图片,默认

参考:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html

猜你喜欢

转载自blog.csdn.net/mingc0758/article/details/80102281