在做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()==){
  return source;
  }   //如果图片小于设置的宽度,则返回原图
  if(source.getWidth()<targetWidth){
  return source;
  }else{
  //如果图片大小大于等于设置的宽度,则按照设置的宽度比例来缩放
  double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
  int targetHeight = (int) (targetWidth * aspectRatio);
  if (targetHeight != && targetWidth != ) {
  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加载网络图片等比例缩放的更多相关文章

  1. Android 使用Glide加载网络图片等比例缩放

    在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...

  2. Picasso加载网络图片失败,提示decodestream时返回null

    最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...

  3. Android通过URL加载网络图片

    public static Bitmap getBitmap(String path) throws IOException { URL url = new URL(path); HttpURLCon ...

  4. xamarin.Android ImageView 异步加载网络图片

    /// <summary> /// 异步获取文件流 /// </summary> /// <param name="url"></para ...

  5. 【转载】一行代码加载网络图片到ImageView——Android Picasso

    原文链接:一句代码加载网络图片到ImageView——Android Picasso  注意:此处使用下面代码需要先配置一下gradle,下载所需包. 具体操作如下图: compile 'com.sq ...

  6. Android中用双缓存技术,加载网络图片

    最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...

  7. wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...

  8. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  9. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

随机推荐

  1. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  2. Node.js(day3)

    一.模块系统 1.什么是模块 Node.js中常用的核心模块有: http模块 fs文件系统模块 url模块 path模块 os系统模块 在使用Node.js中我们发现每个js之间是没有联系的,都是单 ...

  3. [Swift]LeetCode295. 数据流的中位数 | Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. [Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  5. [Swift]LeetCode421. 数组中两个数的最大异或值 | Maximum XOR of Two Numbers in an Array

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  6. Python基础语法(三)

    Python基础语法(三) 1. 数值型数据结构 1.1 要点 在之前的博客也有提到,数值型数据结构在这里就不过多介绍了.在这里提及一些需要知道的知识点. int.float.complex.bool ...

  7. tensorflow 1.0 学习:十图详解tensorflow数据读取机制

    本文转自:https://zhuanlan.zhihu.com/p/27238630 在学习tensorflow的过程中,有很多小伙伴反映读取数据这一块很难理解.确实这一块官方的教程比较简略,网上也找 ...

  8. Redis Windows 64位下安装Redis详细教程

    Windows Redis 下载地址:点击打开链接https://github.com/MicrosoftArchive/redis/releases 点击打开链接 文件介绍 redis-benchm ...

  9. DocX开源WORD操作组件的学习系列三

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  10. tcp没用吗?为什么MOBA、“吃鸡”游戏不推荐用tcp协议

    本文由云+社区发表 作者:腾讯云游戏行业资深架构师 余国良 MOBA类和"吃鸡"游戏为什么对网络延迟要求高? 我们知道,不同类型的游戏因为玩法.竞技程度不一样,采用的同步算法不一样 ...