您的位置:首页 > 移动开发 > Android开发

Picasso ——针对 Android的一个强大的图像下载和缓存库

2014-12-05 14:11 127 查看
简介

图片为安卓应用添加了必备内容和视觉风格。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


picasso-2.4.0.jar (112.05
KB, 下载次数: 5)

MAVEN

<dependency>

<groupId>com.squareup.picasso</groupId>

<artifactId>picasso</artifactId>

<version>2.4.0</version>

</dependency>

复制代码

原文:http://square.github.io/picasso/

翻译作者:eoe - @wanning

转载请注明:文章转载自eoeAndroid社区( http://www.eoeandroid.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: