娄底网站建设79ld联通官网

news/2025/10/9 0:25:35/文章来源:
娄底网站建设79ld,联通官网,湖南广厦建设工程有限公司网站,静态网站建设的流程写作背景 Glide 作为著名的图片加载框架#xff0c;几乎每一个项目都使用到。笔者尝试通过别人的博客来了解 Glide 原理#xff0c;但是每次都是看着看着就懵逼了#xff0c;不是因为别人写的不好#xff0c;而是 Glide 实在是太复杂了#xff0c;于是决定自己撸一遍几乎每一个项目都使用到。笔者尝试通过别人的博客来了解 Glide 原理但是每次都是看着看着就懵逼了不是因为别人写的不好而是 Glide 实在是太复杂了于是决定自己撸一遍加深印象。纸上得来终觉浅绝知此事要躬行。 因为 Glide 太庞大了做不到面面俱到所以笔者主要先梳理主干核心原理后续持续整理。本文源码分析基于4.15.0就从最基础的调用 Glide.with(context).load(url).into(imageView) 来入手把主流程最核心的源码整理并且标注、总结。 该篇是Glide.with Glide.with 源码分析 类作用Glide负责线程池、缓存的构建RequestManagerRetriever负责获取RequestManagerSupportRequestManagerFragment用户接收和转发生命周期RequestManager用于请求管理 第1步从 Glide.with 出发可以看到Glide.with 有很多重载方法目的是返回一个RequestManager。Glide.java) //传递Contextpublic static RequestManager with(NonNull Context context) {return getRetriever(context).get(context);}// 传递FragmentActivitypublic static RequestManager with(NonNull FragmentActivity activity) {return getRetriever(activity).get(activity);}// 传递Fragmentpublic static RequestManager with(NonNull Fragment fragment) {return getRetriever(fragment.getContext()).get(fragment);}//传递Viewpublic static RequestManager with(NonNull View view) {return getRetriever(view.getContext()).get(view);}...... 第2步 Glide.with 里面会调用 getRetriever方法getRetriever() 先会调用 Glide.get()Glide.get 会调用checkAndInitializeGlide 方法注意会传递一个APT生成的 GeneratedAppGlideModuleImplGlide.java) private static RequestManagerRetriever getRetriever(Nullable Context context) {// 省略...return Glide.get(context).getRequestManagerRetriever();}//双层检测单例模式public static Glide get(NonNull Context context) {if (glide null) {//getAnnotationGeneratedGlideModules 内部通过。 //Class.forName(com.bumptech.glide.GeneratedAppGlideModuleImpl)//拿到GeneratedAppGlideModuleImpl 并且作为参数传递GeneratedAppGlideModule annotationGeneratedModule getAnnotationGeneratedGlideModules(context.getApplicationContext());synchronized (Glide.class) {if (glide null) {checkAndInitializeGlide(context, annotationGeneratedModule);}}}return glide;} 最终来到Glide.initializeGlideGeneratedAppGlideModuleImpl 的作用先记录后续再慢慢了解因为 Glide 内容实在太庞大了每一个细节都要掌握无法梳理主流程Glide.java) private static void initializeGlide(NonNull Context context,NonNull GlideBuilder builder,Nullable GeneratedAppGlideModule annotationGeneratedModule) {//1. 获取应用AppContext,最终是用来构建Glide的Context applicationContext context.getApplicationContext();//2.这里通过传递进来的 GeneratedAppGlideModuleImpl 调用 getExcludedModuleClassesListGlideModule manifestModules Collections.emptyList();if (annotationGeneratedModule null || annotationGeneratedModule.isManifestParsingEnabled()) {manifestModules new ManifestParser(applicationContext).parse();}if (annotationGeneratedModule ! null !annotationGeneratedModule.getExcludedModuleClasses().isEmpty()) {SetClass? excludedModuleClasses annotationGeneratedModule.getExcludedModuleClasses();IteratorGlideModule iterator manifestModules.iterator();while (iterator.hasNext()) {GlideModule current iterator.next();if (!excludedModuleClasses.contains(current.getClass())) {continue;}if (Log.isLoggable(TAG, Log.DEBUG)) {Log.d(TAG, AppGlideModule excludes manifest GlideModule: current);}iterator.remove();}}if (Log.isLoggable(TAG, Log.DEBUG)) {for (GlideModule glideModule : manifestModules) {Log.d(TAG, Discovered GlideModule from manifest: glideModule.getClass());}}//3.这里通过传递进来的 GeneratedAppGlideModuleImpl 获取 RequestManagerRetriever.RequestManagerFactoryRequestManagerRetriever.RequestManagerFactory factory annotationGeneratedModule ! null? annotationGeneratedModule.getRequestManagerFactory(): null;//4.将拿到的工厂添加到GlideBuilderbuilder.setRequestManagerFactory(factory);for (GlideModule module : manifestModules) {module.applyOptions(applicationContext, builder);}//5.这里通过传递进来的 GeneratedAppGlideModuleImpl 调用 applyOptionsif (annotationGeneratedModule ! null) {annotationGeneratedModule.applyOptions(applicationContext, builder);}//6.通过GlideBuilder 建造者模式生成 GlideGlide glide builder.build(applicationContext, manifestModules, annotationGeneratedModule);applicationContext.registerComponentCallbacks(glide);//7. 将构建出来的glide 赋值给 Glide 的静态变量Glide.glide glide;} 第3步我们看到了有一个GlideBuilder 来生成Glide这里有一个地方非常关键GliderBuilder 来构建Glide传递的Context 是applicationContext, 你外部使用activity, 这里都会getApplicationContext 传入  Glide build(NonNull Context context,ListGlideModule manifestModules,AppGlideModule annotationGeneratedGlideModule) {//实例化网络请求线程池if (sourceExecutor null) {sourceExecutor GlideExecutor.newSourceExecutor();}//实例化磁盘缓存线程池if (diskCacheExecutor null) {diskCacheExecutor GlideExecutor.newDiskCacheExecutor();}//实例化图片加载动画线程池if (animationExecutor null) {animationExecutor GlideExecutor.newAnimationExecutor();}//实例化图片加载内存大小计算器if (memorySizeCalculator null) {memorySizeCalculator new MemorySizeCalculator.Builder(context).build();}//实例化网络连接监控工厂if (connectivityMonitorFactory null) {connectivityMonitorFactory new DefaultConnectivityMonitorFactory();}//实例化Bitmap对象池if (bitmapPool null) {int size memorySizeCalculator.getBitmapPoolSize();if (size 0) {bitmapPool new LruBitmapPool(size);} else {bitmapPool new BitmapPoolAdapter();}}//实例化数组对象池if (arrayPool null) {arrayPool new LruArrayPool(memorySizeCalculator.getArrayPoolSizeInBytes());}//实例化内存缓存 LruCacheif (memoryCache null) {memoryCache new LruResourceCache(memorySizeCalculator.getMemoryCacheSize());}//实例化磁盘缓存工厂if (diskCacheFactory null) {diskCacheFactory new InternalCacheDiskCacheFactory(context);}//构建执行缓存策略跟线程池的引擎if (engine null) {engine new Engine(memoryCache,diskCacheFactory,diskCacheExecutor,sourceExecutor,GlideExecutor.newUnlimitedSourceExecutor(),animationExecutor,isActiveResourceRetentionAllowed);}if (defaultRequestListeners null) {defaultRequestListeners Collections.emptyList();} else {defaultRequestListeners Collections.unmodifiableList(defaultRequestListeners);}GlideExperiments experiments glideExperimentsBuilder.build();//实例化RequestManagerRetriever 请求管理类RequestManagerRetriever requestManagerRetriever new RequestManagerRetriever(requestManagerFactory);//实例化Glidereturn new Glide(context,engine,memoryCache,bitmapPool,arrayPool,requestManagerRetriever,connectivityMonitorFactory,logLevel,defaultRequestOptionsFactory,defaultTransitionOptions,defaultRequestListeners,manifestModules,annotationGeneratedGlideModule,experiments);} 第4步所以首次调用 Glide.get() 会把 Glide 构建完成那么调用 Glide 的 getRequestManagerRetriver()就能拿到 RequestManagerRetriver对象。下面看看RequestManagerRetriver.get() 方法也是有很多重载方法。 先看下RequestManagerRetriver.get(Context context), 可以看到如果不在主线程或者Context为AppContext那么调用的是getApplicationManager NonNullpublic RequestManager get(NonNull Context context) {if (context null) {throw new IllegalArgumentException(You cannot start a load on a null Context);} else if (Util.isOnMainThread() !(context instanceof Application)) {//1.主线程并且context不为Applicationif (context instanceof FragmentActivity) {//2.如果是FragmentActivityreturn get((FragmentActivity) context);} else if (context instanceof Activity) {//3.如果是Activityreturn get((Activity) context);} else if (context instanceof ContextWrapper ((ContextWrapper) context).getBaseContext().getApplicationContext() ! null) {//4.如果是ContextWrapper并且baseContext的AppContext 不为空return get(((ContextWrapper) context).getBaseContext());}}//5.不在主线程或者context为AppContext 调用getApplicationManagerreturn getApplicationManager(context);} 第5步先看getApplicationManager(context) 构建applicationManager的对象, 这个是App层级的RequestManager。 (RequestManagerReceiver.java) private RequestManager getApplicationManager(NonNull Context context) {// Either an application context or were on a background thread.if (applicationManager null) {synchronized (this) {if (applicationManager null) {//使用AppContext 作为参数拿GlideGlide glide Glide.get(context.getApplicationContext());applicationManager factory.build(glide,new ApplicationLifecycle(),//注意这里是ApplicationLifecyclenew EmptyRequestManagerTreeNode(),context.getApplicationContext());}}}return applicationManager;//返回RequestManager} 第6步再看看传递Activity 的情况, 最终会调用FragmentGet 生成一个Fragment 来监听生命周期。这里是最重要的一个方法 public RequestManager get(NonNull Activity activity) {if (Util.isOnBackgroundThread()) {return get(activity.getApplicationContext());} else if (activity instanceof FragmentActivity) {return get((FragmentActivity) activity);} else {assertNotDestroyed(activity);frameWaiter.registerSelf(activity);android.app.FragmentManager fm activity.getFragmentManager();//注意调用FragmentGet这里会生成一个Fragment 用来监听生命周期return fragmentGet(activity, fm, /* parentHint */ null, isActivityVisible(activity));}} 第7步 , fragmentGet 方法生成空白的Fragment该Fragment 是用来管理请求的生命周期的并且会和RequestManagerFactory工厂生成的RequestManager 绑定。 private RequestManager fragmentGet(NonNull Context context,NonNull android.app.FragmentManager fm,Nullable android.app.Fragment parentHint,boolean isParentVisible) {//1. 通过getRequestManagerFragment生成一个空白的Fragment用来管理请求的生命周期核心方法RequestManagerFragment current getRequestManagerFragment(fm, parentHint);RequestManager requestManager current.getRequestManager();//2. 如果FragmentManager为空就用RequestManagerFactory工厂生成一个并且设给RequestManagerFragmentif (requestManager null) {Glide glide Glide.get(context);requestManager factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);if (isParentVisible) {requestManager.onStart();}current.setRequestManager(requestManager);}//3. 返回RequestManagerreturn requestManager;} 第8步 在看工厂如何生成FragmentManager 之前先看看如何生成空白的Fragment——RequestManagerFragment因为这个RequestManagerFragment最终要和RequestManager 绑定的。 private RequestManagerFragment getRequestManagerFragment(NonNull final android.app.FragmentManager fm, Nullable android.app.Fragment parentHint) {//1. 先缓存中取RequestManagerFragment current pendingRequestManagerFragments.get(fm);//2.缓存里面没有再通过 TAG 从FragmentManager 里拿if (current null) {current (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);//3. 缓存没有通过TAG 也拿不到就初始化一个新的if (current null) {current new RequestManagerFragment();current.setParentFragmentHint(parentHint);//4. 先放入缓存以免下一次Glide请求会再生成一个空白的FragmentpendingRequestManagerFragments.put(fm, current);//5. 通过当前 Activity的 FragmentManager 开始提交添加一个 Fragment容器fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();//6. 这个空白的Fragment 添加到 FragmentManager 成功通过 Handler 发送一个消息清理缓存handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();}}return current;} 上面的 getRequestManagerFragment 有一个很重要的点采用了双重检测保证宿主、空白Fragment、RequestManager 一一对应 第一重保障使用Mapandroid.app.FragmentManager, RequestManagerFragment保存记录集合解决多个请求只有一个空白Fragment 第二重保障通过Handler 发送消息让空白的Fragment 马上添加到FragmentManager FragmentManager 的事务本来就是通过Handler 发送消息来实现通过Handler 发送消息有可能不会被马上执行倘若空白的Fragment的消息一直在等待那么下一次Glide请求就会再生成一个空白的Fragment。而通过Handler 发送删除消息可以让空白的Fragment添加到FragmentManager马上执行并且清空缓存这样下一次Glide 请求到来的时候从FragmentManager的TAG 就能拿到Fragment, 不会再创建。 第9步我们再回过头来看看怎样通过工厂RequestManagerFactory 来生成RequestManager的 private static final RequestManagerFactory DEFAULT_FACTORY new RequestManagerFactory() {NonNullOverridepublic RequestManager build(NonNull Glide glide,NonNull Lifecycle lifecycle,NonNull RequestManagerTreeNode requestManagerTreeNode,NonNull Context context) {//工厂里也是new 一个 RequestManager return new RequestManager(glide, lifecycle, requestManagerTreeNode, context);}}; 第10步RequestManager 初始化监听lifecycle的生命周期。而这个lifecycl就是空白Fragment里的ActivityFragmentLifecycle RequestManager(Glide glide,Lifecycle lifecycle,RequestManagerTreeNode treeNode,RequestTracker requestTracker,ConnectivityMonitorFactory factory,Context context) {this.glide glide;this.lifecycle lifecycle;this.treeNode treeNode;this.requestTracker requestTracker;this.context context;connectivityMonitor factory.build(context.getApplicationContext(),new RequestManagerConnectivityListener(requestTracker));glide.registerRequestManager(this);if (Util.isOnBackgroundThread()) {Util.postOnUiThread(addSelfToLifecycle);} else {lifecycle.addListener(this);//this 就是RequestManager 因此RequestManager就会监听Lifecycle生命周期}lifecycle.addListener(connectivityMonitor);defaultRequestListeners new CopyOnWriteArrayList(glide.getGlideContext().getDefaultRequestListeners());setRequestOptions(glide.getGlideContext().getDefaultRequestOptions());}//Requestmanager 已经监听了空白Fragment的lifecycleOverridepublic synchronized void onStart() {resumeRequests();//恢复请求targetTracker.onStart();}Overridepublic synchronized void onStop() {pauseRequests();//暂停请求targetTracker.onStop();} public class RequestManagerFragment extends Fragment {private static final String TAG RMFragment;private final ActivityFragmentLifecycle lifecycle;......Overridepublic void onStart() {super.onStart();lifecycle.onStart();//转发生命周期}Overridepublic void onStop() {super.onStop();lifecycle.onStop();//转发生命周期}Overridepublic void onDestroy() {super.onDestroy();lifecycle.onDestroy();unregisterFragmentWithRoot();//转发生命周期} } 到此可以看到 RequestManager 通过监听空白Fragment 的 lifecycle 的生命周期来管理请求的生命周期的。 Glide.with 原理总结 Glide 初始化并且生成RequestManagerRequestManager 与 空白Fragment 的 lifecycle 生命周期绑定来管理后续的请求。 1) 空白Fragment 持有ActivityFragmentLifecycle当空白Fragment生命周期改变的时候调用onStart()、onStop()、onDestroy()—— 2)ActivityFragmentLifecycle 持有LifecycleListener集合其中RequestManager是其中的一个LifecycleListenerActivityFragmentLifecycle遍历集合调用LifecycleListener的onStart()、onStop()、onDestroy()—— 3) RequestManager 实现了LifecycleListener 在onStart()、onStop()、onDestroy()方法中进行请求的生命周期管理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/932088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网站托管什么意思多媒体网页设计教程

