android picasso源码下载,Picasso:一个专为Android制作的强大的图片下载和缓存库
Picasso:一個(gè)專為Android打造的強(qiáng)大的圖片下載和緩存庫(kù)
簡(jiǎn)介
在Android應(yīng)用中,圖片消費(fèi)了大量的資源,卻為應(yīng)用提供了很好的視覺(jué)體驗(yàn)。幸運(yùn)的是,Picasso為你的應(yīng)用提供了非常容易的圖片加載方式——通常一行代碼就可以搞定!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso處理了Android上圖片加載的許多坑:
1)在Adapter中,處理了ImageView的循環(huán)利用和取消下載。
2)耗費(fèi)最小的內(nèi)存處理了復(fù)雜的圖形變換。
3)在內(nèi)存和磁盤中自動(dòng)緩存圖片。
特點(diǎn)
Adapter中的下載
自動(dòng)檢測(cè)adapter中的重用功能,且一旦發(fā)現(xiàn)重用,將自動(dòng)取消之前的下載。
@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);
}
圖片轉(zhuǎn)換
變換圖片以便更好的適應(yīng)布局,同時(shí)也減少了內(nèi)存的使用。
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
你也可以自定義圖片的轉(zhuǎn)換方式以達(dá)到更復(fù)雜的變換要求。
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這個(gè)類的一個(gè)實(shí)例傳遞給transform()函數(shù)即可。
占位圖片
Picasso同時(shí)支持“正在下載”時(shí)和“圖片下載出錯(cuò)”后這兩種狀態(tài)展示的默認(rèn)圖片。
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
Picasso至多會(huì)嘗試三次下載,如果三次下載都失敗了,就會(huì)在原圖位置上展示“圖片下載出錯(cuò)”時(shí)的圖片。
資源加載
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);
調(diào)試指示器
開(kāi)發(fā)者可以在圖片的左上角展示一個(gè)彩色的小三角,不同的顏色指明了圖片資源的不同來(lái)源。調(diào)用Picasso對(duì)象的setIndicatorsEnabled(true)方法就可以了。
下載
http://repo1.maven.org/maven2/com/squareup/picasso/picasso/2.5.2/picasso-2.5.2.jar
Picasso的源代碼,例子和這個(gè)網(wǎng)址(http://square.github.io/picasso/)都放在了GitHub上
MAVEN
com.squareup.picasso
picasso
2.5.2
GRADLE
compile 'com.squareup.picasso:picasso:2.5.2'
為Picasso做貢獻(xiàn)
如果你想為Picasso開(kāi)發(fā)貢獻(xiàn)你的代碼,你可以上github,然后fork該代碼庫(kù),再把你的修改發(fā)給我們(pull request)。
在提交代碼的時(shí)候,請(qǐng)保持代碼的習(xí)慣和風(fēng)格與原來(lái)的一致以便于盡可能保持代碼的可閱讀性。同時(shí),為了保證你的代碼正確編譯,請(qǐng)運(yùn)行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/
總結(jié)
以上是生活随笔為你收集整理的android picasso源码下载,Picasso:一个专为Android制作的强大的图片下载和缓存库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 求丁开头的成语接龙!
- 下一篇: 鸿蒙系统3.0演示,华为鸿蒙系统3.0-