1. 程式人生 > >Android圖片載入框架Picasso最全使用教程

Android圖片載入框架Picasso最全使用教程

Picasso介紹

A powerful image downloading and caching library for Android
一個Android下強大的圖片下載快取庫

Picasso實現了圖片的非同步載入,並解決了Android中載入圖片時常見的一些問題,它有以下特點:

  • Adapter中取消了不在檢視範圍內的ImageView的資源載入,因為可能會產生圖片錯位;
  • 使用複雜的圖片轉換技術降低記憶體的使用
  • 自帶記憶體和硬碟的二級快取機制

為什麼要用Picasso

  Android系統作為圖片資源載入的主角,它是通過影象的畫素點來把影象載入到記憶體中的;現在一張500W的攝像頭拍出的照片(2592x1936),載入到記憶體中需要大約19M的記憶體;如果你加入了訊號強度不一的網路中進行了複雜的網路請求,並進行圖片的快取與其他處理,你會耗費大量的時間與精力來處理這些問題,但如果用了Picasso, 這些問題都一消而散;

將Picasso加入到你的專案中

 目前Picasso的最新版本是2.5.2,你可以下載對應的Jar包,將Jar包新增到你的專案中,或者在build.gradle配置檔案中加入

compile 'com.squareup.picasso:picasso:2.5.2'
  • 1
save_snippets_01.png
  • 1

注意如果你開啟了混淆,你需要將以下程式碼新增到混淆規則檔案中:

-dontwarn com.squareup.okhttp.**
  • 1
save_snippets_01.png
  • 1

小試牛刀:從網路載入一張圖片

Picasso使用簡單易用的介面,並有一個實現類Picasso,一個完整的功能請求至少需要三個引數;

  • with(Context context) - Context上下文在很多Android Api中都是必須的
  • load(String imageUrl) - 圖片網路載入地址
  • into(ImageView targetImageView) - 想進行圖片展示的ImageView

簡單用例:

ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
String internetUrl = "http://www.jycoder.com/json/Image/1.jpg";

Picasso
    .with(context)
    .load(internetUrl)
    .into(targetImageView);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  就是這麼簡單,如果你的 URL地址正確並且圖片存在,在幾秒中之內就能看到這張圖片了;如果圖片資源不存在,Picasso也會有錯誤的回撥,現在你已經看到了只需3行程式碼就能載入圖片了,當然這只是冰山一角,讓我們繼續揭開Picasso的神祕面紗;

圖片的其他載入方式

  Picasso的圖片不僅僅能載入網路資源,也能從本地檔案,Android專案資源,以及URI地址進行圖片載入,下面我們就對這三種方式進行例項說明;

從Android Resources 中載入

  程式碼也是三行,只需要將網路資源地址更改為一個int值地址即可,上程式碼:

ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
int resourceId = R.mipmap.ic_launcher;

Picasso
    .with(context)
    .load(resourceId)
    .into(targetImageView);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意: R.mipmapAndroid Studio中新的資源引用路徑,這個老司機都知道.

從本地File檔案中載入

  如果你讓使用者選擇本地的一張圖片進行展示的話,就需要用到這個載入方式了,當然,也是So Easy,只需要將地址更換為一個File即可,上程式碼:

ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");

Picasso
    .with(context)
    .load(file)
    .into(targetImageView); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意:這個file並不一定非得是在你的裝置中,可以是任意的路徑,只要是File路徑即可;

URI地址中載入

  這個請求方式相比其他也並沒有什麼不同,上程式碼:

public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";

private static Uri resourceIdToUri(Context context, int resourceId) {
    return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
}

Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);

Picasso  
    .with(context)
    .load(uri)
    .into(targetImageView);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意:為了示範,只能用資原始檔轉換為URI,並不僅僅是這種方式, 它可以支援任意的URI地址;

OK,到此我們已經對Picasso有一個基本的認識和了解了,跟著我的腳步,繼續發現Picasso更多好玩的功能,下面會介紹Picasso在ListViewGridView的用法,願大家都有美好的一天~~