【转】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
【转】Picasso – Android系统的图片下载和缓存类库的更多相关文章
- Picasso – Android系统的图片下载和缓存类库
Picasso – Android系统的图片下载和缓存类库 Picasso 是Square开源的一个用于Android系统下载和缓存图片的项目.该项目和其他一些下载图片项目的主要区别之一是:使用4.0 ...
- android:强大的图片下载和缓存库Picasso
1.Picasso简单介绍 Picasso是Square公司出品的一个强大的图片下载和缓存图片库.官方网址是:http://square.github.io/picasso/ 仅仅须要一句代码就能够将 ...
- Android图片下载以及缓存框架
实际开发中进行图片下载以及缓存的框架 介绍一下开发中常见图片加载框架的使用和对比一下优缺点. 1.Picasso 框架 在Android中开发,常需要从远程获取图片并显示在客户端,当然我们可以使用原生 ...
- Android菜鸟的成长笔记(5)——Android系统源代码你下载了吗?
原文:Android菜鸟的成长笔记(5)--Android系统源代码你下载了吗? 在上一篇中我们用Android系统源代码分析了我们前面写的代码,有的朋友可能就会问怎么才能下载到Google官方的源代 ...
- android:强大的图像下载和缓存库Picasso
1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...
- 【Android 系统开发】下载 编译 Android源代码 和 Android kernel源代码
下载Android源码简要流程 : a. 获取repo文件: curl http://commondatastorage.googleapis.com/git-repo-downloads/repo ...
- Android系统源代码的下载与编译
http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...
- android系统通过图片绝对路径获取URI的三种方法
最近做项目要通过图片的绝对路径找到图片的URI,然后删除图片,小小总结一下获取URI的方法,亲自试验在 android 4.1.3的系统上都是可用的. 1.将所有的图片路径取出,遍历比较找到需要的路径 ...
- android系统源码下载
ubuntu 安装git curl python 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: mkdir ~/bin PATH=~/bin:$PATH 下载 Repo 工具,并确 ...
随机推荐
- Mysql数据库迁移 Ubuntu14.04
1. 停止数据库服务 sudo service mysql stop 2. 创建数据迁移目标文件夹(实际应该是挂载到新硬盘上) cd /var/lib ls -l drwx------ 6 mysq ...
- Android核心组件 Service
Service: 服务 Service 是Activity系统的核心组件之一. 因此需要继承和注册 Service 是内有界面的, 适合在后台长期的执行任务. (如放歌, 检测版本跟新, 下载, 上传 ...
- WCF、WebAPI、WCF REST、Web Service之间的区别
在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...
- Python学习_算数运算函数
记录以grades列表为例,分别定义输出.求和.平均值.方差和标准差函数,并输出相应的值 grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90 ...
- PDF ITextSharp
示例源码 //Document:(文档)生成pdf必备的一个对象,生成一个Document示例 Document document = new Document(PageSize.A4, 30, 30 ...
- 后台启动mysql ,redis
mysqld_safe --user=mysql & redis.conf daemonize no修改为daemonize yes
- Cygwin安装与配置
Cygwin可以在windows环境下模拟Linux系统,而且可以重用Linux下面丰富的脚本工具.windows的cmd太弱了.Cygwin是由Cygnus(天鹅座) Solution公司开发,不过 ...
- UI事件监听的击穿
什么是UI事件监听的击穿 在游戏视图中,有两个UI界面叠在一起的时候,单击一个空白处,却触发了被覆盖在下层了UI界面中的单击事件,这就是单击击穿了上层界面. 假设场景中放置了一个箱子,单击箱子会触发一 ...
- 在工程中添加pch文件
在Xcode6之前,新建一个工程的时候,系统会帮我们自动新建一个以工程名为名字的pch (precompile header)文件,在开发过程中,可以将那些整个工程都广泛使用的头文件包含在该文件下,编 ...
- GitHub 有哪些优秀的项目
GitHub 有哪些优秀的项目 http://www.zhihu.com/question/20584141