当前位置: 首页 > news >正文

【转】Picasso – Android系统的图片下载和缓存类库

来源:http://blog.chengyunfeng.com/?p=492

另一篇参考:http://blog.csdn.net/xu_fu/article/details/17043231

Picasso 是Square开源的一个用于Android系统下载和缓存图片的项目。该项目和其他一些下载图片项目的主要区别之一是:使用4.0+系统上的HTTP缓存来代替磁盘缓存。

Picasso 的使用是非常简单的,例如:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Picasso有如下特性:

  • 处理Adapter中的 ImageView 回收和取消已经回收ImageView的下载进程
  • 使用最少的内存完成复杂的图片转换,比如把下载的图片转换为圆角等
  • 自动添加磁盘和内存缓存
具体介绍

在Adapter中下载
自动检测Adapter中的ImageView重用和取消不必要的下载

@Override public void getView(int position, View convertView, ViewGroup parent) {
  SquaredImageView view = (SquaredImageView) convertView;
  if (view == null) {
    view = new SquaredImageView(context);
  }
  String url = getItem(position);
 
  Picasso.with(context).load(url).into(view);
}

图片转换

转换图片以适合所显示的ImageView,来减少内存消耗

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)

还可以设置自定义转换来实现高级效果,例如下面的矩形特效(把图片居中裁剪为矩形)

public class CropSquareTransformation implements Transformation {
  @Override public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
    if (result != source) {
      source.recycle();
    }
    return result;
  }
 
  @Override public String key() { return "square()"; }
}

用该类示例调用函数 RequestBuilder.transform(Transformation) 即可。

占位符图片

Picasso支持下载和加载错误占位符图片。

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

如果重试3次(下载源代码可以根据需要修改)还是无法成功加载图片 则用错误占位符图片显示。

支持本地资源加载

从 Resources, assets, files, content providers 加载图片都支持

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File("/images/oprah_bees.gif")).into(imageView2);

注意:

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

的方式不能支持9.png,还是要使用老办法:

iv.setImageResource(R.drawable.speech_bubble);

调试支持

调用函数 Picasso.setDebug(true) (注意:应该是 Picasso.with(MyMsg.this).setDebugging(true);)可以在加载的图片左上角显示一个 三角形 ,不同的颜色代表加载的来源

红色:代表从网络下载的图片

黄色:代表从磁盘缓存加载的图片

绿色:代表从内存中加载的图片

如果项目中使用了OkHttp库的话,默认会使用OkHttp来下载图片。否则使用HttpUrlConnection来下载图片。

其他功能查看项目主页:http://github.com/square/picasso

参考项目:https://github.com/nostra13/Android-Universal-Image-Loader

https://github.com/mitmel/Android-Image-Cache

https://github.com/novoda/ImageLoader 

https://github.com/square/okhttp

 

 

 

转载于:https://www.cnblogs.com/sudawei/p/3553928.html

相关文章:

  • codeforces C. Cows and Sequence 解题报告
  • hdu1231(最大连续子序列)
  • Android Studio Gradle project refresh failed No such property classpath for class
  • 给hmailserver添加DKIM签名
  • SharePoint 2013 App 示例之图片墙
  • OAuth2.0 介绍
  • CCI_Q1.7
  • 服务器安全部署文档
  • [转]How to solve SSIS error code 0xC020801C/0xC004700C/0xC0047017
  • 【VC++学习笔记三】控件自绘
  • 长沙多校联合训练
  • Windows下修改Git bash的HOME路径(转)
  • 高性能javascript学习总结(3)--数据访问
  • 灵感不断
  • 各种触摸手势
  • JavaScript-如何实现克隆(clone)函数
  • python3.6+scrapy+mysql 爬虫实战
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • canvas 绘制双线技巧
  • Codepen 每日精选(2018-3-25)
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • Joomla 2.x, 3.x useful code cheatsheet
  • nginx 负载服务器优化
  • Python 反序列化安全问题(二)
  • Redash本地开发环境搭建
  • RxJS: 简单入门
  • Vue2 SSR 的优化之旅
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 判断客户端类型,Android,iOS,PC
  • 区块链将重新定义世界
  • 使用API自动生成工具优化前端工作流
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 微服务框架lagom
  • 学习笔记TF060:图像语音结合,看图说话
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • kubernetes资源对象--ingress
  • Linux权限管理(week1_day5)--技术流ken
  • !$boo在php中什么意思,php前戏
  • #pragma pack(1)
  • #QT(智能家居界面-界面切换)
  • #WEB前端(HTML属性)
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (二)丶RabbitMQ的六大核心
  • (九)信息融合方式简介
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (删)Java线程同步实现一:synchronzied和wait()/notify()