Glide等比缩放图片问题总结

前言

等比例缩放图片在聊天列表中比较常见,而不是显示固定宽高的图片。最近对IM项目迁移到Androidx时,顺便升级了glide,发现glide等比例缩放图片出现bug(自定义ImageViewTarget实现图片缩放),第一次能正常加载,第二次无法正常等比例缩放。原来项目是使用glide 3.7.0,现在是使用gilde 4.11.0 (4.10.0开始支持AndroidX)

解决方案

不同版本glide,ImageViewTarget使用差异如下:

glide 3.6.0

public class GlideRatioScaleTransForm extends ImageViewTarget<Bitmap> {

    private ImageView target;

    public GlideRatioScaleTransForm(ImageView target) {
        super(target);
        this.target = target;
    }

    @Override
    protected void setResource(Bitmap resource) {
        view.setImageBitmap(resource);

        int width = resource.getWidth();
        int height = resource.getHeight();

        //获取imageView的宽
        int imageViewWidth = target.getWidth();

        //计算缩放比例
        float sy = (float) (imageViewWidth * 0.2) / (float) (width * 0.2);

        //计算图片等比例放大后的高
        int imageViewHeight = (int) (height * sy);
        ViewGroup.LayoutParams params = target.getLayoutParams();
        params.height = imageViewHeight;
        target.setLayoutParams(params);
    }
}

glide 4.11.0

public class GlideRatioScaleTransForm extends ImageViewTarget<Bitmap> {

    public GlideRatioScaleTransForm(ImageView target) {
        super(target);
    }

    @Override
    protected void setResource(@Nullable Bitmap resource) {
        if (resource == null) {
            return;
        }
        view.setImageBitmap(resource);
        int width = resource.getWidth();
        int height = resource.getHeight();

        //获取imageView的宽
        int imageViewWidth = view.getWidth();
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (imageViewWidth <=0){//修复等比例缩放bug
            imageViewWidth = params.width;
        }
        //计算缩放比例
        float sy = (float) (imageViewWidth * 0.2) / (float) (width * 0.2);
        //计算图片等比例放大后的高
        int imageViewHeight = (int) (height * sy);
        params.height = imageViewHeight;
        view.setLayoutParams(params);
    }


}

使用

 <ImageView
        android:id="@+id/iv_img"
          android:layout_width="110dp"
          android:layout_height="110dp"
          />
//3.7.0
Glide.with(mContext)
                  .load(HttpConstant.SWIM_CHAT_BASE_URL + imageMessageBody.getUrl())
                  .asBitmap()
                  .placeholder(R.drawable.ic_net_error)
                  .thumbnail(0.5f)//缩略图
                  .diskCacheStrategy(DiskCacheStrategy.ALL)
                  .into(new GlideRatioScaleTransForm(iv_img));

 //4.11.0,asBitmap() 需要设置在 load(url)之前
 Glide.with(mContext)
         .asBitmap()
            .load(HttpConstant.SWIM_CHAT_BASE_URL + imageMessageBody.getUrl())
            .placeholder(R.drawable.ic_net_error)
            .thumbnail(0.5f)//缩略图
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(new GlideRatioScaleTransForm(iv_img));
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值