目录
10 Glide初始化配置
10.1 初始化配置开发步骤
- 添加Glide注解依赖包
implementation 'com.github.bumptech.glide:compiler:4.8.0' annotationProcessor 'com.github.bumptech.glide:annotations:4.8.0'
- 添加一个AppGlideModule的实现类GlideConfigModule
- 实现类添加@GlideModule
@GlideModule public class GlideConfigModule extends AppGlideModule { }
- registerComponents()实现定制组件操作
包括:添加、替换@Override public void registerComponents(Context context, Glide glide, Registry registry) { registry.append(Photo.class, InputStream.class, new CustomModelLoader.Factory()); }
- applyOptions() 添加配置初始化
- proguard.cfg 添加Keep AppGlideModule的实现:
-keep public class extends com.bumptech.glide.module.AppGlideModule -keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
10.2 内存缓存配置
方法一:使用系统默认的内存大小
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).setMemoryCacheScreens(2).build();
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
方法二:自定义内存缓存为20MB
builder.setMemoryCache(new LruResourceCache(20*1024*1024));
10.3 Bitmap池配置
自定义Bitmap池缓存为30MB
int bitmapPoolSizeBytes = 1024 * 1024 * 30; // 30mb
builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
10.4 磁盘缓存
设置磁盘缓存100MB
int diskCacheSizeBytes = 100*1024 *1024 ;
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
10.4 图片压缩质量
图片压缩质量设置为RGB_565:
builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)
.disallowHardwareConfig());
10.5 日志级别配置
//日志级别配置
builder.setLogLevel(Log.DEBUG);