Glide庫加載圖片

Glide庫加載圖片

文章轉載於:http://blog.csdn.net/shangmingchao

一. 下載

在build.gradle中添加依賴:

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

需要support-v4庫的支持,如果你的項目沒有support-v4庫(項目默認已經添加了),還需要添加support-v4依賴:

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

然後配置混淆規則:

    -keep public class * implements com.bumptech.glide.module.GlideModule
    -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
      **[] $VALUES;
      public *;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

其中第一個混淆規則表明不混淆所有的GlideModule。 
如果需要的話,還需添加相應的權限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 1
  • 2
  • 1
  • 2

二. 集成網絡框架OkHttp(可選)

Glide的網絡請求部分可以使用當前最流行的網絡請求框架Volley或OkHttp,也可以通過Glide的ModelLoader接口自己寫網絡請求。 
Glide默認使用HttpUrlConnection進行網絡請求,爲了讓APP保持一致的網絡請求形式,可以讓Glide使用我們指定的網絡請求形式請求網絡資源,這裏我們選OkHttp (具有支持HTTP/2、利用連接池技術減少請求延遲、緩存響應結果等等優點),需要添加一個集成庫:

    //OkHttp 2.x
    //compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
    //compile 'com.squareup.okhttp:okhttp:2.7.5'

    //OkHttp 3.x
    compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意: 
1. OkHttp 2.x和OkHttp 3.x需使用不同的集成庫。 
2. Gradle會自動將OkHttpGlideModule合併到應用的manifest文件中。 
3. 如果你沒有對所有的GlideModule配置混淆規則(即沒有使用-keep public class * implements com.bumptech.glide.module.GlideModule),則需要把OkHttp的GlideModule進行防混淆配置:

-keep class com.bumptech.glide.integration.okhttp.OkHttpGlideModule
  • 1
  • 1

三. 使用

簡單使用

Glide
    .with(this)
    .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
    .into(imageView);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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生命週期控制。

返回關聯了相應上下文的RequestManager實例。

requestManager.load()使用

Glide基本可以load任何可以拿到的媒體資源,如: 
load SD卡資源:load("file://"+ Environment.getExternalStorageDirectory().getPath()+"/test.jpg") 
load assets資源:load("file:///android_asset/f003.gif") 
load raw資源:load("Android.resource://com.frank.glide/raw/raw_1")或load("android.resource://com.frank.glide/raw/"+R.raw.raw_1) 
load drawable資源:load("android.resource://com.frank.glide/drawable/news")或load("android.resource://com.frank.glide/drawable/"+R.drawable.news) 
load ContentProvider資源:load("content://media/external/images/media/139469") 
load http資源:load("http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg") 
load 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)
load的資源也可以是本地視頻,如果想要load網絡視頻或更高級的操作可以使用VideoView等其它控件完成。 
而且可以使用自己的ModelLoader進行資源加載: 
using(ModelLoader<A, T> modelLoader, Class<T> dataClass)using(final StreamModelLoader<T> modelLoader)using(StreamByteArrayLoader modelLoader)using(final FileDescriptorModelLoader<T> modelLoader)
返回GenericRequestBuilder實例。

GenericRequestBuilder使用

