Glide加载图片变绿
原因,Glide默认加载图片的格式是DecodeFormat.PREFER_RGB_565
,缺少ALPHA通道,导致加载图片变绿。
解决方案
Glide.setup(new GlideBuilder(context).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888));
1
Glide在弱网状态下加载大图片,失败几率很大。
解决方案,配置自己的网络栈,
前提:我用的是Retrofit2,其内部使用的是OkHttp3.
配置
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
1
自定义GlideModule
public class MyGlideModule implements GlideModule {
@Override
public void registerComponents(Context context, Glide glide) {
// 设置长时间读取和断线重连
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.MINUTES).readTimeout(10, TimeUnit.MINUTES).retryOnConnectionFailure(true).build();
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 防止图片变绿,在有ALPHA通道的情况下
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
}
manifest的Application标签配置
<application>
<meta-data
android:name="com.example.admin.quwang.http.MyGlideModule"
android:value="GlideModule"
>
</application>
1
2
3
4
5
6
在弱网状态下即可解决图片加载问题
Glide的OOM
ImageView设置的ScaleType是fitxy,Glide会默认按照图片实际大小加载。而其他的模式按照的ImageView的大小。
如果非要设置fitxy,那么使用Glide.with(context).load().centerCrop().into();或者使用 Glide.with(context).load().fitCenter().into()
Glide 和dataBinding共同使用的时候,根节点不能是ImageView。
原因:Glide加载图片时候为了防止图片错位会给ImageView设置Tag,而dataBinding的原理也是给View设置tag。这样就会导致类型转换异常
解决方案:给ImageView嵌套一层父亲容器。