android:強大的圖片下載和緩衝庫Picasso

來源:互聯網
上載者:User

標籤: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/


未經同意不得用於商業目的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.