1.)添加引用 build.gradle 中添加配置
compile 'com.github.bumptech.glide:glide:3.7.0'
2.)设置绑定生命周期
我们可以更加高效的使用Glide提供的方式进行绑定,这样可以更好的让加载图片的请求的生命周期动态管理起来
1
2
3
4
|
Glide.
with
(Context context);
// 绑定Context
Glide.
with
(Activity activity);
// 绑定Activity
Glide.
with
(FragmentActivity activity);
// 绑定FragmentActivity
Glide.
with
(Fragment fragment);
// 绑定Fragment
|
3. )简单的加载图片实例
Glide.with(this).load(imageUrl).into(imageView);
4.)设置加载中以及加载失败图片
api里面对placeholder()、error()函数中有多态实现 用的时候可以具体的熟悉一下
1
|
Glide.with(
this
).load(imageUrl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
|
5.)设置跳过内存缓存
Glide.with(this).load(imageUrl).skipMemoryCache(true).into(imageView);
6.)设置下载优先级
Glide.with(this).load(imageUrl).priority(Priority.NORMAL).into(imageView);
7.)设置缓存策略
1
2
3
4
5
6
|
Glide.with(
this
).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
// 策略解说:
// all:缓存源资源和转换后的资源
// none:不作任何磁盘缓存
// source:缓存源资源
// result:缓存转换后的资源
|
8.)设置加载动画
1
2
|
//api也提供了几个常用的动画:比如crossFade()
Glide.with(
this
).load(imageUrl).animate(R.anim.item_alpha_in).into(imageView);
|
9.)设置缩略图支持
1
2
|
// 这样会先加载缩略图 然后在加载全图
Glide.with(
this
).load(imageUrl).thumbnail(
0
.1f).into(imageView);
|
10.)设置加载尺寸
Glide.with(this).load(imageUrl).override(800, 800).into(imageView);
11.)设置动态转换
1
2
3
4
|
Glide.with(
this
).load(imageUrl).centerCrop().into(imageView);
// api提供了比如:centerCrop()、fitCenter()等函数也可以通过自定义Transformation
// 自定义Transformation具体使用
Glide.with(
this
).load(imageUrl).transform(
new
GlideRoundTransform(
this
)).into(imageView);
|
12.)设置要加载的内容
1
2
3
4
5
6
7
|
// 项目中有很多需要先下载图片然后再做一些合成的功能,比如项目中出现的图文混排,该如何实现目标下
Glide.with(
this
).load(imageUrl).centerCrop().into(
new
SimpleTarget<GlideDrawable>() {
@Override
public
void
onResourceReady(GlideDrawable resource, GlideAnimation<?
super
GlideDrawable> glideAnimation) {
imageView.setImageDrawable(resource);
}
});
|
13 .)设置监听请求接口 ,设置监听的用处 可以用于监控请求发生错误来源,以及图片来源 是内存还是磁盘
1
2
3
4
5
6
7
8
9
10
11
|
Glide.with(
this
).load(imageUrl).listener(
new
RequestListener<String, GlideDrawable>() {
@Override
public
boolean
onException(Exception e, String model, Target<GlideDrawable> target,
boolean
isFirstResource) {
return
false
;
}
@Override
public
boolean
onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
boolean
isFromMemoryCache,
boolean
isFirstResource) {
//imageView.setImageDrawable(resource);
return
false
;
}
}).into(imageView);
|
14.)设置动态GIF加载方式
1
2
|
Glide.
with
(
this
).load(imageUrl).asBitmap().
into
(imageView);
//显示gif静态图片
Glide.
with
(
this
).load(imageUrl).asGif().
into
(imageView);
//显示gif动态图片
|
15.)缓存的动态清理
1
2
|
Glide.get(
this
).clearDiskCache();
//清理磁盘缓存 需要在子线程中执行
Glide.get(
this
).clearMemory();
//清理内存缓存 可以在UI主线程中进行
|