android:强大的图片下载和缓存库Picasso
来源:互联网 发布:qq的smtp服务器端口 编辑:程序博客网 时间:2024/05/16 17:21
- android:强大的图片下载和缓存库Picasso
1.Picasso简介
Picasso是Square公司出品的一个强大的图片下载和缓存图片库。官方网址是:http://square.github.io/picasso/
只需要一句代码就可以将图片下载并设置到ImageView上。
1Picasso.with(context).load(
"http://i.imgur.com/DvpvklR.png"
).into(imageView);
2.主要特点
2.1Adapter downloads
使用ListView,GridView的时候,自动检测Adapter的重用(re-use),取消下载,使用缓存。
123456789@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);
}
2.2图像处理与变换将图像进行变换,以更好的适应布局控件等,减小内存开销。
12345Picasso.with(context)
.load(url)
.resize(
200
,
200
)
.centerCrop()
.into(imageView)
当然,我们也可以写自己的变换类,但是必须实现Transformation接口,如:123456789101112131415161718192021/**
* 自定义接口,实现图像缩小为原来的一半
*/
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()"
;
}
}
然后设置transform方法就可以了:12Picasso.with(
this
).load(
"http://i.imgur.com/DvpvklR.png"
)
.transform(
new
CropSquareTransformation()).into(iv_test2);
效果图如下:2.3。支持设置加载之前的图片,和加载失败后的图片。
如:<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cD48cHJlIGNsYXNzPQ=="brush:java;">Picasso.with(this) .load("http://i.imgur.com/DvpvklR.png") .placeholder(R.drawable.abc) .error(R.drawable.ic_launcher) .transform(new CropSquareTransformation()) .into(iv_test1);
ImageView创建时显示abc.png,如果加载成功,显示的是DvpvklR.png,如果加载失败,显示ic_launcher.png.
2.4支持加载本地图片和sdcard中的图片文件等。
12Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(
new
File(...)).into(imageView2);
Picasso下载地址:http://square.github.io/picasso
/
0 0
- 强大的图片下载和缓存库Picasso
- 强大的图片下载和缓存库Picasso
- android:强大的图片下载和缓存库Picasso
- android:强大的图片下载和缓存库Picasso
- android:强大的图片下载和缓存库Picasso
- android:强大的图片下载和缓存库Picasso
- android:强大的图片下载和缓存库Picasso
- Picasso:一个强大的图片下载和缓存库
- Picasso:一个强大的图片下载和缓存库
- Picasso:一个专为Android打造的强大的图片下载和缓存库
- 一个强大的图片下载和缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- source 1.3 中不支持泛型 解决办法
- 《iOS总结》UITableViewCell的增加与删除-MickyChiang
- linux中sd卡驱动(2)
- [.Net码农]RDLC报表打印时提示 客户端无法加载打印控件的问题
- Hadoop单机伪分布式配置
- android:强大的图片下载和缓存库Picasso
- 基于zookeeper的分布式Queue
- [.Net码农]RDLC打印控件失败
- 1075 - Incorrect table definition;there can be only one auto column and it must be defined as a key
- 使用提交按钮,提交数据
- 状态为恢复中的SQLServer 数据修复方法
- base64加解密
- POJ 1635 Subway tree systems(HASH+判断两棵有根树是否同构)
- SQL Server中删除以指定字符开头的存储过程