GenericRequestBuilder<ModelType,DataType,ResourceType,TranscodeType>是最頂層的Request Builder,用於處理選項設置和開始一般resource類型資源的加載。其中ModelType是指代表資源的類型,如"http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg"這個String就代表了一張圖片資源,所以這個ModelType就是String。DataType是指ModelLoader提供的,可以被ResourceDecoder解碼的數據類型。ResourceType是指將要加載的resource類型。TranscodeType是指已解碼的資源將要被轉成的資源類型。

  1. thumbnail(float sizeMultiplier). 請求給定係數的縮略圖。如果縮略圖比全尺寸圖先加載完,就顯示縮略圖,否則就不顯示。係數sizeMultiplier必須在(0,1)之間,可以遞歸調用該方法。
  2. sizeMultiplier(float sizeMultiplier). 在加載資源之前給Target大小設置係數。
  3. diskCacheStrategy(DiskCacheStrategy strategy).設置緩存策略。DiskCacheStrategy.SOURCE:緩存原始數據,DiskCacheStrategy.RESULT:緩存變換(如縮放、裁剪等)後的資源數據,DiskCacheStrategy.NONE:什麼都不緩存,DiskCacheStrategy.ALL:緩存SOURC和RESULT。默認採用DiskCacheStrategy.RESULT策略,對於download only操作要使用DiskCacheStrategy.SOURCE。
  4. priority(Priority priority). 指定加載的優先級,優先級越高越優先加載,但不保證所有圖片都按序加載。枚舉Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW。默認爲Priority.NORMAL。
  5. dontAnimate(). 移除所有的動畫。
  6. animate(int animationId). 在異步加載資源完成時會執行該動畫。
  7. animate(ViewPropertyAnimation.Animator animator). 在異步加載資源完成時會執行該動畫。
  8. placeholder(int resourceId). 設置資源加載過程中的佔位Drawable。
  9. placeholder(Drawable drawable). 設置資源加載過程中的佔位Drawable。
  10. fallback(int resourceId). 設置model爲空時要顯示的Drawable。如果沒設置fallback,model爲空時將顯示error的Drawable,如果error的Drawable也沒設置,就顯示placeholder的Drawable。
  11. fallback(Drawable drawable).設置model爲空時顯示的Drawable。
  12. error(int resourceId).設置load失敗時顯示的Drawable。
  13. error(Drawable drawable).設置load失敗時顯示的Drawable。
  14. listener(RequestListener<? super ModelType, TranscodeType> requestListener). 監聽資源加載的請求狀態,可以使用兩個回調:onResourceReady(R resource, T model, Target<R> target, boolean isFromMemoryCache, boolean isFirstResource)onException(Exception e, T model, Target&lt;R&gt; target, boolean isFirstResource),但不要每次請求都使用新的監聽器,要避免不必要的內存申請,可以使用單例進行統一的異常監聽和處理。
  15. skipMemoryCache(boolean skip). 設置是否跳過內存緩存,但不保證一定不被緩存(比如請求已經在加載資源且沒設置跳過內存緩存,這個資源就會被緩存在內存中)。
  16. override(int width, int height). 重新設置Target的寬高值(單位爲pixel)。
  17. into(Y target).設置資源將被加載到的Target。
  18. into(ImageView view). 設置資源將被加載到的ImageView。取消該ImageView之前所有的加載並釋放資源。
  19. into(int width, int height). 後臺線程加載時要加載資源的寬高值(單位爲pixel)。
  20. preload(int width, int height). 預加載resource到緩存中(單位爲pixel)。
  21. asBitmap(). 無論資源是不是gif動畫,都作爲Bitmap對待。如果是gif動畫會停在第一幀。
  22. asGif().把資源作爲GifDrawable對待。如果資源不是gif動畫將會失敗,會回調.error()

