创建一个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

(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的源码。

{ 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; 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; }

关于每个参数的作用,注释已经写得很清楚,不再赘述。

放下一处烦恼,收获一个惊喜;放下一种偏见,收获一种幸福;

创建一个ImagePipeline(一)

相关文章:

你感兴趣的文章:

标签云: