Picasso 实现图片的比例缩放

聊天列表中显示图片,这时候需要将整个图片按照设定的最大宽高等比例缩放,才能在看小图的时候把整个图片都显示而不变形。

Picasso提供了这样的方法--实现Transformation
具体实现流程如下:

  1. 获取图片的宽高
  2. 设定显示的最大的宽高
  3. 按源图片宽高比例 计算压缩图片的宽高
  4. 根据压缩图片的宽高重新创建新图

具体实现如下:

 private class CropSquareTransformation implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {

            int targetWidth = 目标最大宽度; 

            int targetHeight = 目标最大高度;

            if (source.getWidth() == 0 || source.getHeight() == 0) {
                return source;
            }

            if (source.getWidth() > source.getHeight()) {//横向长图
                if (source.getHeight() < targetHeight && source.getWidth() <= 400) {
                    return source;
                } else {
                    //如果图片大小大于等于设置的高度,则按照设置的高度比例来缩放
                    double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
                    int width = (int) (targetHeight * aspectRatio);
                    if (width > 400) { //对横向长图的宽度 进行二次限制
                        width = 400;
                        targetHeight = (int) (width / aspectRatio);// 根据二次限制的宽度,计算最终高度
                    }
                    if (width != 0 && targetHeight != 0) {
                        Bitmap result = Bitmap.createScaledBitmap(source, width, targetHeight, false);
                        if (result != source) {
                            // Same bitmap is returned if sizes are the same
                            source.recycle();
                        }
                        return result;
                    } else {
                        return source;
                    }
                }
            } else {//竖向长图
                //如果图片小于设置的宽度,则返回原图
                if (source.getWidth() < targetWidth && source.getHeight() <= 600) {
                    return source;
                } else {
                    //如果图片大小大于等于设置的宽度,则按照设置的宽度比例来缩放
                    double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                    int height = (int) (targetWidth * aspectRatio);
                    if (height > 600) {//对横向长图的高度进行二次限制
                        height = 600;
                        targetWidth = (int) (height / aspectRatio);//根据二次限制的高度,计算最终宽度
                    }
                    if (height != 0 && targetWidth != 0) {
                        Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, height, false);
                        if (result != source) {
                            // Same bitmap is returned if sizes are the same
                            source.recycle();
                        }
                        return result;
                    } else {
                        return source;
                    }
                }
            }
        }

        @Override
        public String key() {
            return "desiredWidth" + " desiredHeight";
        }
    }

使用Picasso加载网络或者本地图片时,进行如下设置:

     Picasso.with(context)
           .load(url)
                  .placeholder(R.drawable.default_pic)
                  .transform(new CropSquareTransformation())
                  .noFade()
                  .into(mImgContent);

注意

  • 目标最大宽高可以是ImageView.getWidth() 或者 getHeight().
  • 通过Transformation transformation = new Transformation() {}方式声明transform之后,直接调用Picasso.with(context)
    .load(url)
    .transform(new CropSquareTransformation())
    .into(mImgContent);
    ,有异常”Transformation list must not be null”,具体什么原因看源码

Picasso说明书

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值