Glide Transformation 中的outWidth 和 outHeight 获取方式以及设置方式

起初我仅仅设置了ImageView的LayoutParams 的高度和宽度,但是发现在Transformantion中传来的outWidth和outHeight并没有和ImageView的LayoutParams 的宽高一致, 通过Debug 发现Glide是在ViewTarget类中的获取到outWidth和outHeight的,代码如下:

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);
}

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);
}

private int getTargetDimen(int viewSize, int paramSize, int paddingSize) {
  int adjustedViewSize = viewSize - paddingSize;
  if (isSizeValid(adjustedViewSize)) {
    return adjustedViewSize;
  }

  if (paramSize == PENDING_SIZE) {
    return PENDING_SIZE;
  }

  if (paramSize == LayoutParams.WRAP_CONTENT) {
    return SIZE_ORIGINAL;
  } else if (paramSize > 0) {
    return paramSize - paddingSize;
  } else {
    return PENDING_SIZE;
  }
}

private boolean isSizeValid(int size) {
  return size > 0 || size == SIZE_ORIGINAL;
}

解决方案: 设置ImageView的Bottom, imageView.setBottom(imageView.getTop() + height )

你可能感兴趣的:(android)