private DiskCacheConfig(Builder builder) { mVersion = builder.mVersion; mBaseDirectoryName = Preconditions.checkNotNull(builder.mBaseDirectoryName); mBaseDirectoryPathSupplier = Preconditions.checkNotNull(builder.mBaseDirectoryPathSupplier); mDefaultSizeLimit = builder.mMaxCacheSize; mLowDiskSpaceSizeLimit = builder.mMaxCacheSizeOnLowDiskSpace; mMinimumSizeLimit = builder.mMaxCacheSizeOnVeryLowDiskSpace; mEntryEvictionComparatorSupplier = Preconditions.checkNotNull(builder.mEntryEvictionComparatorSupplier); mCacheErrorLogger = builder.mCacheErrorLogger == null ? NoOpCacheErrorLogger.getInstance() : builder.mCacheErrorLogger; mCacheEventListener = builder.mCacheEventListener == null ? NoOpCacheEventListener.getInstance() : builder.mCacheEventListener; mDiskTrimmableRegistry = builder.mDiskTrimmableRegistry == null ? NoOpDiskTrimmableRegistry.getInstance() : builder.mDiskTrimmableRegistry; mContext = builder.mContext; mIndexPopulateAtStartupEnabled = builder.mIndexPopulateAtStartupEnabled; }
private DiskCacheConfig getDiskCacheConfig() { return DiskCacheConfig.newBuilder(this) .setBaseDirectoryPath(getDiskCacheDir(this)) .setBaseDirectoryName("ImagePipelineCacheDefault") .setMaxCacheSize(MAX_DISK_CACHE_SIZE) .setMaxCacheSizeOnLowDiskSpace(MAX_DISK_CACHE_LOW_SIZE) .setMaxCacheSizeOnVeryLowDiskSpace(MAX_DISK_CACHE_VERYLOW_SIZE) .setDiskTrimmableRegistry(NoOpDiskTrimmableRegistry.getInstance()) .build(); }
public static ImagePipelineConfig getDefaultImagePipelineConfig(Context context) { final int cacheSize = (int) SharedPreferencesUtil.getData(HViewerApplication.mContext, SettingFragment.KEY_PREF_CACHE_SIZE, 300); MAX_DISK_CACHE_VERYLOW_SIZE = cacheSize / 5 * ByteConstants.MB; MAX_DISK_CACHE_LOW_SIZE = cacheSize * 3 / 5 * ByteConstants.MB; MAX_DISK_CACHE_SIZE = cacheSize * ByteConstants.MB; //内存配置 final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams( MAX_MEMORY_CACHE_SIZE, // 内存缓存中总图片的最大大小,以字节为单位。 Integer.MAX_VALUE, // 内存缓存中图片的最大数量。 MAX_MEMORY_CACHE_SIZE, // 内存缓存中准备清除但尚未被删除的总图片的最大大小,以字节为单位。 Integer.MAX_VALUE, // 内存缓存中准备清除的总图片的最大数量。 Integer.MAX_VALUE); // 内存缓存中单个图片的最大大小。 //修改内存图片缓存数量,空间策略(这个方式有点恶心) Supplier<MemoryCacheParams> mSupplierMemoryCacheParams = new Supplier<MemoryCacheParams>() { @Override public MemoryCacheParams get() { return bitmapCacheParams; } }; //小图片的磁盘配置 DiskCacheConfig diskSmallCacheConfig = DiskCacheConfig.newBuilder(context) .setBaseDirectoryPath(getDiskCacheDir(context)) //缓存图片基路径 .setBaseDirectoryName(IMAGE_PIPELINE_SMALL_CACHE_DIR) //文件夹名 .setMaxCacheSize(MAX_DISK_CACHE_SIZE) //默认缓存的最大大小。 .setMaxCacheSizeOnLowDiskSpace(MAX_SMALL_DISK_LOW_CACHE_SIZE) //缓存的最大大小,使用设备时低磁盘空间。 .setMaxCacheSizeOnVeryLowDiskSpace(MAX_SMALL_DISK_VERYLOW_CACHE_SIZE) //缓存的最大大小,当设备极低磁盘空间 .setDiskTrimmableRegistry(NoOpDiskTrimmableRegistry.getInstance()) .build(); //默认图片的磁盘配置 DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(context) .setBaseDirectoryPath(getDiskCacheDir(context))//缓存图片基路径 .setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR) //文件夹名 .setMaxCacheSize(MAX_DISK_CACHE_SIZE) //默认缓存的最大大小。 .setMaxCacheSizeOnLowDiskSpace(MAX_DISK_CACHE_LOW_SIZE) //缓存的最大大小,使用设备时低磁盘空间。 .setMaxCacheSizeOnVeryLowDiskSpace(MAX_DISK_CACHE_VERYLOW_SIZE) //缓存的最大大小,当设备极低磁盘空间 .setDiskTrimmableRegistry(NoOpDiskTrimmableRegistry.getInstance()) .build(); // 自定义使用okhttp进行请求 OkHttpClient okHttpClient = new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new HttpDns()) .build(); //缓存图片配置 ImagePipelineConfig.Builder configBuilder = ImagePipelineConfig.newBuilder(context) .setBitmapsConfig(Bitmap.Config.RGB_565) .setBitmapMemoryCacheParamsSupplier(mSupplierMemoryCacheParams) .setSmallImageDiskCacheConfig(diskSmallCacheConfig) .setMainDiskCacheConfig(diskCacheConfig) .setMemoryTrimmableRegistry(NoOpMemoryTrimmableRegistry.getInstance()) .setResizeAndRotateEnabledForNetwork(true) .setNetworkFetcher(new MyOkHttpNetworkFetcher(okHttpClient)); // 这段代码用于清理缓存 NoOpMemoryTrimmableRegistry.getInstance().registerMemoryTrimmable(new MemoryTrimmable() { @Override public void trim(MemoryTrimType trimType) { final double suggestedTrimRatio = trimType.getSuggestedTrimRatio(); Logger.d("ImagePipeline", String.format("onCreate suggestedTrimRatio : %d", suggestedTrimRatio)); if (MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio() == suggestedTrimRatio || MemoryTrimType.OnSystemLowMemoryWhileAppInBackground.getSuggestedTrimRatio() == suggestedTrimRatio || MemoryTrimType.OnSystemLowMemoryWhileAppInForeground.getSuggestedTrimRatio() == suggestedTrimRatio ) { ImagePipelineFactory.getInstance().getImagePipeline().clearMemoryCaches(); } } }); return configBuilder.build(); }