参考文章
Glide 4.x添加自定义组件原理
Glide 系列-1:预热、Glide 的常用配置方式及其原理
Glide 4.0 详解
问题描述
在加载图片的时候,经常会出现连接超时的现象发生,导致无法加载出图片
Glide 3.5.2、okhttp3 版本
com.github.bumptech.glide:glide:3.5.2
com.squareup.okhttp3:okhttp:3.5.0
解决办法
implementation 'com.github.bumptech.glide:okhttp3-integration:1.5.0'
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build();
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
//注册使用Okhttp发起网络访问
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
<application
...>
<meta-data
android:name="com.xxxxxx.MyGlideModule"
android:value="GlideModule" />
</applicaton>
Glide 4.9.0、okhttp3 版本
implementation 'com.squareup.okhttp3:okhttp:4.0.1'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.github.bumptech.glide:annotations:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.9.0'
特别说明:glide:compiler 需要在主model里面,否则会扫描不到注解
@GlideModule
public class FlickrGlideModule extends AppGlideModule {
@Override
public void applyOptions( Context context, GlideBuilder builder) {
super.applyOptions(context, builder);
}
@Override
public void registerComponents(Context context, Glide glide,Registry registry) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}
// 关闭扫描清单文件
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}
防止混淆
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keep public class extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
一个项目里面只能有一个AppGlideModule,或者很多的LibraryGlideModule
网友评论