picasso是Square公司开源的一个安卓图片加载框架,可以实现图片下载和缓存功能。picasso解决了adapter加载不在视野范围的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()”; }
}
将此类的实例传递给该transform方法。
Place Holders
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(“file:///android_asset/DvpvklR.png”).into(imageView2);
Picasso.with(context).load(new File(...))。into(imageView3);
Debug Indicators
对于开发,您可以启用指示图像源的彩**带的显示。调用setIndicatorsEnabled(true)毕加索实例。