怎么在Android中使用Picasso加载网络图片?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联主要从事成都网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务山东,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
通过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来完成。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
标题名称:怎么在Android中使用Picasso加载网络图片
文章网址:https://www.cdcxhl.com/article22/isjgcc.html
成都网站建设公司_创新互联,为您提供建站公司、定制开发、营销型网站建设、App设计、品牌网站制作、网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联