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

Picasso – Android系统的图片下载和缓存类库的更多相关文章

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

    来源:http://blog.chengyunfeng.com/?p=492 另一篇参考:http://blog.csdn.net/xu_fu/article/details/17043231 Pic ...

  2. android:强大的图片下载和缓存库Picasso

    1.Picasso简单介绍 Picasso是Square公司出品的一个强大的图片下载和缓存图片库.官方网址是:http://square.github.io/picasso/ 仅仅须要一句代码就能够将 ...

  3. Android图片下载以及缓存框架

    实际开发中进行图片下载以及缓存的框架 介绍一下开发中常见图片加载框架的使用和对比一下优缺点. 1.Picasso 框架 在Android中开发,常需要从远程获取图片并显示在客户端,当然我们可以使用原生 ...

  4. Android菜鸟的成长笔记(5)——Android系统源代码你下载了吗?

    原文:Android菜鸟的成长笔记(5)--Android系统源代码你下载了吗? 在上一篇中我们用Android系统源代码分析了我们前面写的代码,有的朋友可能就会问怎么才能下载到Google官方的源代 ...

  5. android:强大的图像下载和缓存库Picasso

    1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...

  6. 【Android 系统开发】下载 编译 Android源代码 和 Android kernel源代码

    下载Android源码简要流程 : a. 获取repo文件: curl http://commondatastorage.googleapis.com/git-repo-downloads/repo ...

  7. Android系统源代码的下载与编译

    http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...

  8. android系统通过图片绝对路径获取URI的三种方法

    最近做项目要通过图片的绝对路径找到图片的URI,然后删除图片,小小总结一下获取URI的方法,亲自试验在 android 4.1.3的系统上都是可用的. 1.将所有的图片路径取出,遍历比较找到需要的路径 ...

  9. android系统源码下载

    ubuntu 安装git curl python 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: mkdir ~/bin PATH=~/bin:$PATH   下载 Repo 工具,并确 ...

随机推荐

  1. 什么是工程师文化?各位工程师是为什么活的?作为一个IT或互联网公司为什么要工程师文化?

    为什么要工程师文化? 看看最近二十年来社会的发展,计算机和互联网已经渗透到了这个社会的每一个角落,各式各样的计算机技术成为了整个世界发展的强大引擎,各式各样的创新,无论是业务创新还是技术创新,都是依托 ...

  2. Droppable(放置)组件

    .加载方式 //class 加载方式 <div id="dd" class="easyui-droppable" data-options="a ...

  3. easyui-form添加自定义表单验证

    easyui自定义表单验证规则其实不是很复杂,只要重写一下重写 $.fn.validatebox.defaults.rules 自定义示例 $.extend($.fn.validatebox.defa ...

  4. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  5. sql 字段字符串内容替换

    SELECT * FROM dbo.Table WHERE Name LIKE '%NYCL23%'UPDATE Table SET Name=replace(Name,'NYCL23','WYCL1 ...

  6. 强大的微软Microsoft Translator翻译接口

    一.前言 当我们需要对日文.韩文等语言转换中文字符的时候,就用到了微软提供的翻译接口. 二.实现流程 1.首先注册一个账号 https://datamarket.azure.com/account 2 ...

  7. C#类的基本用法

    Preson类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  8. C#Graphics画图

    public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { G ...

  9. easyui之combobox(不定时补充)

    1,combobox的valuefeild和textfeild这两个属性,可以理解为键值对,即:键valuefeild:值textfeild textfeild是文本框内显示的值,如果要取文本框内的值 ...

  10. (原)ubuntu下使用ftp软件

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6121663.html 参考网址: http://tieba.baidu.com/p/387426074 ...