官方:http://square.github.io/picasso/
简介
图片为
安卓应用添加了必备内容和视觉风格。Picasso允许应用程序加载图片——往往只需一行代码!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso会自动处理安卓加载图片时出现的许多常见缺陷:
1.在适配器中处理ImageView循环和下载取消。
2.保证最小内存使用率情况下的复杂图片转换。
3.自动内存和磁盘高速缓存。
特性
适配器下载
可以自动检测适配器复用
- @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);
- }
图像变换
变换图像可以更好地适应布局,并且减少内存大小。
- 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()"; }
- }
把该类的实例传递给变换方法。
占位符
Picasso把下载和错误占位符作为可选功能。
- Picasso.with(context)
- .load(url)
- .placeholder(R.drawable.user_placeholder)
- .error(R.drawable.user_placeholder_error)
- .into(imageView);
在显示错误占位符前请求会重试三次。
资源加载
资源,资产,文件,内容供应商均可作为图像源。
- Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
- Picasso.with(context).load(new File(...)).into(imageView2);
DEBUG指标
开发时可以启用彩带来指示图像源。在Picasso实例中调用setIndicatorsEnabled(true)。

Download
- MAVEN
- <dependency>
- <groupId>com.squareup.picasso</groupId>
- <artifactId>picasso</artifactId>
- <version>2.4.0</version>
- </dependency>