- Picasso簡單介紹
Picasso是由一家名叫square的公司開源的羔杨,Square是美國的一家移動(dòng)支付公司杨蛋,成立于2009年兜材,總部位于舊金山逞力。Square公司曾被[麻省理工大學(xué)]《MIT Technology Review》評為全球最聰明的50家公司之一曙寡。目前Android上比較優(yōu)秀的okhttp,retrofit框架都是出自這家公司寇荧,哈哈,說這些廢話就是想表達(dá)一個(gè)意思揩抡,這家公司比較牛逼!既然square那么牛逼峦嗤,Picasso自然也不會(huì)很菜蕊唐,肯定還是值得實(shí)用的。Picasso 主要是解決圖片加載以及圖片緩存問題的烁设,秉承了Square一向簡單明了的風(fēng)格替梨,功能強(qiáng)大且使用起來非常簡單。
- 先說幾個(gè)官方介紹的優(yōu)點(diǎn)
Picasso會(huì)自動(dòng)處理圖片加載過程的一些坑弓熏,比如:
1.Adapter的圖片復(fù)用過程中,能夠自動(dòng)回收圖片和取消圖片下載挽鞠。
2.使用最少的內(nèi)存來實(shí)現(xiàn)各種復(fù)雜的圖片變換狈孔。
3.Picasso會(huì)自動(dòng)進(jìn)行內(nèi)存和硬盤圖片緩存。
- 最基本的加載圖片方法
使用Picasso除抛,我們只需要一行代碼母截,就能實(shí)現(xiàn)圖片的加載功能,非常簡單明了清寇。
1.加載網(wǎng)絡(luò)圖片
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
2.加載本地資源
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(new File(...)).into(imageView3);
3.加載圖片并設(shè)置圖片加載進(jìn)度監(jiān)聽
Picasso.with(context)
.load(url)
.into(imageView,new Callback.EmptyCallback(){
@Override
public void onSuccess() {
super.onSuccess();
//在這里執(zhí)行你想要執(zhí)行的操作
}
@Override
public void onError() {
super.onError();
//在這里執(zhí)行你想要執(zhí)行的操作
}
});
- 在適配器中加載圖片
Picasso在適配器中會(huì)自動(dòng)識(shí)別復(fù)用并取消之前的下載任務(wù)。
@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);
}
- 對圖片進(jìn)行變換
1.使用Picasso可以對圖像進(jìn)行變換翩迈,以便更好地適應(yīng)布局和減少內(nèi)存大小盔夜。
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
2.對圖片進(jìn)行自定義變換,以產(chǎn)生更棒的效果喂链。
//創(chuàng)建MyTransformation類來實(shí)現(xiàn)Transformation 接口的,并實(shí)現(xiàn)transform方法進(jìn)行自定義變換
public class MyTransformation 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()"; }
}
- 設(shè)置下載過程中圖片顯示洞坑,圖片加載失敗后顯示的圖片
Picasso加載的過程中蝇率,加載失敗的圖片會(huì)自動(dòng)再進(jìn)行兩次重復(fù)加載嘗試,如果三次都失敗本慕,才會(huì)使用加載失敗替代圖。
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)//設(shè)置下載過程中圖片顯示
.error(R.drawable.user_placeholder_error)//設(shè)置圖片加載失敗顯示的圖片
.into(imageView);
- 程序調(diào)試提示
通過獲取Picasso的實(shí)例并設(shè)置setIndicatorsEnabled(true)间狂,Picasso在加載圖片的時(shí)候,會(huì)在圖片的左上角顯示不同的指示顏色忙菠,紅色說明當(dāng)前圖片加載自網(wǎng)絡(luò),綠色說明當(dāng)前圖片加載自內(nèi)存牛欢,藍(lán)色說明當(dāng)前圖片加載自硬盤。
Picasso.with(this).setIndicatorsEnabled(true);
- 對Picasso進(jìn)行封裝
和之前使用ImagerLoader一樣傍睹,我們也對Picasso進(jìn)行二次封裝一下。下面是我自己對Picasso進(jìn)行簡單封裝生成的一個(gè)類吮炕。
public class ImagerLoaderUtil {
private Picasso picasso;
private Context context;
private static ImagerLoaderUtil ImagerLoaderUtil;
public ImagerLoaderUtil(Context context) {
this.context = context;
}
public synchronized static ImagerLoaderUtil getInstance(Context context){
if(ImagerLoaderUtil == null){
ImagerLoaderUtil = new ImagerLoaderUtil(context);
ImagerLoaderUtil.initImageLoader();
}
return ImagerLoaderUtil;
}
public void displayMyImage(String imageUrl, ImageView imageView ){
picasso.load(imageUrl).into(imageView);
}
public void displayMyImage(String imageUrl, ImageView imageView,Callback callback ){
picasso.load(imageUrl).into(imageView, callback);
}
private void initImageLoader() {
picasso = Picasso.with(context);
}
}
對picasso類的封裝非常簡單访得,功能也比較單一,根據(jù)項(xiàng)目需求不同悍抑,可以相應(yīng)的實(shí)現(xiàn)displayMyImage更多的重載方法,使其更加強(qiáng)大搜骡。
對Picasso的簡單介紹就到這里了,如果想要了解更多谈竿,大家可以參考項(xiàng)目地址:Picasso的github項(xiàng)目地址摸吠。