技巧

  1. 禁止內存緩存:

        .skipMemoryCache(true)
    • 1
    • 1
  2. 清除內存緩存:

        // 必須在UI線程中調用
        Glide.get(context).clearMemory();
    • 1
    • 2
    • 1
    • 2
  3. 禁止磁盤緩存:

       .diskCacheStrategy(DiskCacheStrategy.NONE)
    • 1
    • 1
  4. 清除磁盤緩存:

       // 必須在後臺線程中調用,建議同時clearMemory()
       Glide.get(applicationContext).clearDiskCache();
    • 1
    • 2
    • 1
    • 2
  5. 獲取緩存大小:

       new GetDiskCacheSizeTask(textView).execute(new File(getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR));
    • 1
    • 1
    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;
    }
    }
    • 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
    • 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
  6. 指定資源的優先加載順序:

        //優先加載
        Glide
            .with(context)
            .load(heroImageUrl)
            .priority(Priority.HIGH)
            .into(imageViewHero);
        //後加載
        Glide
            .with(context)
            .load(itemImageUrl)
            .priority(Priority.LOW)
            .into(imageViewItem);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  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);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  8. 對圖片進行裁剪、模糊、濾鏡等處理: 
    推薦使用獨立的圖片處理庫:wasabeef/glide-transformations,使用也很簡單:

        compile 'jp.wasabeef:glide-transformations:2.0.0'
    • 1
    • 1

    之後我們就可以使用GenericRequestBuilder或其子類的transform()bitmapTransform()方法設置圖片轉換了:

        //圓形裁剪
        Glide.with(this)
            .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
            .bitmapTransform(new CropCircleTransformation(this))
            .into(iv_0);
        //圓角處理
        Glide.with(this)
            .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
            .bitmapTransform(new RoundedCornersTransformation(this,30,0, RoundedCornersTransformation.CornerType.ALL))
            .into(iv_0);
        //灰度處理
        Glide.with(this)
            .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
            .bitmapTransform(new GrayscaleTransformation(this))
            .into(iv_0);
        //其它變換...
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    可根據情況使用GenericRequestBuilder子類DrawableRequestBuilder的bitmapTransform(Transformation<Bitmap>... bitmapTransformations)transform(BitmapTransformation... transformations)transform(Transformation<GifBitmapWrapper>... transformation),或其子類BitmapRequestBuilder的transform(BitmapTransformation... transformations)transform(Transformation<Bitmap>... transformations)方法。 
    當然如果想自己寫Transformation: 
    最簡單的方式就是繼承BitmapTransformation

    private static class MyTransformation extends BitmapTransformation {
    
        public MyTransformation(Context context) {
            super(context);
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, 
                int outWidth, int outHeight) {
           Bitmap myTransformedBitmap = ... //對Bitmap進行各種變換處理。
           return myTransformedBitmap;
        }
    
        @Override
        public String getId() {
            // 返回代表該變換的唯一Id,會作爲cache key的一部分。
            // 注意:最好不要用getClass().getName(),因爲容易受混淆影響。如果變換過程不影響緩存數據,可以返回空字符串。
            return "com.example.myapp.MyTransformation";
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    使用時只需使用transform()bitmapTransform()方法即可:

    Glide.with(yourFragment)
        .load(yourUrl)
        .asBitmap()
        .transform(new MyTransformation(context))
        .into(yourView);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    自定義圖片處理時Glide會自動計算View/Target大小,我們不需要傳View的寬高,當然你可以使用override(int, int)去改變這種行爲。

    自定義圖片處理時,爲了避免創建大量Bitmap以及減少GC,可以考慮重用Bitmap,這就需要BitmapPool,典型地就是,從Bitmap池中拿一個Bitmap,用這個Bitmap生成一個Canvas, 然後在這個Canvas上畫初始的Bitmap並使用Matrix、Paint、或者Shader處理這張圖片。 
    爲了有效並正確重用Bitmap需要遵循以下三條準則:

    1. 永遠不要把transform()傳給你的原始resource或原始Bitmap給recycle()了,更不要放回BitmapPool,因爲這些都自動完成了。值得注意的是,任何從BitmapPool取出的用於自定義圖片變換的輔助Bitmap,如果不經過transform()方法返回,就必須主動放回BitmapPool或者調用recycle()回收。
    2. 如果你從BitmapPool拿出多個Bitmap或不使用你從BitmapPool拿出的一個Bitmap,一定要返回extras給BitmapPool。
    3. 如果你的圖片處理沒有替換原始resource(例如由於一張圖片已經匹配了你想要的尺寸,你需要提前返回), transform()`方法就返回原始resource或原始Bitmap。 
      如:
        private static class MyTransformation extends BitmapTransformation {
    
            public MyTransformation(Context context) {
                super(context);
            }
    
            @Override
            protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
                Bitmap result = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
                // 如果BitmapPool中找不到符合該條件的Bitmap,get()方法會返回null,就需要我們自己創建Bitmap了
                if (result == null) {
                    // 如果想讓Bitmap支持透明度,就需要使用ARGB_8888
                    result = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
                }
                //創建最終Bitmap的Canvas.
                Canvas canvas = new Canvas(result);
                Paint paint = new Paint();
                paint.setAlpha(128);
                // 將原始Bitmap處理後畫到最終Bitmap中
                canvas.drawBitmap(toTransform, 0, 0, paint);
                // 由於我們的圖片處理替換了原始Bitmap,就return我們新的Bitmap就行。
                // Glide會自動幫我們回收原始Bitmap。
                return result;
            }
    
            @Override
            public String getId() {
                // Return some id that uniquely identifies your transformation.
                return "com.example.myapp.MyTransformation";
            }
        }
    • 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
    • 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

    也可以直接實現Transformation接口,進行更靈活的圖片處理,如進行簡單地圓角處理:

    public class RoundedCornersTransformation  implements Transformation<Bitmap> {
    
        private BitmapPool mBitmapPool;
        private int mRadius;
    
        public RoundedCornersTransformation(Context context, int mRadius) {
            this(Glide.get(context).getBitmapPool(), mRadius);
        }
    
        public RoundedCornersTransformation(BitmapPool mBitmapPool, int mRadius) {
            this.mBitmapPool = mBitmapPool;
            this.mRadius = mRadius;
        }
    
        @Override
        public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
            //從其包裝類中拿出Bitmap
            Bitmap source = resource.get();
            int width = source.getWidth();
            int height = source.getHeight();
            Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(result);
            //以上已經算是教科書式寫法了
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
            canvas.drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, paint);
            //返回包裝成Resource的最終Bitmap
            return BitmapResource.obtain(result, mBitmapPool);
        }
    
        @Override
        public String getId() {
            return "RoundedTransformation(radius=" + mRadius + ")";
        }
    }
    • 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
    • 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
  9. 對請求狀態進行監聽:

    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
    
        private ImageView iv_0;
        private LoggingListener mCommonRequestListener;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv_0 = (ImageView) findViewById(R.id.iv_0);
            mCommonRequestListener = new LoggingListener<String, GlideDrawable>();
            Glide
                    .with(this)
                    .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
                    .listener(mCommonRequestListener)
                    .into(iv_0);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    /**
    * @param <A> model類型
    * @param <B> resource類型
    */
    public class LoggingListener<A, B> implements RequestListener<A, B> {
    private final int level;
    private final String name;
    private final RequestListener<A, B> delegate;
    
    public LoggingListener() {
        this("");
    }
    
    public LoggingListener(@NonNull String name) {
        this(Log.VERBOSE, name);
    }
    
    public LoggingListener(int level, @NonNull String name) {
        this(level, name, null);
    }
    
    public LoggingListener(RequestListener<A, B> delegate) {
        this(Log.VERBOSE, "", delegate);
    }
    
    public LoggingListener(int level, @NonNull String name, RequestListener<A, B> delegate) {
        this.level = level;
        this.name = name;
        this.delegate = delegate == null ? NoOpRequestListener.<A, B>get() : delegate;
    }
    
    @Override
    public boolean onException(Exception e, A model, Target<B> target, boolean isFirstResource) {
        android.util.Log.println(level, "GLIDE", String.format(Locale.ROOT,
                "%s.onException(%s, %s, %s, %s)\n%s",
                name, e, model, strip(target), isFirst(isFirstResource), android.util.Log.getStackTraceString(e)));
        return delegate.onException(e, model, target, isFirstResource);
    }
    
    @Override
    public boolean onResourceReady(B resource, A model, Target<B> target, boolean isFromMemoryCache,
                                   boolean isFirstResource) {
        String resourceString = strip(getResourceDescription(resource));
        String targetString = strip(getTargetDescription(target));
        android.util.Log.println(level, "GLIDE", String.format(Locale.ROOT,
                "%s.onResourceReady(%s, %s, %s, %s, %s)",
                name, resourceString, model, targetString, isMem(isFromMemoryCache), isFirst(isFirstResource)));
        return delegate.onResourceReady(resource, model, target, isFromMemoryCache, isFirstResource);
    }
    
    private String isMem(boolean isFromMemoryCache) {
        return isFromMemoryCache ? "sync" : "async";
    }
    
    private String isFirst(boolean isFirstResource) {
        return isFirstResource ? "first" : "not first";
    }
    
    private String getTargetDescription(Target<B> target) {
        String result;
        if (target instanceof ViewTarget) {
            View v = ((ViewTarget) target).getView();
            LayoutParams p = v.getLayoutParams();
            result = String.format(Locale.ROOT,
                    "%s(params=%dx%d->size=%dx%d)", target, p.width, p.height, v.getWidth(), v.getHeight());
        } else {
            result = String.valueOf(target);
        }
        return result;
    }
    
    private String getResourceDescription(B resource) {
        String result;
        if (resource instanceof Bitmap) {
            Bitmap bm = (Bitmap) resource;
            result = String.format(Locale.ROOT,
                    "%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
        } else if (resource instanceof BitmapDrawable) {
            Bitmap bm = ((BitmapDrawable) resource).getBitmap();
            result = String.format(Locale.ROOT,
                    "%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
        } else if (resource instanceof GlideBitmapDrawable) {
            Bitmap bm = ((GlideBitmapDrawable) resource).getBitmap();
            result = String.format(Locale.ROOT,
                    "%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
        } else if (resource instanceof Drawable) {
            Drawable d = (Drawable) resource;
            result = String.format(Locale.ROOT,
                    "%s(%dx%d)", resource, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        } else {
            result = String.valueOf(resource);
        }
        return result;
    }
    
    private static String strip(Object text) {
        return String.valueOf(text).replaceAll("(com|android|net|org)(\\.[a-z]+)+\\.", "");
    }
    }
    • 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
    • 97
    • 98
    • 99
    • 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
    • 97
    • 98
    • 99
    public final class NoOpRequestListener<A, B> implements RequestListener<A, B> {
    private static final RequestListener INSTANCE = new NoOpRequestListener();
    
    @SuppressWarnings("unchecked")
    public static <A, B> RequestListener<A, B> get() {
        return INSTANCE;
    }
    
    private NoOpRequestListener() {
    }
    
    @Override public boolean onException(Exception e, A a, Target<B> target, boolean b) {
        return false;
    }
    @Override public boolean onResourceReady(B b, A a, Target<B> target, boolean b2, boolean b1) {
        return false;
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    通過GenericRequestBuilder的listener()方法添加一個RequestListener實現,但要注意,最好不要用匿名類,也不要每次都創建新的監聽器,要使用單例進行統一監聽處理,以避免不必要的內存申請和不必要的引用。方法最好返回false,以便Glide能繼續進行後續處理(如顯示error佔位符)。

  10. 對資源的下載進度進行監聽: 
    可以藉助OkHttp的攔截器進行進度監聽。OkHttp的攔截器官方Sample請移步這裏。我們可以利用這個攔截器進行監聽並處理,需要自定義ModelLoader和DataFetcher,具體請詳見我的Git:https://github.com/shangmingchao/ProgressGlide,歡迎Star啊,不過沒有太大必要告訴用戶圖片加載的進度(sjudd和TWiStErRob他們說的)。同時也可以看一下TWiStErRob大神的實現(自備梯子哈~.~)。

四. Glide有哪些“坑”?

  1. ImageView的setTag問題 
    問題描述:如果使用Glideinto(imageView)爲ImageView設置圖片的同時使用ImageView的setTag(final Object tag)方法,將會導致java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting異常。因爲GlideViewTarget中通過view.setTag(tag)view.getTag()標記請求的,由於Android 4.0之前Tag存儲在靜態map裏,如果Glide使用setTag(int key, final Object tag)方法標記請求則可能會導致內存泄露,所以Glide默認使用view.setTag(tag)標記請求,你就不能重複調用了。 
    解決辦法:如果你需要爲ImageView設置Tag,必須使用setTag(int key, final Object tag)getTag(int key)方法,其中key必須是合法的資源ID以確保key的唯一性,典型做法就是在資源文件中聲明type="id"的item資源。
  2. placeholder()導致的圖片變形問題 
    問題描述:使用.placeholder()方法在某些情況下會導致圖片顯示的時候出現圖片變形的情況。這是因爲Glide默認開啓的crossFade動畫導致的TransitionDrawable繪製異常,詳細描述和討論可以看一下這個#363 issue。根本原因就是你的placeholder圖片和你要加載顯示的圖片寬高比不一樣,而Android的TransitionDrawable無法很好地處理不同寬高比的過渡問題,這的確是個Bug,是Android的也是Glide的。 
    解決辦法:使用.dontAnimate()方法禁用過渡動畫,或者使用animate()方法自己寫動畫,再或者自己修復TransitionDrawable的問題。
  3. ImageView的資源回收問題 
    問題描述:默認情況下,Glide會根據with()使用的Activity或Fragment的生命週期自動調整資源請求以及資源回收。但是如果有很佔內存的Fragment或Activity不銷燬而僅僅是隱藏視圖,那麼這些圖片資源就沒辦法及時回收,即使是GC的時候。 
    解決辦法:可以考慮使用WeakReference,如:

        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<>(imageView);
        ImageView target = imageViewWeakReference.get();
        if (target != null) {
            Glide.with(context).load(uri).into(target);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5
  4. ImageView的scaleType問題 
    scaleType默認爲fitCenter模式,如果你想設置成centerInside,不好意思,3.x還沒有這個方法,參見這個#591 issue,折中的解決辦法就是放棄使用centerInside,或者結合android:scaleType="centerInside".dontTransform()使用以禁止Glide對資源進行轉換。 
    如果你想要ImageView的寬高根據圖片資源的大小而定(即使用wrap_comtent),那麼你就必須明確告訴Glide我想加載原始資源:使用android:scaleType="center",或者.dontTransform(),或者.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)。 
    不推薦使用fitXY,因爲這樣Glide會加載全尺寸圖像到內存中而造成不必要的內存佔用。
  5. 異步線程完成後加載圖片的崩潰問題 
    問題描述:通常情況下,異步線程會被約束在Activity生命週期內,所以異步線程完成後使用Glide加載圖片是沒有問題的。但如果你的異步線程在Activity銷燬時沒有取消掉,那麼異步線程完成後就Glide就無法爲一個已銷燬的Activity加載圖片資源,拋出的異常如下(在with()方法中就進行判斷並拋出異常):

    java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
        at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
        at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
        at com.bumptech.glide.Glide.with(Glide.java:653)
        at com.frank.glidedemo.TestActivity.onGetDataCompleted(TestActivity.java:23)
        at com.frank.glidedemo.TestActivity.access$000(TestActivity.java:10)
        at com.frank.glidedemo.TestActivity$BackgroundTask.onPostExecute(TestActivity.java:46)
        at com.frank.glidedemo.TestActivity$BackgroundTask.onPostExecute(TestActivity.java:28)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    解決辦法:正確管理Background Threads(異步線程),當Activity停止或銷燬時,停止所有相關的異步線程,停止所有後續的UI操作。或者加載前使用isFinishing()isDestroyed()進行限制(不建議這種處理方式)。

  6. 由於Bitmap複用導致的在某些設備上圖片錯亂的問題 
    問題描述: Glide默認使用BitmapPool的方式對應用中用到的Bitmap進行復用,以減少頻繁的內存申請和內存回收,而且默認使用的Bitmap模式爲RGB565以減少內存開銷。但在某些設備上(通常在Galaxy系列5.X設備上很容易復現)某些情況下會出現圖片加載錯亂的問題,具體詳見這個#601 issue。原因初步確定是OpenGL紋理渲染異常。 
    解決辦法:GlideModule使用PREFER_ARGB_8888(Glide4.X已經默認使用該模式了),雖然內存佔用比RGB565更多一點,但可以更好地處理有透明度Bitmap的複用問題。或者禁用Bitmap複用setBitmapPool(new BitmapPoolAdapter())來修復這個問題(不推薦這種處理方式)。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章