標籤:android style blog http color java 使用 os
1.Picasso簡單介紹
Picasso是Square公司出品的一個強大的圖片下載和緩衝圖片庫。官方網址是:http://square.github.io/picasso/
僅僅須要一句代碼就能夠將圖片下載並設定到ImageView上。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
2.主要特點
2.1Adapter downloads
使用ListView,GridView的時候,自己主動檢測Adapter的重用(re-use),取消下載,使用緩衝。
@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影像處理與變換
將映像進行變換,以更好的適應布局控制項等,減小記憶體開銷。
Picasso.with(context) .load(url) .resize(200, 200) .centerCrop() .into(imageView)
當然,我們也能夠寫自己的變換類,可是必須實現Transformation介面,如:
/** * 自己定義介面,實現映像縮小為原來的一半 */public class CropSquareTransformation implements Transformation {@Overridepublic 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;}@Overridepublic String key() {return "square()";}}
然後設定transform方法就能夠了:
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").transform(new CropSquareTransformation()).into(iv_test2);
例如以下:
2.3。支援設定載入之前的圖片,和載入失敗後的圖片。
如:
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中的圖片檔案等。
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);Picasso.with(context).load(new File(...)).into(imageView2);
Picasso:http://square.github.io/picasso/
未經同意不得用於商業目的