Picasso 实现图片的比例缩放
来源:互联网 发布:win10蓝牙 软件 编辑:程序博客网 时间:2024/04/29 12:20
聊天列表中显示图片,这时候需要将整个图片按照设定的最大宽高等比例缩放,才能在看小图的时候把整个图片都显示而不变形。
Picasso提供了这样的方法--实现Transformation
具体实现流程如下:
- 获取图片的宽高
- 设定显示的最大的宽高
- 按源图片宽高比例 计算压缩图片的宽高
- 根据压缩图片的宽高重新创建新图
具体实现如下:
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)
,有异常”Transformation list must not be null”,具体什么原因看源码
.load(url)
.transform(new CropSquareTransformation())
.into(mImgContent);
Picasso说明书
0 0
- Picasso 实现图片的比例缩放
- Picasso处理图片,按照设置的宽度比例来缩放
- css 实现图片的等比例缩放
- Picasso图片加载框架的使用及加载网络图片等比例缩放
- Android 使用Picasso加载网络图片等比例缩放
- Android 使用Picasso加载网络图片等比例缩放
- Android 使用Picasso加载网络图片等比例缩放
- Android 使用Picasso加载网络图片等比例缩放
- 图片实现等比例缩放
- 图片实现缩放功能,设置缩放比例
- 图片的等比例缩放
- 图片的缩放----等比例缩放
- CSS实现网页图片的等比例缩放
- javascript等比例缩放图片的实现代码
- javascript 实现网页图片等比例缩放
- 用css实现图片等比例缩放
- js实现图片等比例缩放
- 使用jquery实现图片等比例缩放
- 自定义控件(26)---图片伸缩、圆角切割、内存位图
- iOS开发常用开源框架——网络
- NSArray/NSMutableArray
- POJ 2923 Relocation(状态压缩+01背包)
- 字符集 编码
- Picasso 实现图片的比例缩放
- mybatis学习笔记(十二) 映射配置文件详解
- JavaScript题目
- css3的转换效果过渡效果动画效果和列
- 【腾讯优测干货】Crash率从2.2%降至0.2%,这个团队是怎么做到的?
- CC中的坐标系
- Android CPU使用率:top和dump cpuinfo的不同
- centos配置
- 四舍五入你所不知道的事