Picasso
picasso是Square公司開源的一個Android圖形緩存庫,地址http://square.github.io/picasso/,可以實現圖片下載和緩存功能。僅僅只需要一行代碼就能完全實現圖片的異步加載
使用 :Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
可以解決:
1.在adapter中需要取消已經不在視野范圍的ImageView圖片資源的加載,否則會導致圖片錯位,Picasso已經解決了這個問題。
2.使用復雜的圖片壓縮轉換來盡可能的減少內存消耗
3.自帶內存和硬盤二級緩存功能
特性:
1、ADAPTER 中的下載:Adapter的重用會被自動檢測到,Picasso會取消上次的加載
@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、?圖片轉換:轉換圖片以適應布局大小并減少內存占用
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView);
3、可以自定義轉換
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()"; }
}
4、?Place holders-空白或者錯誤占位圖片:picasso提供了兩種占位圖片,未加載完成或者加載發生錯誤的時需要一張圖片作為提示
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
如果加載發生錯誤會重復三次請求,三次都失敗才會顯示erro Place holder
5、?資源文件的加載:除了加載網絡圖片picasso還支持加載Resources, assets, files, content providers中的資源文件
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File(...)).into(imageView2);