Picasso:一個專為Android打造的強大的圖片下載和緩衝庫

來源:互聯網
上載者:User

標籤:

簡介

在Android應用中,圖片消費了大量的資源,卻為應用提供了很好的視覺體驗。幸運的是,Picasso為你的應用提供了非常容易的圖片載入方式——通常一行代碼就可以搞定!

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Picasso處理了Android片載入的許多坑:

1)在Adapter中,處理了ImageView的迴圈利用和取消下載。

2)耗費最小的記憶體處理了複雜的圖形變換。

3)在記憶體和磁碟中自動緩衝圖片。


特點Adapter中的下載

自動檢測adapter中的重用功能,且一旦發現重用,將自動取消之前的下載。

@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(Bitmapsource) {    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()"; }}

CropSquareTransformation這個類的一個執行個體傳遞給transform()函數即可。

佔位圖片

Picasso同時支援“正在下載”時和“圖片下載出錯”後這兩種狀態展示的預設圖片。

Picasso.with(context)    .load(url)    .placeholder(R.drawable.user_placeholder)    .error(R.drawable.user_placeholder_error).into(imageView);

Picasso至多會嘗試三次下載,如果三次下載都失敗了,就會在原圖位置上展示“圖片下載出錯”時的圖片。

資源載入

Picasso支援將resources、assets、files和contentprovider作為圖片的載入源。

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(newFile(...)).into(imageView3);

調試指標

開發人員可以在圖片的左上方展示一個彩色的小三角,不同的顏色指明了圖片資源的不同來源。調用Picasso對象的setIndicatorsEnabled(true)方法就可以了。


下載

http://repo1.maven.org/maven2/com/squareup/picasso/picasso/2.5.2/picasso-2.5.2.jar

Picasso的原始碼,例子和這個網址(http://square.github.io/picasso/)都放在了 GitHub上

MAVEN<dependency>  <groupId>com.squareup.picasso</groupId>  <artifactId>picasso</artifactId>  <version>2.5.2</version></dependency>GRADLEcompile 'com.squareup.picasso:picasso:2.5.2'

為Picasso做貢獻

如果你想為Picasso開發貢獻你的代碼,你可以上github,然後fork該程式碼程式庫,再把你的修改發給我們(pull request)。

在提交代碼的時候,請保持代碼的習慣和風格與原來的一致以便於儘可能保持代碼的可閱讀性。同時,為了保證你的代碼正確編譯,請運行mvn clean verify。

你需要同意Individual ContributorLicense Agreement (CLA)才能將你的代碼合并到Picasso工程中。

重要資料

1]Javadoc : http://square.github.io/picasso/javadoc/index.html

[2]StackOverflow:http://stackoverflow.com/questions/tagged/picasso?sort=active

許可證

Copyright 2013 Square, Inc.

 

Licensed under the Apache License, Version2.0 (the "License");

you may not use this file except incompliance with the License.

You may obtain a copy of the License at

 

  http://www.apache.org/licenses/LICENSE-2.0

 

Unless required by applicable law or agreedto in writing, software

distributed under the License isdistributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.

See the License for the specific languagegoverning permissions and

limitations under the License.

 

原文連結:http://square.github.io/picasso/







Picasso:一個專為Android打造的強大的圖片下載和緩衝庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.