写在开头
本篇博文并不是按照Glide的代码来实现,而是根据Glide的架构和思想来完成这个“WriteGlide”的demo。
架构
单看这图片可能不好理解,可以理解成去银行的处理业务。
有很多人来到银行处理业务,例如小明(BitmapRequest)来取钱,老王来办银行卡,等等。那来办业务需要带领银行卡或者身份证来处理业务,相当于加载图片(BitmapRequest)需要携带url。
银行柜台有多个窗口,每个窗口对应一个小姐姐(BitmapDispatcher),当小明(BitmapRequest)过来时,小姐姐就需要去处理他的业务(从三级缓存获取图片),RequestManager用来管理银行,例如银行到点上班了,然后要多少个柜台,多少个小姐姐来处理业务。
这个其实就是典型的生产者–消费者模式,来银行办理业务的人员就是生产者,柜台的小姐姐就是消费者。既然是生产者–消费者模式,这里就需要考虑并发问题,这里采用阻塞队列(BlockingQueue)来实现。
我们下面来看下Glide如何调用的
Glide.with(this).loading(R.drawable.loading).load(IMAGE_URL[0])
.listener(new RequestListener() {
@Override
public boolean onException() {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource) {
Toast.makeText(MainActivity.this, "自定义处理图片(比如设置圆角)"
return false;
}
})
.into(imageView);
上面球球的参数对应的就是BItmapRequest所需要的参数:
private String url; //图片地址
private SoftReference<ImageView> softReference;
//这个主要和imageview做绑定,防止图片错位
private String urlMD5;
//正在等待的图片
private int loadingResId;
private Context context;
private RequestListener requestListener;
public BitmapRequest(Context context){
this.context = context;
}
public BitmapRequest loading(int loadingResId){
this.loadingResId = loadingResId;
return this;
}
public BitmapRequest listener(RequestListener requestListener){
this.requestListener = requestListener;
return this;
}
public BitmapRequest load(String url){
this.url = url;
this.urlMD5 = MD5Utils.toMD5(url);
return this;
}
public void into(ImageView imageView){
this.softReference = new SoftReference<>(imageView);
imageView.setTag(urlMD5);
RequestManager.getInstance().addBitmapRequest(this);
}
public String getUrl() {
return url;
}
public ImageView getImageView() {
return softReference.get();
}
public String getUrlMD5() {
return urlMD5;
}
public int getLoadingResId() {
return loadingResId;
}
public Context getContext() {
return context;
}
public RequestListener getRequestListener() {
return requestListener;
}
上面的代码需要注意的是load(url)的时候需要设置下图片的urlMd5值,之后再into的时候和图片进行绑定。