Fresco源码解析 - 创建一个ImagePipeline(一)
在Fresco源码解析 - 初始化过程分析章节中,
我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类。
ImagePipelineFactory.initialize(context);
会创建一个所有参数都使用默认值的ImagePipelineConfig来初始化ImagePipeline。
ImagePipelineFactory.initialize(imagePipelineConfig)会首先用 imagePipelineConfig创建一个ImagePipelineFactory的实例 - sInstance。
sInstance = new ImagePipelineFactory(imagePipelineConfig);
然后,初始化Drawee时,在PipelineDraweeControllerBuilderSupplier的构造方法中通过 ImagePipelineFactory.getInstance()获取这个实例。
Fresco.java
private static void initializeDrawee(Context context) {
sDraweeControllerBuilderSupplier = new PipelineDraweeControllerBuilderSupplier(context);
SimpleDraweeView.initialize(sDraweeControllerBuilderSupplier);
}
PipelineDraweeControllerBuilderSupplier.java public PipelineDraweeControllerBuilderSupplier(Context context) {
this(context, ImagePipelineFactory.getInstance());
} public PipelineDraweeControllerBuilderSupplier(
Context context,
ImagePipelineFactory imagePipelineFactory) {
this(context, imagePipelineFactory, null);
}
PipelineDraweeControllerBuilderSupplier还有一个构造方法,就是 this(context, imagePipelineFactory, null)调用的构造方法。
public PipelineDraweeControllerBuilderSupplier(
Context context,
ImagePipelineFactory imagePipelineFactory,
Set<ControllerListener> boundControllerListeners) {
mContext = context;
mImagePipeline = imagePipelineFactory.getImagePipeline();
mPipelineDraweeControllerFactory = new PipelineDraweeControllerFactory(
context.getResources(),
DeferredReleaser.getInstance(),
imagePipelineFactory.getAnimatedDrawableFactory(),
UiThreadImmediateExecutorService.getInstance());
mBoundControllerListeners = boundControllerListeners;
}
其中,mImagePipeline = imagePipelineFactory.getImagePipeline()用于获取ImagePipeline的实例。
ImagePipelineFactory.java
public ImagePipeline getImagePipeline() {
if (mImagePipeline == null) {
mImagePipeline =
new ImagePipeline(
getProducerSequenceFactory(),
mConfig.getRequestListeners(),
mConfig.getIsPrefetchEnabledSupplier(),
getBitmapMemoryCache(),
getEncodedMemoryCache(),
mConfig.getCacheKeyFactory());
}
return mImagePipeline;
}
可以看出mImagePipeline是一个单例,构造ImagePipeline时用到的mConfig就是本片最开始讲到的 ImagePipelineConfig imagePipelineConfig。
经过这个过程,一个ImagePipeline就被创建好了,下面我们具体解析一下ImagePipeline的每个参数。
因为ImagePipelineFactory用ImagePipelineConfig来创建一个ImagePipeline,我们首先分析一下ImagePipelineConfig的源码。
public class ImagePipelineConfig {
private final Supplier<MemoryCacheParams> mBitmapMemoryCacheParamsSupplier;
private final CacheKeyFactory mCacheKeyFactory;
private final Context mContext;
private final Supplier<MemoryCacheParams> mEncodedMemoryCacheParamsSupplier;
private final ExecutorSupplier mExecutorSupplier;
private final ImageCacheStatsTracker mImageCacheStatsTracker;
private final AnimatedDrawableUtil mAnimatedDrawableUtil;
private final AnimatedImageFactory mAnimatedImageFactory;
private final ImageDecoder mImageDecoder;
private final Supplier<Boolean> mIsPrefetchEnabledSupplier;
private final DiskCacheConfig mMainDiskCacheConfig;
private final MemoryTrimmableRegistry mMemoryTrimmableRegistry;
private final NetworkFetcher mNetworkFetcher;
private final PoolFactory mPoolFactory;
private final ProgressiveJpegConfig mProgressiveJpegConfig;
private final Set<RequestListener> mRequestListeners;
private final boolean mResizeAndRotateEnabledForNetwork;
private final DiskCacheConfig mSmallImageDiskCacheConfig;
private final PlatformBitmapFactory mPlatformBitmapFactory; // other methods
}
上图可以看出,获取图像的第一站是Memeory Cache,然后是Disk Cache,最后是Network,而Memory和Disk都是缓存在本地的数据,MemoryCacheParams就用于表示它们的缓存策略。
MemoryCacheParams.java
/**
* Pass arguments to control the cache's behavior in the constructor.
*
* @param maxCacheSize The maximum size of the cache, in bytes.
* @param maxCacheEntries The maximum number of items that can live in the cache.
* @param maxEvictionQueueSize The eviction queue is an area of memory that stores items ready
* for eviction but have not yet been deleted. This is the maximum
* size of that queue in bytes.
* @param maxEvictionQueueEntries The maximum number of entries in the eviction queue.
* @param maxCacheEntrySize The maximum size of a single cache entry.
*/
public MemoryCacheParams(
int maxCacheSize,
int maxCacheEntries,
int maxEvictionQueueSize,
int maxEvictionQueueEntries,
int maxCacheEntrySize) {
this.maxCacheSize = maxCacheSize;
this.maxCacheEntries = maxCacheEntries;
this.maxEvictionQueueSize = maxEvictionQueueSize;
this.maxEvictionQueueEntries = maxEvictionQueueEntries;
this.maxCacheEntrySize = maxCacheEntrySize;
}
关于每个参数的作用,注释已经写得很清楚,不再赘述。
CacheKeyFactory会为ImageRequest创建一个索引 - CacheKey。
/**
* Factory methods for creating cache keys for the pipeline.
*/
public interface CacheKeyFactory { /**
* @return {@link CacheKey} for doing bitmap cache lookups in the pipeline.
*/
public CacheKey getBitmapCacheKey(ImageRequest request); /**
* @return {@link CacheKey} for doing encoded image lookups in the pipeline.
*/
public CacheKey getEncodedCacheKey(ImageRequest request); /**
* @return a {@link String} that unambiguously indicates the source of the image.
*/
public Uri getCacheKeySourceUri(Uri sourceUri);
}
ExecutorSupplier会根据ImagePipeline的使用场景获取不同的Executor。
public interface ExecutorSupplier { /** Executor used to do all disk reads, whether for disk cache or local files. */
Executor forLocalStorageRead(); /** Executor used to do all disk writes, whether for disk cache or local files. */
Executor forLocalStorageWrite(); /** Executor used for all decodes. */
Executor forDecode(); /** Executor used for all image transformations, such as transcoding, resizing, and rotating. */
Executor forTransform(); /** Executor used for background operations, such as postprocessing. */
Executor forBackground();
}
ImageCacheStatsTracker 作为 Cache 埋点工具,可以统计Cache的各种操作数据。
public interface ImageCacheStatsTracker { /** Called whenever decoded images are put into the bitmap cache. */
public void onBitmapCachePut(); /** Called on a bitmap cache hit. */
public void onBitmapCacheHit(); /** Called on a bitmap cache miss. */
public void onBitmapCacheMiss(); /** Called whenever encoded images are put into the encoded memory cache. */
public void onMemoryCachePut(); /** Called on an encoded memory cache hit. */
public void onMemoryCacheHit(); /** Called on an encoded memory cache hit. */
public void onMemoryCacheMiss(); /**
* Called on an staging area hit.
*
* <p>The staging area stores encoded images. It gets the images before they are written
* to disk cache.
*/
public void onStagingAreaHit(); /** Called on a staging area miss hit. */
public void onStagingAreaMiss(); /** Called on a disk cache hit. */
public void onDiskCacheHit(); /** Called on a disk cache miss. */
public void onDiskCacheMiss(); /** Called if an exception is thrown on a disk cache read. */
public void onDiskCacheGetFail(); /**
* Registers a bitmap cache with this tracker.
*
* <p>Use this method if you need access to the cache itself to compile your stats.
*/
public void registerBitmapMemoryCache(CountingMemoryCache<?, ?> bitmapMemoryCache); /**
* Registers an encoded memory cache with this tracker.
*
* <p>Use this method if you need access to the cache itself to compile your stats.
*/
public void registerEncodedMemoryCache(CountingMemoryCache<?, ?> encodedMemoryCache);
}
剩下的几个参数与Drawable关联比较大,我们下一篇再分析。
Fresco源码解析 - 创建一个ImagePipeline(一)的更多相关文章
- Fresco源码解析 - DataSource怎样存储数据
Fresco源码解析 - DataSource怎样存储数据 datasource是一个独立的 package,与FB导入的guava包都在同一个工程内 - fbcore. datasource的类关系 ...
- [Java多线程]-线程池的基本使用和部分源码解析(创建,执行原理)
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 多线 ...
- React源码解析——创建更新过程
一.ReactDOM.render 创建ReactRoot,并且根据情况调用root.legacy_renderSubtreeIntoContainer或者root.render,前者是遗留的 API ...
- SpringBoot源码解析:创建SpringApplication对象实例
上篇文章SpringBoot自动装配原理解析中,我们分析了SpringBoot的自动装配原理以及@SpringBootApplication注解的原理,本篇文章则继续基于上篇文章中的main方法来分析 ...
- Fresco 源码分析(序)
1. 为什么要写这个分析的博客 其实关于Fresco的相关内容,大家上网搜索,一般可以找到一大推,但是为什么我还要写关于这个的呢,因为在网上搜索中文和英文的关于fresco的相关知识时,大家只是潜在的 ...
- Fresco 源码分析 —— 整体架构
Fresco 是我们项目中图片加载专用框架.虽然我不是负责 Fresco 框架,但是由本人负责组里的图片加载浏览等工作,因此了解 Fresco 的源码有助于我今后的工作,也可以学习 Fresco 的源 ...
- Android 开源项目源码解析(第二期)
Android 开源项目源码解析(第二期) 阅读目录 android-Ultra-Pull-To-Refresh 源码解析 DynamicLoadApk 源码解析 NineOldAnimations ...
- Spring源码解析——循环依赖的解决方案
一.前言 承接<Spring源码解析--创建bean>.<Spring源码解析--创建bean的实例>,我们今天接着聊聊,循环依赖的解决方案,即创建bean的ObjectFac ...
- SpringBoot源码解析系列文章汇总
相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的SpringBoot源码解析系列文章的汇总,当你使用SpringBoot不仅仅满足于基本使用时.或者出去面试被面试官虐了时.或者说想要深入了解一下 ...
随机推荐
- Bulk Insert & BCP执行效率对比
我们以BCP导出的CSV数据文件,分别使用Bulk insert与BCP导入数据库,对比两种方法执行效率 备注:导入目标表创建了分区聚集索引 1.BCP导出csv数据文件 数据量:15000000行, ...
- 谈谈.NET中常见的内存泄露问题——GC、委托事件和弱引用
其实吧,内存泄露一直是个令人头疼的问题,在带有GC的语言中这个情况得到了很大的好转,但是仍然可能会有问题.一.什么是内存泄露(memory leak)?内存泄露不是指内存坏了,也不是指内存没插稳漏出来 ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- JS之Array.slice()方法
1.Array.slice(startIndex,endIndex); 返回由原始数组从startIndex到endIndex-1的元素构成的新数组; startIndex:默认值0,如果startI ...
- 终于有人把P2P、P2C、O2O、B2C、B2B、C2C 的区别讲透了
http://news.mbalib.com/story/88506 P2P.P2C .O2O .B2C.B2B. C2C,每天看着这些常见又陌生的名词,如果有人跟你说让你解释它的含义,金融的小伙伴们 ...
- json对象与json字符串互换
json字符串转json对象:jQuery.parseJSON(jsonStr); / var jsonObj= eval('(' + str + ...
- CFBundleVersion与CFBundleShortVersionString
CFBundleVersion,标识(发布或未发布)的内部版本号.这是一个单调增加的字符串,包括一个或多个时期分隔的整数. CFBundleShortVersionString 标识应用程序的发布版 ...
- Java Convert String & Int
To convert a int to string: int num = 123; String str = String.valueOf(num); To convert a string to ...
- Oracle 11G INDEX FULL SCAN 和 INDEX FAST FULL SCAN 对比分析
SQL> drop table test; 表已删除. SQL> create table test as select * from dba_objects where 1!=1; 表已 ...
- 30天,APP创业从0到1【7.11郑州站】
活动概况 时间:2015年07月11日13:30-16:30 地点:慧谷咖啡(郑州杨金路9号河南外包产业园天元W栋) 主办:APICloud.环信.河南中医药大学医药信息化实验室 网址:www.api ...