原文地址:http://android.xsoftlab.net/training/displaying-bitmaps/process-bitmap.html 我们在上节课Load Large Bitmaps Efficiently中讨论了BitmapFactory.decode*方法,说到了不应该在UI线程中执行读取数据的过程,尤其是从磁盘或者网络…

vs 2017网站开发php网站开发类型什么意思

文章目录题目描述思路 && 代码二刷最近一直在充电基础知识、维护 leetcode 总结博客 今天继续摸一摸新题目吧~ 题目描述 感觉和 03. 无重复的最长子串 有点像,都是在字符串上用滑动窗口来找子串。并且都是属于那种,解决一次以后&…

海派虫网站推广软件wordpress跳转到登录页面代码

ES6是JavaScript的一个重要版本,其中包含了许多新的语法和特性,其中迭代器和生成器是其中非常重要的特性之一。本文将详细介绍迭代器和生成器的概念、用法以及注意事项。 目录 1. 是什么2. 为什么存在3. 怎么使用3.1 迭代器3.2 生成器3.3 for...of循环3…

昭通网站建设公司自己做的网站怎么放到网上去

图像像素存储形式  对于只有黑白颜色的灰度图,为单通道,一个像素块对应矩阵中一个数字,数值为0到255, 其中0表示最暗(黑色) ,255表示最亮(白色) 对于采用RGB模式的彩色图片&#…

