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

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

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

帮助
1
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png.into(imageView"));
Picasso有如下特性:

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

在Adapter中下载

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

帮助

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

复制代码

图片转换

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

帮助

  1. Picasso.with(context)
  2. .load(url)
  3. .resize(50, 50)
  4. .centerCrop()
  5. .into(imageView)

复制代码

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

帮助

  1. public class CropSquareTransformation implements Transformation {
  2. @Override public Bitmap transform(Bitmap source) {
  3. int size = Math.min(source.getWidth(), source.getHeight());
  4. int x = (source.getWidth() - size) / 2;
  5. int y = (source.getHeight() - size) / 2;
  6. Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
  7. if (result != source) {
  8. source.recycle();
  9. }
  10. return result;
  11. }@Override public String key() { return "square()"; }
  12. }

复制代码

用该类示例调用函数 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.setDebug(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

转自:http://blog.chengyunfeng.com/?p=492

最新文章

  1. eclipse报错“Undefined variable from import: ...”解决方案
  2. MySQL 事物控制和锁定语句
  3. Microsoft Azure News(1) 新的数据中心Japan East, Japan West and Brazil South
  4. ASP.NET 小白从零开始建站简易教程 (一)域名、虚拟主机、FTP上传文件
  5. javascript特效--制作背景电子钟(整点时祝贺生日快乐)
  6. android 入门 006(sqlite增删改查)
  7. POJ 2886 Who Gets the Most Candies? 线段树
  8. suse系统卸载数据库实例
  9. Android Studio虚拟机配置虚拟键盘
  10. 解决svn: Cannot negotiate authentication mechanism错误问题
  11. ios-Ineligible Devices 不被识别的设备
  12. spring的注解使用
  13. Servlet 上传下载文件
  14. Android四大组件之 --- Service入门
  15. 红米手机5 Plus完美刷成开发版获取root权限的教程
  16. mui.init方法配置
  17. [EXP]Huawei Router HG532e - Command Execution
  18. 【Darwin学习笔记】之获取系统处理器数量的方法
  19. elasticsearch 自定义similarity 插件开发
  20. MC34063组成DC-DC电路

热门文章

  1. 初学Pexpect
  2. Python 文件I/O (转)
  3. 一小时搞定DIV+CSS布局-固定页面开度布局
  4. 新手们的GDI+绘制方格
  5. struct可以拥有class般的构造函数
  6. UCOS 内存管理理解 创建任务
  7. Hibernate学习笔记--Hibernate框架错误集合及解决
  8. 一个poi操作实现导出功能的类
  9. repo sync 时的自动续接脚本[转]
  10. cf C. Insertion Sort