Glide Github 地址:https://github.com/bumptech/glide
参考地址:Android 图片加载(四)Glide自定义模块
参考:https://github.com/JessYanCoding/MVPArms
AppGlideModule 是glide v4 以上添加的abstract的类,作用是设置glide的初始化设置。
转自:Glide通过清单文件加载GlideModule的原理
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//..............
}
@Override
public void registerComponents(Context context, Glide glide) {
//............
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest ......>
......
<application >
<meta-data
android:name="XXX.XXX.XXX.MyGlideModule"
android:value="GlideModule" />
</application>
混淆:
-keep public class extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
参考:https://github.com/JessYanCoding/MVPArms
TODO 不太懂啊,懂了再记录,暂时先记这些。
.setMemoryCache(MemoryCache memoryCache)
.setBitmapPool(BitmapPool bitmapPool)
.setDiskCache(DiskCache.Factory diskCacheFactory)
.setDiskCacheService(ExecutorService service)
.setResizeService(ExecutorService service)
.setDecodeFormat(DecodeFormat decodeFormat)
磁盘缓存配置(默认缓存大小250M,默认保存在内部存储中)
设置磁盘缓存保存在外部存储,且指定缓存大小:
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, diskCacheSize);
设置磁盘缓存保存在自己指定的目录下,且指定缓存大小
builder.setDiskCache(new DiskLruCacheFactory(new DiskLruCacheFactory.CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
return diskCacheFolder;
}
}, diskCacheSize);
设置内存缓存大小
builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
设置Bitmap池大小
builder.setBitmapPool(new LruBitmapPool(bitmapPoolSize));
Glide默认使用低质量的RGB565,如果想使用高质量
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888)
Glide 默认使用 HttpURLConnection 做网络请求,在这切换成 Okhttp 请求
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
但是当你从一个使用HTTPS,还是self-signed的服务器下载图片时,Glide并不会下载或者显示图片,因为self-signed认证会被认为存在安全问题
所以,https://www.jianshu.com/p/b78b219ff367
/**
* ================================================
* {@link AppGlideModule} 的默认实现类
* 用于配置缓存文件夹,切换图片请求框架等操作
*
* Created by JessYan on 16/4/15.
* Contact me
* Follow me
* ================================================
*/
@GlideModule(glideName = "GlideArms")
public class GlideConfiguration extends AppGlideModule {
public static final int IMAGE_DISK_CACHE_MAX_SIZE = 100 * 1024 * 1024;//图片缓存文件最大值为100Mb
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
LogUtils.debugInfo("GlideConfiguration applyOptions");
final AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
builder.setDiskCache(() -> {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.create(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE);
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
....
}
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
//Glide 默认使用 HttpURLConnection 做网络请求,在这切换成 Okhttp 请求
LogUtils.debugInfo("GlideConfiguration registerComponents");
AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(appComponent.okHttpClient()));
....
}
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}