图片加载框架有很多,但是glide是google官方推荐的,而且使用起来非常的方便而且功能十分强大;
1.使用方法:
Glide.with(this).load("http://baidu.com").into(new ImageView(this));
简简单单的一句代码,就可以将url转换成图片放置在相应的控件中,很神奇。
2.源码解析:
从with方法开始:
public static RequestManager with(Activity activity) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(activity); }
先看这个RequestManager:从名字我们就可以看出,这是一个图片网络请求的管理类;
public class RequestManager implements LifecycleListener
它实现了LifecycleListener接口,这是一个与Context的生命周期绑定的接口,将request与生命周期绑定,这样就可以通过context的生命周期去操作网络请求的开始,暂停等;
再看retriever的get方法
public RequestManager get(FragmentActivity activity) { if (Util.isOnBackgroundThread()) { return get(activity.getApplicationContext()); } else { assertNotDestroyed(activity); FragmentManager fm = activity.getSupportFragmentManager(); return supportFragmentGet(activity, fm); } }
这里做了一个判断,如果Glide在子线程中使用或者传入的context是ApplicationContext,那么就与全局的ApplicationContext绑定,如果不是那么创建一个无界面的fragment,即SupportRequestManagerFragment,让请求和你的activity的生命周期同步;
在with方法中,主要是创建了一个requestManager的类,这个是网络请求的管理类,主要负责将所有的类的生命周期与调用其的组件的生命周期相绑定,这也是Glide与其他图片加载框架不一样的地方,它是和组件的生命周期相绑定的。
再看load方法:
load方法比较简单了,它的重载方法有很多,对应了加载图片的不同方式,可以传入String类型的值去加载,也可以通过url地址去加载,还可以通过传入本地文件去加载,但最后都会返回一个DrawableTypeRequest(继承了DrawableRequestBuilder);这个类就是支持链式调用的类,主要是进行一些初始化的操作;
最后看Glide的最核心的方法,into:
public Target<TranscodeType> into(ImageView view) { Util.assertMainThread(); if (view == null) { throw new IllegalArgumentException("You must pass in a non null View"); } if (!isTransformationSet && view.getScaleType() != null) { switch (view.getScaleType()) { case CENTER_CROP: applyCenterCrop(); break; case FIT_CENTER: case FIT_START: case FIT_END: applyFitCenter(); break; //$CASES-OMITTED$ default: // Do nothing. } } return into(glide.buildImageViewTarget(view, transcodeClass)); }
这里有三点要注意:
(1)
Util.assertMainThread();
这个方法会判断是否在主线程运行,如果不是,那么就会抛出异常,其实想想也可以想通,into方法就是将图片加载到相应的控件中,所以,就必须在主线程中运行;
(2)下面都是一些判断,如果你没有调用transform方法并且自己对图片的scaletype进行了设置,那么他会根据你的设置去加载图片;
(3)最后它调用into的方法:
public <Y extends Target<TranscodeType>> Y into(Y target) { Util.assertMainThread(); if (target == null) { throw new IllegalArgumentException("You must pass in a non null Target"); } if (!isModelSet) { throw new IllegalArgumentException("You must first set a model (try #load())"); } Request previous = target.getRequest(); if (previous != null) { previous.clear(); requestTracker.removeRequest(previous); previous.recycle(); } Request request = buildRequest(target); target.setRequest(request); lifecycle.addListener(target); requestTracker.runRequest(request); return target; }
其实这个代码逻辑很清晰,但是,此处有一个target不好理解,其实target就是对View的一层封装,就把他当成一个view就可以了,代码逻辑很好理解,主要就是对之前请求的彻底清除和对新请求的建立与绑定生命周期,这也就是控件复用时图片不会错位的原因;
关于新建请求是调用buildRequest的方法
private Request buildRequest(Target<TranscodeType> target) { if (priority == null) { priority = Priority.NORMAL; } return buildRequestRecursive(target, null); } private Request buildRequestRecursive(Target<TranscodeType> target, ThumbnailRequestCoordinator parentCoordinator) { if (thumbnailRequestBuilder != null) { if (isThumbnailBuilt) { throw new IllegalStateException("You cannot use a request as both the main request and a thumbnail, " + "consider using clone() on the request(s) passed to thumbnail()"); } // Recursive case: contains a potentially recursive thumb