initFresco
private void initFresco() { DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this). setMaxCacheSize(128 * ByteConstants.MB).build(); Supplier<MemoryCacheParams> bitmapCacheParamsSupplier = () -> new MemoryCacheParams( // 可用最大内存数,以字节为单位 24 * ByteConstants.MB // 内存中允许的最多图片数量 , 128 // 内存中准备清理但是尚未删除的总图片所可用的最大内存数,以字节为单位 , 16 * ByteConstants.MB // 内存中准备清除的图片最大数量 , 32 , 4 * ByteConstants.MB); // 当内存紧张时采取的措施 MemoryTrimmableRegistry registry = NoOpMemoryTrimmableRegistry.getInstance(); registry.registerMemoryTrimmable(trimType -> { double str = trimType.getSuggestedTrimRatio(); if (MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio() == str || MemoryTrimType.OnSystemLowMemoryWhileAppInBackground.getSuggestedTrimRatio() == str || MemoryTrimType.OnSystemLowMemoryWhileAppInForeground.getSuggestedTrimRatio() == str) { // 清除内存缓存 Fresco.getImagePipeline().clearMemoryCaches(); } }); ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this) .setBitmapMemoryCacheParamsSupplier(bitmapCacheParamsSupplier) .setMainDiskCacheConfig(diskCacheConfig) .setDownsampleEnabled(true) .setBitmapsConfig(Bitmap.Config.RGB_565) .setMemoryTrimmableRegistry(registry) .setResizeAndRotateEnabledForNetwork(true) .build(); Fresco.initialize(this, config); }