Java 类com.facebook.imagepipeline.cache.DefaultBitmapMemoryCacheParamsSupplier 实例源码

项目:JianshuApp    文件:FrescoManager.java   
public static void init(Context context, File baseDirectoryPath) {
    ImagePipelineConfig.Builder imagePipelineConfigBuilder = ImagePipelineConfig.newBuilder(context)
            .setMainDiskCacheConfig(DiskCacheConfig.newBuilder(context)
                    .setBaseDirectoryPath(baseDirectoryPath)
                    .setBaseDirectoryName("original")
                    .build())
            .setDownsampleEnabled(true);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    Supplier<MemoryCacheParams> memoryCacheParamsSupplier = new DefaultBitmapMemoryCacheParamsSupplier(activityManager) {
        @Override
        public MemoryCacheParams get() {
            int maxCacheEntries = 256;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                maxCacheEntries = 64;
            }
            return new MemoryCacheParams(
                    getMaxCacheSize(),
                    maxCacheEntries,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE);
        }
        private int getMaxCacheSize() {
            final int maxMemory = Math.min(activityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);

            if (maxMemory < 32 * ByteConstants.MB) {
                return 4 * ByteConstants.MB;
            } else if (maxMemory < 64 * ByteConstants.MB) {
                return 6 * ByteConstants.MB;
            } else {
                return maxMemory / 4;
            }
        }
    };
    imagePipelineConfigBuilder.setBitmapMemoryCacheParamsSupplier(memoryCacheParamsSupplier);
    Fresco.initialize(context, imagePipelineConfigBuilder.build());
}