第一步:添加依赖:
//glide的依赖 implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
简单使用:一行代码即可搞定:
//一行代码搞定的方法 Glide.with(上下文).load(url地址).into(imageView);
复杂使用:需要设置placeholder图片显示类型:
1.老版本(glide版本4.6.0之前):
//老版本写法:
Glide.with(this)
.load(url)
.placeholder(R.mipmap.myuser_nologin)
.dontAnimate()
.error(R.mipmap.myuser_nologin)
.centerCrop()
.into(imageView);
2.新版本(glide版本4.6.0之后):
//新版Glide(4.6.0 之后) 需要设置options
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.drawable.booth_map)
.error(R.drawable.error)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
.load(listBean.getImage0())
.apply(options)
.into(viewHolder.ivPictureText);
Glide原理(源码解读):
在Glide的with方法里返回了RequestManager对象:
而不管哪个调用哪个with方法,都会进入到get方法中,就是以下方法:
而get方法中返回有一个方法fragmentGet:
@Deprecated
@NonNull
private RequestManager fragmentGet(@NonNull Context context, @NonNull FragmentManager fm, @Nullable android.app.Fragment parentHint, boolean isParentVisible) {
RequestManagerFragment current = this.getRequestManagerFragment(fm, parentHint, isParentVisible);
RequestManager requestManager = current.getRequestManager();
if (requestManager == null) {
Glide glide = Glide.get(context);
requestManager = this.factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
current.setRequestManager(requestManager);
}
return requestManager;
}
这里新建了一个视图的RequestManagerFragment:
RequestManagerFragment.class中持有一个lifecycle,在Fragment进入关键生命周期时会主动通知lifecycle执行相关方法
public class RequestManagerFragment extends Fragment {
.......
private final ActivityFragmentLifecycle lifecycle;
......
@Override
public void onStart() {
super.onStart();
lifecycle.onStart();
}
@Override
public void onStop() {
super.onStop();
lifecycle.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
unregisterFragmentWithRoot();
}
.........
}
ActivityFragmentLifecycle.class中持有一个lifecycleListeners,在Fragment进入关键生命周期时Lifecycle会通知他的所有Listener:
class ActivityFragmentLifecycle implements Lifecycle {
.............
private final Set lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap());
.............
void onStart() {
isStarted = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStart();
}
}
void onStop() {
isStarted = false;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStop();
}
}
void onDestroy() {
isDestroyed = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onDestroy();
}
}
}
RequestManger.class关键生命周期中处理加载任务:
@Override
public synchronized void onStart() {
resumeRequests();
targetTracker.onStart();
}
@Override
public synchronized void onStop() {
pauseRequests();
targetTracker.onStop();
}
@Override
public synchronized void onDestroy() {
targetTracker.onDestroy();
for (Target> target : targetTracker.getAll()) {
clear(target);
}
targetTracker.clear();
requestTracker.clearRequests();
lifecycle.removeListener(this);
lifecycle.removeListener(connectivityMonitor);
mainHandler.removeCallbacks(addSelfToLifecycle);
glide.unregisterRequestManager(this);
}
总结:
- 首先在Glide加载绑定了Activity的生命周期,比如with(context)方法
- 然后在Activity内新建一个无UI的fragment,这个特殊的fragment持有一个Lifecycle。通过Lifecycle在Fragment关键生命周期通知RequestManager进行相关操作。
- 最后在生命周期onStart时继续加载,在onStop时暂停加载,在onDestory时停止加载任务和清除操作
缓存机制:
- DiskCacheStrategy.NONE 不缓存文件
- DiskCacheStrategy.SOURCE 只缓存原图
- DiskCacheStrategy.RESULT 只缓存最终加载的图(默认的缓存略)
- DiskCacheStrategy.ALL 同时缓存原图和结果图
图片加载流程:
- 首先从ActivateResource获取,是个值为弱引用的Map
- MemoryCache和DiskCache是LruCache



