1. 程式人生 > >開發中使用Glide遇到的問題

開發中使用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巢狀一層父親容器。