Glide ViewTarget及SimpleTarget加载问题

Java基础

浏览数:21

2019-8-22

问题场景

加载异常大小的图片导致 900×30像素 ;
升级Glide到4.6之后,在加载图片时需要回调监听,使用BitmapImageViewTarget得到Bitmap对象之后进行设置,App崩溃。

  • 调用代码
GlideApp.with(this).asBitmap().load(url).into(new BitmapImageViewTarget(imv) {

    @Override
  public void onResourceReady (@NonNull Bitmap resource,
        @Nullable Transition<? super Bitmap> transition) {
        imv.setImageBitmap(resource);
    }
});
  • 报错信息

Glide加载崩溃处理.jpg

问题分析

在上述报错信息中实际上是图片过大导致加载失败,但是为什么会说是Glide的加载问题?

  • 使用SimpleTarget替换BitmapImageViewTarget,程序不会崩溃;

  • 使用ViewTarget本身包括其子类替换BitmapImageViewTarget,程序都会崩溃;

  • 不使用Target,程序不会崩溃;

解决过程

1、为什么替换为SimpleTarget就不会导致崩溃?

实际上我们需要看的是ViewTarget为什么都会导致崩溃.

ViewTarget源码
  • getTargetHeight():获取当前目标的高度
private int getTargetHeight() {
   int verticalPadding = view.getPaddingTop() +view.getPaddingBottom();
   LayoutParams layoutParams = view.getLayoutParams();
   int layoutParamSize = layoutParams != null ? layoutParams.height : PENDING_SIZE;
   return getTargetDimen(view.getHeight(),layoutParamSize, verticalPadding);
}
  • getTargetWidth(): 获取当前目标的宽度
private int getTargetWidth() {
  int horizontalPadding = view.getPaddingLeft() + view.getPaddingRight();
  LayoutParams layoutParams = view.getLayoutParams();
  int layoutParamSize = layoutParams != null ? layoutParams.width : PENDING_SIZE;
  return getTargetDimen(view.getWidth(), layoutParamSize, horizontalPadding);
}
  • getTargetDimen() : 测量最终的展示高度
private int getTargetDimen(int viewSize, int paramSize, int paddingSize) {
  
 int adjustedParamSize = paramSize - paddingSize;
  if (adjustedParamSize > 0) {
    return adjustedParamSize;
  }
  
 if (waitForLayout && view.isLayoutRequested()) {
    return PENDING_SIZE;
  }

 int adjustedViewSize = viewSize - paddingSize;
  if (adjustedViewSize > 0) {
    return adjustedViewSize;
  }

 if (!view.isLayoutRequested() && paramSize == LayoutParams.WRAP_CONTENT) {
    if (Log.isLoggable(TAG, Log.INFO)) {
      Log.i(TAG, "Glide treats LayoutParams.WRAP_CONTENT as a request for an image the size of"
  + " this device's screen dimensions. If you want to load the original image and are"
  + " ok with the corresponding memory cost and OOMs (depending on the input size), use"
  + " .override(Target.SIZE_ORIGINAL). Otherwise, use LayoutParams.MATCH_PARENT, set"
  + " layout_width and layout_height to fixed dimension, or use .override() with fixed"
  + " dimensions.");
    }
    return getMaxDisplayLength(view.getContext());
  }

 return PENDING_SIZE;
}

从源码中可看到,ViewTarget对其进行了高度和宽度的重新计算,使得图片最终展示格式并不符合Bitmap,当Bitmap绘制图片的时候则会抛出上面的异常,而在SimpleTarget中则没有对高度和宽度的重新计算。

2、为什么不使用Target就不会导致崩溃?

我们经常会这样调用Glide:

 GlideApp.with(this).asBitmap().load(url).into(imv);

实际上into()方法可传入ImageView,也可以传入Target;

  • into(imv);
    实际上默认会调用ViewTarget<ImageView,Bitmap>;
    RequestOptions requestOptions = this.requestOptions;
    if (!requestOptions.isTransformationSet()
    && requestOptions.isTransformationAllowed()
    && view.getScaleType() != null) {
                switch (view.getScaleType()) {
              case CENTER_CROP:
                requestOptions = requestOptions.clone().optionalCenterCrop();
                break;
              case CENTER_INSIDE:
                requestOptions = requestOptions.clone().optionalCenterInside();
                break;
              case FIT_CENTER:
              case FIT_START:
              case FIT_END:
                requestOptions = requestOptions.clone().optionalFitCenter();
                break;
              case FIT_XY:
                requestOptions = requestOptions.clone().optionalCenterInside();
                break;
              case CENTER:
              case MATRIX:
              default:
                // Do nothing.
      }
    }
    
    
  • into(ViewTarget);
    protected RequestOptions getMutableOptions() {
        return defaultRequestOptions == this.requestOptions
                  ? this.requestOptions.clone() : this.requestOptions;
    }
    

最终他们调用的requestOptions是不一样的,导致图片重新计算高度和宽度。

作者:会撒娇的犀犀利