Android Download Image Picasso
Android 通過Picasso 加載圖片。
1,圖片的異步加載(最基礎(chǔ)功能)
ImageView targetImageView = (ImageView) findViewById(R.id.ImageView); String Url = "http://img4.imgtn.bdimg.com/it/u=220980845,3087446489&fm=23&gp=0.jpg";//Picasso使用了流式接口的調(diào)用方式 //Picasso類是核心實(shí)現(xiàn)類。 //實(shí)現(xiàn)圖片加載功能至少需要三個(gè)參數(shù):Picasso //with(Context context) //Context對于很多Android API的調(diào)用都是必須的,這里就不多說了.with(context)//load(String imageUrl):被加載圖像的Url地址。 //大多情況下,一個(gè)字符串代表一個(gè)網(wǎng)絡(luò)圖片的URL。.load(Url)//into(ImageView targetImageView):圖片最終要展示的地方。.into(targetImageView);2.圖片轉(zhuǎn)換
使用最少的內(nèi)存完成復(fù)雜的圖片轉(zhuǎn)換,轉(zhuǎn)換圖片以適合所顯示的ImageView,來減少內(nèi)存消耗
3,加載過重 & 錯(cuò)誤處理
Picasso支持加載過程中和加載錯(cuò)誤時(shí)顯示對應(yīng)圖片
4,在Adapter中的回收不在視野的ImageView和取消已經(jīng)回收的ImageView下載進(jìn)程
@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); }5,從不同資源源加載
支持多種數(shù)據(jù)源 網(wǎng)絡(luò)、本地、資源、Assets 等
6,每次任務(wù)調(diào)度前會(huì)選擇優(yōu)先級高的任務(wù),比如 App 頁面中 Banner 的優(yōu)先級高于 Icon 時(shí)就很適用。
7,支持飛行模式、并發(fā)線程數(shù)根據(jù)網(wǎng)絡(luò)類型而變
手機(jī)切換到飛行模式或網(wǎng)絡(luò)類型變換時(shí)會(huì)自動(dòng)調(diào)整線程池最大并發(fā)數(shù),比如 wifi 最大并發(fā)為 4, 4g 為 3,3g 為 2
8, “無”本地緩存**
無”本地緩存,不是說沒有本地緩存,而是 Picasso 自己沒有實(shí)現(xiàn),交給了 Square 的另外一個(gè)網(wǎng)絡(luò)庫 okhttp 去實(shí)現(xiàn),這樣的好處是可以通過請求 Response Header 中的 Cache-Control 及 Expired 控制圖片的過期時(shí)間。
引用自 http://www.jianshu.com/p/51dc758b52f9
總結(jié)
以上是生活随笔為你收集整理的Android Download Image Picasso的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10月第4周榜单丨飞瓜数据B站UP主排行
- 下一篇: 如何快速地学习