【Android 進階】圖片加載框架之Glide

http://blog.csdn.net/leaf_130/article/details/54847104

簡介

在泰國舉行的谷歌開發者論壇上,谷歌爲我們介紹了一個名叫 Glide 的圖片加載庫,作者是bumptech。這個庫被廣泛的運用在google的開源項目中,包括2014年google I/O大會上發佈的官方app。

特點

(1)使用簡單 
(2)可配置度高,自適應程度高 
(3)支持常見圖片格式 : Jpg png gif webp 
(4)支持多種數據源: 網絡、本地、資源、Assets 等 
(5)高效緩存策略: 支持Memory和Disk圖片緩存,默認Bitmap格式採用RGB_565,內存使用至少減少一半. 
(6)生命週期集成: 根據Activity/Fragment生命週期自動管理請求 
(7)高效處理Bitmap : 使用Bitmap Pool使Bitmap複用,主動調用recycle回收需要回收的Bitmap,減小系統回收壓力.

功能API介紹:

1)簡單使用:

Glide
    .with(this)
    .load("http://xxx.com/source/a.png")
    .into(imageView);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2)Glide.with()的使用

(1)with(Context context). 使用Application上下文,Glide請求將不受Activity/Fragment生命週期控制。 
(2)with(Activity activity).使用Activity作爲上下文,Glide的請求會受到Activity生命週期控制。 
(3)with(FragmentActivity activity).Glide的請求會受到FragmentActivity生命週期控制。 
(4)with(Android.app.Fragment fragment).Glide的請求會受到Fragment 生命週期控制。 
(5)with(android.support.v4.app.Fragment fragment).Glide的請求會受到Fragment生命週期控制。

3)load()的使用

Glide基本可以load任何可以拿到的媒體資源

  • SD卡資源: 
    load(“file://”+ Environment.getExternalStorageDirectory().getPath()+”/test.jpg”)
  • assets資源: 
    load(“file:///android_asset/3.gif”)
  • raw資源: 
    load(“Android.resource://com.frank.glide/raw/raw_1”)或load(“android.resource://com.frank.glide/raw/”+R.raw.raw_1)
  • drawable資源: 
    load(“android.resource://com.veyron.glide/drawable/news”)或load(“android.resource://comveyron.glide/drawable/”+R.drawable.news)
  • ContentProvider資源: 
    load(“content://media/external/images/media/139469”)
  • http資源: 
    load(“http://img.my.csdn.NET/uploads/201508/05/1438760757_3588.jpg“)
  • https資源: 
    load(“https://img.alicdn.com/tps/TB1uyhoMpXXXXcLXVXXXXXXXXXX-476-538.jpg_240x5000q50.jpg_.webp“)

此外,load不限於string類型:

load(Uri uri),load(File file),load(Integer resourceId),load(URL url),load(byte[] model),load(T model),loadFromMediaStore(Uri uri)。 
  • 1
  • 1

4)重要功能

1)禁止內存緩存: .skipMemoryCache(true)
(2)清除內存緩存: // 必須在UI線程中調用
    Glide.get(context).clearMemory();
(3)禁止磁盤緩存: .diskCacheStrategy(DiskCacheStrategy.NONE) 
(4)清除磁盤緩存: // 必須在後臺線程中調用,建議同時clearMemory()
   Glide.get(applicationContext).clearDiskCache(); 
(5)獲取緩存大小: new GetDiskCacheSizeTask(textView).execute(new File(getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR));

class GetDiskCacheSizeTask extends AsyncTask<File, Long, Long> {
private final TextView resultView;

public GetDiskCacheSizeTask(TextView resultView) {
    this.resultView = resultView;
}

@Override
protected void onPreExecute() {
    resultView.setText("Calculating...");
}

@Override
protected void onProgressUpdate(Long... values) { /* onPostExecute(values[values.length - 1]); */ }

@Override
protected Long doInBackground(File... dirs) {
    try {
        long totalSize = 0;
        for (File dir : dirs) {
            publishProgress(totalSize);
            totalSize += calculateSize(dir);
        }
        return totalSize;
    } catch (RuntimeException ex) {
        final String message = String.format("Cannot get size of %s: %s", Arrays.toString(dirs), ex);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                resultView.setText("error");
                Toast.makeText(resultView.getContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }
    return 0L;
}

@Override
protected void onPostExecute(Long size) {
    String sizeText = android.text.format.Formatter.formatFileSize(resultView.getContext(), size);
    resultView.setText(sizeText);
}

private static long calculateSize(File dir) {
    if (dir == null) return 0;
    if (!dir.isDirectory()) return dir.length();
    long result = 0;
    File[] children = dir.listFiles();
    if (children != null)
        for (File child : children)
            result += calculateSize(child);
    return result;
}
}


(6)指定資源的優先加載順序:
//優先加載
    Glide
        .with(context)
        .load(heroImageUrl)
        .priority(Priority.HIGH)
        .into(imageViewHero);
    //後加載
    Glide
        .with(context)
        .load(itemImageUrl)
        .priority(Priority.LOW)
        .into(imageViewItem);

(7)先顯示縮略圖,再顯示原圖:
//用原圖的1/10作爲縮略圖
    Glide
        .with(this)
        .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
        .thumbnail(0.1f)
        .into(iv_0);

    //用其它圖片作爲縮略圖
    DrawableRequestBuilder<Integer> thumbnailRequest = Glide
        .with(this)
        .load(R.drawable.news);

    Glide.with(this)
        .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
        .thumbnail(thumbnailRequest)
        .into(iv_0);
(8)對圖片進行裁剪、模糊、濾鏡等處理:具體看demo源碼
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

5)部分api介紹:

這裏寫圖片描述

這裏寫圖片描述

補充:

CenterCrop 
CenterCrop()是一個裁剪技術,即縮放圖像讓它填充到 ImageView 界限內並且裁剪額外的部分。ImageView 可能會完全填充,但圖像可能不會完整顯示。
FitCenter 
fitCenter() 也是裁剪技術,即縮放圖像讓圖像都測量出來等於或小於 ImageView 的邊界範圍。該圖像將會完全顯示,但可能不會填滿整個 ImageView。
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

使用步驟:

1)在build.gradle中添加依賴:

compile 'com.github.bumptech.glide:glide:3.7.0'
  • 1
  • 1

2)如果你的項目沒有support-v4庫,還需要添加support-v4依賴:

 compile 'com.android.support:support-v4:23.3.0'
  • 1
  • 1

3)如果使用變換,可以添加一個自定義的變換庫 
添加依賴:

compile 'jp.wasabeef:glide-transformations:2.0.1'
    // If you want to use the GPU Filters
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Demo

ExampleForGlide

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章