Android 使用Picasso加载网络图片等比例缩放的实现方法
作者:记录自己的点点滴滴 时间:2023-08-29 15:36:32
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形。显然按照android:scaleType不能实现,因为会有很多限制,所以必须要自己写算法。
通过Picasso来缩放
其实picasso提供了这样的方法。具体是显示Transformation 的 transform 方法。
(1) 先获取网络或本地图片的宽高
(2) 获取需要的目标宽
(3) 按比例得到目标的高度
(4) 按照目标的宽高创建新图
Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = mImg.getWidth();
LogCat.i("source.getHeight()="+source.getHeight());
LogCat.i("source.getWidth()="+source.getWidth());
LogCat.i("targetWidth="+targetWidth);
if(source.getWidth()==0){
return source;
}
//如果图片小于设置的宽度,则返回原图
if(source.getWidth()<targetWidth){
return source;
}else{
//如果图片大小大于等于设置的宽度,则按照设置的宽度比例来缩放
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
if (targetHeight != 0 && targetWidth != 0) {
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, 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 "transformation" + " desiredWidth";
}
};
之后在Picasso设置transform
Picasso.with(mContext)
.load(imageUrl)
.placeholder(R.mipmap.zhanwei)
.error(R.mipmap.zhanwei)
.transform(transformation)
.into(viewHolder.mImageView);
Transformation 这是Picasso的一个非常强大的功能了,它允许你在load图片 -> into ImageView 中间这个过成对图片做一系列的变换。比如你要做图片高斯模糊、添加圆角、做度灰处理、圆形图片等等都可以通过Transformation来完成。
参考文章: https://stackoverflow.com/questions/21889735/resize-image-to-full-width-and-variable-height-with-picasso
总结
以上所述是小编给大家介绍的Android 使用Picasso加载网络图片等比例缩放的实现方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:https://www.cnblogs.com/jiangzhishan/archive/2018/08/03/9415066.html
标签:android,picasso,等比例缩放
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# string格式的日期时间字符串转为DateTime类型的方法
2022-04-07 09:34:30
IntelliJ IDEA本地代码提交到github网站不显示与本地不同步问题的解决办法
2022-09-21 10:50:44
![](https://img.aspxhome.com/file/2023/2/93342_0s.png)
java统计字符串中指定元素出现次数方法
2022-11-02 16:05:40
![](https://img.aspxhome.com/file/2023/9/78969_0s.jpg)
解决logback-classic 使用testCompile的打包问题
2021-07-01 08:16:50
使用windows控制台调试服务的方法
2023-08-19 03:34:30
maven工程中jar包瘦身的五种方法
2023-11-04 05:01:54
Java中一些基础概念的使用详解
2023-07-30 23:35:23
Java垃圾回收器的方法和原理总结
2022-06-27 07:11:46
![](https://img.aspxhome.com/file/2023/9/91069_0s.jpg)
Android仿简书搜索框效果的示例代码
2023-06-18 16:02:58
![](https://img.aspxhome.com/file/2023/7/83527_0s.gif)
Spring Boot 多数据源处理事务的思路详解
2022-04-21 18:21:47
Java实战宠物店在线交易平台的实现流程
2022-07-06 22:04:37
![](https://img.aspxhome.com/file/2023/4/119204_0s.png)
C#调用WebService实例开发
2022-11-21 22:51:08
![](https://img.aspxhome.com/file/2023/9/102209_0s.jpg)
详解Maven私 服Nexus的安装与使用
2023-11-24 12:34:02
C#通过属性名称获取(读取)属性值的方法
2023-10-10 11:49:06
C#中在WebClient中使用post发送数据实现方法
2023-05-01 00:03:54
java冷知识:javac AbstractProcessor详解
2022-08-01 19:32:09
![](https://img.aspxhome.com/file/2023/6/66626_0s.jpg)
一篇文章带你搞定JAVA注解
2023-03-15 05:22:23
![](https://img.aspxhome.com/file/2023/2/63012_0s.png)
Java集合删除元素ArrayList实例详解
2022-11-09 19:12:39
![](https://img.aspxhome.com/file/2023/0/69310_0s.png)
C#泛型约束的深入理解
2023-02-21 09:32:19
Spring Cloud之配置中心的搭建
2023-04-02 21:19:49