我們今天來了解下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中淘汰下來的圖片,但是近期可能會用到的,暫時存放在這里。
- 驅逐隊列的數目
- 單個緩存條目的最大大小
以上純屬個人意見,如有錯誤,請及時更正。
- 前言
- Android四大圖片緩存框架之-Fresco(一)
- Android四大圖片緩存框架之-Fresco之initialize(二)
- Android 四大緩存框架之-Universal-Image-Loader
- Android四大圖片緩存框架之-Picasso和Glide
- Android ORM數據庫框架之-greenDao(一)
- Android ORM數據庫框架之-greenDao(二)
- Android ORM數據庫框架之-greenDao(三)
- Android ORM數據庫框架之-greenDao(四)
- Android 網絡開源庫之-retrofit
- RxJava的簡單學習(學習自扔物線)
- Android ORM框架之-ActiveAndroid的簡單分析