开发中使用Glide遇到的问题

  • Glide加载图片变绿

    • 原因,Glide默认加载图片的格式是DecodeFormat.PREFER_RGB_565
      ,缺少ALPHA通道,导致加载图片变绿。
    • 解决方案

        Glide.setup(new GlideBuilder(context).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888));
      
  • Glide在弱网状态下加载大图片,失败几率很大。

    • 解决方案,配置自己的网络栈,
    • 前提:我用的是Retrofit2,其内部使用的是OkHttp3.
    • 配置

        compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
      
    • 自定义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>
      
    • 在弱网状态下即可解决图片加载问题

  • 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嵌套一层父亲容器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值