1.导入依赖
implementation 'com.facebook.fresco:fresco:1.12.1'
2.清单文件进行初始化
3.创建一个类继承Application
然后在ocreate中写我们的缓存路径
// 高级初始化:
Fresco.initialize(this, ImagePipelineConfig.newBuilder(App.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
//磁盘缓存路径
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsolutePath())) // 注意Android运行时权限。
.setMaxCacheSize(10 * 1024 * 1024)
.build()
)
.build()
);
4.设置缓存大小
private static int MAX_MEM = 30 * ByteConstants.MB;
5.设置内存
@RequiresApi(api = Build.VERSION_CODES.N)
private ImagePipelineConfig getConfigureCaches(Context context) {
final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams(
MAX_MEM,// 内存缓存中总图片的最大大小,以字节为单位。
Integer.MAX_VALUE,// 内存缓存中图片的最大数量。
MAX_MEM,// 内存缓存中准备清除但尚未被删除的总图片的最大大小,以字节为单位。
Integer.MAX_VALUE,// 内存缓存中准备清除的总图片的最大数量。
Integer.MAX_VALUE);// 内存缓存中单个图片的最大大小。
Supplier<MemoryCacheParams> mSupplierMemoryCacheParams = new Supplier<MemoryCacheParams>() {
@Override
public MemoryCacheParams get() {
return bitmapCacheParams;
}
};
ImagePipelineConfig.Builder builder = ImagePipelineConfig.newBuilder(context);
builder.setBitmapMemoryCacheParamsSupplier((com.facebook.common.internal.Supplier<MemoryCacheParams>) mSupplierMemoryCacheParams);
return builder.build();
}