国庆七日赛训总结

前五天跟高中生听noi金牌选手讲课,打的是noip模拟赛,均分150左右。 倒是学到了不少新算法,收获颇丰。 后两天在提高组上课,打s组模拟赛 分别取得190和170,其中两次的t2都是能对,却遇到了除以0re和数组开小的问题…

免费试用网站制作设计说明书范文

用来查询农历年的代码,可能比较粗糙,希望不要太打击偶,哈哈。一,Console.java[用来获取控制台的输入]/*#()Console.java 20:40:25 2/2/2006控制台消息输入/输出。*/import java.io.*;import java.text.SimpleDateFormat;import ja…

湘乡网站seo北京搜索引擎优化主管

入门效果之浮雕 "浮雕"图象效果是指图像的前景前向凸出背景。常见于一些纪念碑的雕刻上。要实现浮雕事实上很easy。我们把图象的一个象素和左上方的象素进行求差运算。并加上一个灰度。这个灰度就是表示背景颜色。这里我们设置这个插值为128 (图象RGB的值是0-255)。同…

task2.c

task2.cinclude <stdio.h> int main() { double a, b, c; scanf_s("%lf%lf%lf" , &a, &b, &c); if (a + b > c and a + c > b and b + c > a)printf("能构成三角形\n"…

还有网站吗建域名做网站

auto的实际价值就是简化代码&#xff0c;类型很长时可以自动推导。也可以用typedef&#xff0c;main函数里面也能typedef&#xff0c;不一定要在全局定义。 &#xff08;1&#xff09;类型别名思考 随着程序越来越复杂&#xff0c;程序中用到的类型也越来越复杂&#xff0c;经…

SpringCloud实用篇02-(Nacos配置管理,Feign远程调用,Gateway服务网关) - a

SpringCloud实用篇02-(Nacos配置管理,Feign远程调用,Gateway服务网关) 目录SpringCloud实用篇02-(Nacos配置管理,Feign远程调用,Gateway服务网关)0.学习目标1.Nacos配置管理1.1.统一配置管理1.1.1.在nacos中添…

网站备案后内容小米发布会完整版

CSS语法 1. CSS语法格式 通常情况下语法格式如下: 选择器{属性名:属性值;属性名:属性值;属性名:属性值;... }2. CSS添加方式 2.1 行内样式 直接将样式写在本行的标签内。 <h1><p style"font-size: 48px; color:red;";>行内样式测试</p></…

制作投票的网站青岛网络seo公司

Flutter Boost 是一个 Flutter 插件&#xff0c;它可以帮助开发者在原生应用和 Flutter 应用之间无缝跳转。以下是一些基本步骤&#xff0c;展示了如何使用 Flutter Boost 从原生&#xff08;Native&#xff09;页面跳转到 Flutter 页面。 ### 1. 配置 Flutter Boost 在你的 …

网站的背景图怎么做的vi设计都包括什么

题目描述 一个非递减有序的整型数组有n个元素&#xff0c;给定一个整数num&#xff0c;将num插入该序列的适当位置&#xff0c;使序列仍保持非递减有序。 要求定义一个函数insert()&#xff0c;将整数num插入在数组a的适当位置上&#xff0c;函数原型如下&#xff1a; void …

建设网站宣传页做平面素材比较好的网站

操作截图 在Jenkins里面设置通过标签进行构建 在Jenkins中进入项目&#xff0c;配置以下 将execute shell换到invoke top-level maven targets之前 在gitlab中配置标签 代码迭代新的版本 项目代码迭代 修改docker-compose.yml 提交新版本的代码 在Jenkins中追加新…

怎么删除织梦做的网站小城镇建设网站参考文献

自定义指令&#xff1a; vue中通过directive方法自定义指令&#xff0c;如&#xff1a;自定义一个v-focus指令&#xff1a; <script>Vue.directive(focus, {//通过directive(指令名,{配置})注册全局指令inserted: function (el) {//inserted:表示当自定义指令插入元素后…

夺宝网站制作能自己做头像的网站

Hello&#xff0c;everyone&#xff1a;11月20日早&#xff0c;星期二CSDN一分钟新闻时间&#xff1a;小米与美图战略合作&#xff0c;将获得美图手机品牌和影像技术等全球独家授权 小米要收购美图的节奏&#xff1f; …

百度生成在线网站地图宁夏网站建设哪个好

目录 1.1、错误描述 1.2、解决方案 1.1、错误描述 最近遇到一个jasper报表线上预览出现死循环的问题&#xff0c;实施人员反馈&#xff0c;线上生产环境中&#xff0c;使用某个功能显示pdf的时候&#xff0c;出现了接口超时问题&#xff0c;在这个项目中&#xff0c;我们使用…

举报的网站是国外的域名和空间优设计网站

“低-零功率”概念最先由美国国防部提出&#xff0c;主要是针对诸如俄罗斯等大国的远程传感器&#xff0c;帮助美军破除“灰色地带挑衅”的威胁。由于“灰色地带”冲突仅依托小规模军事力量&#xff0c;其强度维持在不足以引发美国及其盟国进行直接干预的程度&#xff0c;因此&…

总资料汇总关联化站点形式的尝试(未完成)

归档用 总资料汇总&关联化站点形式的尝试 在原先的文章当中,我曾经稍微归整了一些关于自己资料,这里引用下原文梳理 | 脑神经科学原理学习资料整理 posted @ 2025-09-23 15:04 tokepson 阅读(16) 评论(0)在这…

8051指令集

以下是按照你提供的指令列表整理的完整表格,补充了机器周期数(基于8051默认12T模式,即1个机器周期=12个时钟周期),表格按机器码(Hex Code)顺序排列:Hex Code Bytes Mnemonic Operands Machine Cycles00 1 NOP …