💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
我们今天来了解下Fresco的初始化过程。 以系统默认的初始化参数为例。 ~~~ Fresco.initialize(getApplicationContext()); ~~~ 进入到 com.facebook.drawee.backends.pipeline.Fresco中 ~~~ public static void initialize(Context context) { ImagePipelineFactory.initialize(context); initializeDrawee(context); } ~~~ 我们先看ImagePipelineFactory.initialize(context);方法 ~~~ public static void initialize(Context context) { initialize(ImagePipelineConfig.newBuilder(context).build()); } ~~~ 我们继续看ImagePipelineConfig.newBuilder(context)方法, ~~~ public static ImagePipelineConfig.Builder newBuilder(Context context) { return new ImagePipelineConfig.Builder(context, null); } ~~~ 这里调用了ImagePipelineCongif的一个内部类Builder。 ~~~ private Builder(Context context) { this.mDownsampleEnabled = false; this.mResizeAndRotateEnabledForNetwork = true; this.mContext = (Context)Preconditions.checkNotNull(context); } ~~~ 这里只初始化了3个参数。其他的一些相关的参数并没有在这里进行初始化,那么,其他的一些设置是怎么实现的呢。别着急,我们接下来看 ImagePipelineConfig.newBuilder(context).build()的build()方法。 ~~~ public ImagePipelineConfig build() { return new ImagePipelineConfig(this, null); } ~~~ 这里返回了ImagePipelineConfig的一个实例。我们看下他的构造函数吧。 ~~~ private ImagePipelineConfig(ImagePipelineConfig.Builder builder) { this.mAnimatedImageFactory = builder.mAnimatedImageFactory; this.mBitmapMemoryCacheParamsSupplier = (Supplier)(builder.mBitmapMemoryCacheParamsSupplier == null?new DefaultBitmapMemoryCacheParamsSupplier((ActivityManager)builder.mContext.getSystemService("activity")):builder.mBitmapMemoryCacheParamsSupplier); this.mCacheKeyFactory = (CacheKeyFactory)(builder.mCacheKeyFactory == null?DefaultCacheKeyFactory.getInstance():builder.mCacheKeyFactory); this.mContext = (Context)Preconditions.checkNotNull(builder.mContext); this.mDownsampleEnabled = builder.mDownsampleEnabled; this.mEncodedMemoryCacheParamsSupplier = (Supplier)(builder.mEncodedMemoryCacheParamsSupplier == null?new DefaultEncodedMemoryCacheParamsSupplier():builder.mEncodedMemoryCacheParamsSupplier); this.mImageCacheStatsTracker = (ImageCacheStatsTracker)(builder.mImageCacheStatsTracker == null?NoOpImageCacheStatsTracker.getInstance():builder.mImageCacheStatsTracker); this.mImageDecoder = builder.mImageDecoder; this.mIsPrefetchEnabledSupplier = builder.mIsPrefetchEnabledSupplier == null?new Supplier() { public Boolean get() { return Boolean.valueOf(true); } }:builder.mIsPrefetchEnabledSupplier; this.mMainDiskCacheConfig = builder.mMainDiskCacheConfig == null?getDefaultMainDiskCacheConfig(builder.mContext):builder.mMainDiskCacheConfig; this.mMemoryTrimmableRegistry = (MemoryTrimmableRegistry)(builder.mMemoryTrimmableRegistry == null?NoOpMemoryTrimmableRegistry.getInstance():builder.mMemoryTrimmableRegistry); this.mNetworkFetcher = (NetworkFetcher)(builder.mNetworkFetcher == null?new HttpUrlConnectionNetworkFetcher():builder.mNetworkFetcher); this.mPlatformBitmapFactory = builder.mPlatformBitmapFactory; this.mPoolFactory = builder.mPoolFactory == null?new PoolFactory(PoolConfig.newBuilder().build()):builder.mPoolFactory; this.mProgressiveJpegConfig = (ProgressiveJpegConfig)(builder.mProgressiveJpegConfig == null?new SimpleProgressiveJpegConfig():builder.mProgressiveJpegConfig); this.mRequestListeners = (Set)(builder.mRequestListeners == null?new HashSet():builder.mRequestListeners); this.mResizeAndRotateEnabledForNetwork = builder.mResizeAndRotateEnabledForNetwork; this.mSmallImageDiskCacheConfig = builder.mSmallImageDiskCacheConfig == null?this.mMainDiskCacheConfig:builder.mSmallImageDiskCacheConfig; int decodeThreads = this.mPoolFactory.getFlexByteArrayPoolMaxNumThreads(); this.mExecutorSupplier = (ExecutorSupplier)(builder.mExecutorSupplier == null?new DefaultExecutorSupplier():builder.mExecutorSupplier); } ~~~ 看到这里,就明白了,构造函数中根据判断ImagePipelineConfig.Builder对象的成员变量是否为空来初始化,不为空,则用我们设置好的,为空,那么就用系统的。我们以mBitmapMemoryCacheParamsSupplier为例。 ~~~ public class DefaultBitmapMemoryCacheParamsSupplier implements Supplier<MemoryCacheParams> { private static final int MAX_CACHE_ENTRIES = 256; private static final int MAX_EVICTION_QUEUE_SIZE = 2147483647; private static final int MAX_EVICTION_QUEUE_ENTRIES = 2147483647; private static final int MAX_CACHE_ENTRY_SIZE = 2147483647; private final ActivityManager mActivityManager; public DefaultBitmapMemoryCacheParamsSupplier(ActivityManager activityManager) { this.mActivityManager = activityManager; } public MemoryCacheParams get() { return new MemoryCacheParams(this.getMaxCacheSize(), 256, 2147483647, 2147483647, 2147483647); } private int getMaxCacheSize() { int maxMemory = Math.min(this.mActivityManager.getMemoryClass() * 1048576, 2147483647); return maxMemory < 33554432?4194304:(maxMemory < 67108864?6291456:(VERSION.SDK_INT <= 9?8388608:maxMemory / 4)); } } ~~~ 看到这些敏感的数字就知道,这里就是配置LruCache大小的地方了。(猜测)。我想,这里会在某个地方调用get方法来获取系统设置的参数。接着看下这些参数的意思。 ~~~ 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; } ~~~ - 内存缓存的最大Size - 缓存的最大条目,应该就是缓存图片的最大数目 - 驱逐队列的Size,(以下是我猜测的内容,有待验证),驱逐队列指的是重Lrucache中淘汰下来的图片,但是近期可能会用到的,暂时存放在这里。 - 驱逐队列的数目 - 单个缓存条目的最大大小 以上纯属个人意见,如有错误,请及时更正。