自我学习Glide(一)Glide.with(this)

1,223 阅读3分钟

初次认识Glide

Glide基本用法 借鉴 加载本地图片

File file = new File(getExternalCacheDir() + "/image.jpg");

Glide.with(this).load(file).into(imageView);/

/ 加载应用资源int resource = R.drawable.image;

Glide.with(this).load(resource).into(imageView);

// 加载二进制流byte[] image = getImageBytes();

Glide.with(this).load(image).into(imageView);

// 加载Uri对象Uri imageUri = getImageUri();

Glide.with(this).load(imageUri).into(imageView);

with()方法是Glide类中的一组静态方法,我们开始进入Glide.with()进行里面具体都干了啥,

初次映入眼帘咱经常见的with()方法重载

(1)with的方法 接着往下走看看这个getRetriever()这个方法去干啥了(呀!有英文咋弄,不怕咱能电脑翻译)

1)with的方法.png

getRetriever() 在这里已经明显为生命周期做准备了判空,还提醒我们视图绑定,像咱自己写代码封装代码,一定也要注意为空判断和是否自己需要的类型判断哟!接着又去getRequestManagerRetriever()这个方法我们接着往下看

getRetriever().png

getRequestManagerRetriever() RequestManagerRetriever(请求管理器检索器)在这直接return 出去了那我们就得想知道请求管理器检索器是啥?

RequestManagerRetriever.png

RequestManagerRetriever

image.png

进入里面直接看到为RequestManager服务的,接着看咋为RequestManager服务的

看着熟悉,就是咱们在外面调用的相对应get()方法那咱看一下方法里面

RequestManager.get().png

RequestManager.get()

RequestManagerRetriever类中会对context加以区分后走对应的get方法,首先判断如果不是主线程,则用ApplicationContext调用get方法:

image.png

getApplicationManager() 如果是在主线程调用,不管是context是fragment是activtiy,最后都会走fragmentGet和supportFragmentGet这两个方法:

image.png

image.png

fragmentGet

supportFragmentGet 都是向当前页面添加一个fragment,一个是添加v4包的,一个是添加android.app.Fragment(已经弃用仍在使用),那我们先SupportfragmentGet看看.首先通过getSupportRequestManagerFragment获得了一个SupportRequestManagerFragment对象:

SupportRequestManagerFragment:一个没有视图的Fragment,持有ActivityFragmentLifecycle实例,并持有RequestManager实例,RequestManager可以启动、停止、管理Glide的requests请求。

image.png

getSupportRequestManagerFragment() 1.创建没有视图的的Fragment,并绑定到当前activity;

2、通过builder模式创建RequestManager,并且将fragment的lifecycle传入,这样Fragment和RequestManager就建立了联系;3、获取Fragment对象,先根据tag去找,如果没有从内存中查找,pendingSupportRequestManagerFragments是一个存储fragment实例的HashMap,再没有的话就new一个。因为Glide.wth(context)最后就是返回了一个RequestManagert对象,所以这一步到这里结束了,那添加的这个fragment有什么用呢,我们看一下SupportRequestManagerFragment核心方

image.png

SupportRequestManagerFragment 1

image.png SupportRequestManagerFragment 2 这里为什么监控Fragment的生命周期呢,其实大家应该也知道Fragment是依附在 Activity 的 Activity 的生命周期在 Fragment 中都有,所以监听 Fragment 就行了。

ActivityFragmentLifecycle 明显可以知道生命周期里面具体什么呢走进去看一下!

image.png

ActivityFragmentLifecycle注释翻译

image.png ActivityFragmentLifecycle生命周期

image.png

onStart() 1:在创建Fragment的时候会创建ActivityFragmentLifecycle对象;2:在Fragment生命周期的方法中会调用Lifecycle的相关方法来通知RequestManager;3:LifecycleListener是一个接口,Lifecycle最终是调用了lifecycleListener来通知相关的实现类的,也就是RequestManager。

4:这里知道了吧,它实现的是Glide中的 Lifecycle 生命周期接口,注册是在刚刚我们讲解 RequestManagerFactory 工厂中实例化的 RequestManager 然后在构造函数中添加了生命周期回调监听,具体来看下。

image.png

RequestManager

image.png RequestManager RequestManage

小总结

image.png

来自一位大佬的总结图

1、创建一个没有视图的Fragment,具体来说是SupportRequestManagerFragment/RequestManagerFragment,并绑定到当前Activity,这样Fragment就可以感知Activity的生命周期; 2、在创建Fragment时,初始化Lifecycle、LifecycleListener,并且在生命周期的onStart() 、onStop()、 onDestroy()中调用相关方法; 3、在创建RequestManager时传入Lifecycle 对象,并且LifecycleListener实现了LifecycleListener接口; 4、这样当生命周期变化的时候,就能通过接口回调去通知RequestManager处理请求。 5、其实就是为了得到一个RequestManager对象而已,然后Glide会根据我们传入with()方法的参数来确定图片加载的生命周期,接下来我们开始分析第二步,load()方法。

借鉴链接:blog.csdn.net/liumeng1233…

借鉴链接:www.jianshu.com/p/79dd4953e…

借鉴链接:www.jianshu.com/p/7194e151d…

借鉴链接:www.jianshu.com/p/36092fe62…