Picasso開源圖片加載利器使用初探

javap 9年前發布 | 2K 次閱讀 Java

英文介紹鏈接地址 : http://square.github.io/picasso/

Picasso 英文意思國外一個很有名的畫家畢加索的名字,國外項目取名還是很有意思的!

從github新下載的picasso項目有依賴其他第三方開源項目okhttp和okio,這兩個項目也是相當經典的,據說okhttp里網絡請求的代碼處理邏輯已經加入到android4點幾的源碼中了。

picasso也提供了封裝好了的jar包可以使用,這樣就不需要導入okhttp和okio項目了,但是看jar包里的OkHttpDownloader這個類還是引用了okhttp里的對象,可是在jar包里并沒找到,不知道為什么~誰能解釋下啊?


Picasso使用的方法匯總:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso.with(context).load(url).into(view);
Picasso.with(context).load(url) .resize(50, 50).centerCrop().into(imageView)
//這里的placeholder將resource傳入通過getResource.getDrawable取資源,所以可以是張圖片也可以是color id
Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error).into(imageView);

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);
//這里顯示notification的圖片
Picasso.with(activity).load(Data.URLS[new Random().nextInt(Data.URLS.length)]).resizeDimen(R.dimen.notification_icon_width_height,    R.dimen.notification_icon_width_height).into(remoteViews, R.id.photo, NOTIFICATION_ID, notification);
//這里是通過設置tag標簽,就是當前傳過來的context,這樣就可以根據這個context tag來pause和resume顯示了
Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).fit().tag(context).into(view);
//監聽onScrollStateChanged的時候調用執行
picasso.resumeTag(context);
picasso.pauseTag(context);

Picasso.with(context).load(contactUri).placeholder(R.drawable.contact_picture_placeholder).tag(context).into(holder.icon);
//這個onpause方法里的這段代碼還是很有意思的
@Override protected void onPause() {
    super.onPause();
    if (isFinishing()) {
      // Always cancel the request here, this is safe to call even if the image has been loaded.
      // This ensures that the anonymous callback we have does not prevent the activity from
      // being garbage collected. It also prevents our callback from getting invoked even after the
      // activity has finished.
      Picasso.with(this).cancelRequest(imageView);
    }
  }
// Trigger the download of the URL asynchronously into the image view.
    Picasso.with(context)
        .load(url)
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
        .centerInside()
        .tag(context)
        .into(holder.image);
//Picasso.with使用的是單例模式
Picasso.with(this).cancelTag(this);

然后呢,Picasso還提供了debug的標示,調用picasso的setIndicatorsEnabled方法,true是debug模式,跟蹤代碼其實就是在最后生成的PicassoDrawable類的ondraw里繪制了個左上角小三角,根據

public enum LoadedFrom {
    MEMORY(Color.GREEN),
    DISK(Color.BLUE),
    NETWORK(Color.RED);

枚舉里的不同值標示不同加載來源,這對分析圖片加載有好處。

另外鏈接個分析picasso源碼不錯的博客地址http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0731/1639.html

不要感謝我哦!


Picasso真心是個很強大的圖片加載框架,之前用過universal-Image-Loader圖片加載框架也挺不錯的

 本文由用戶 javap 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!