Glide工具類

關(guān)于Glide的介紹已經(jīng)無需多說,官方地址如下,分享一下我很久之前封裝的基于Glide的圖片加載工具類

https://github.com/bumptech/glide

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

//使用這個(gè)類來結(jié)合Glide可以比較簡(jiǎn)單地實(shí)現(xiàn)毛玻璃效果~
//implementation 'jp.wasabeef:glide-transformations:3.1.1'
import jp.wasabeef.glide.transformations.BlurTransformation;
                                              
public class GlideHelper {
    private static Context mContext;

    public GlideHelper() {

    }

    //在Application中初始化GlideHelper
    public static void initGlideHelper(Context context) {
        mContext = context;
    }

    //加載網(wǎng)絡(luò)圖片
    public static void showImage(ImageView view, String url, int isRound, Target target) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.ALL).timeout(60000).priority(Priority.HIGH);
        if (isRound == 1) {  //圓形帶有邊框效果
            options.transform(new GlideCircleStrokeTransform());
        } else if (isRound == 2) {  //圓形無邊框效果
            options.transform(new CircleCrop());
        } else if (isRound == 3) {  //自定義所加載View的弧度
            options.transforms(new Transformation[]{new CenterCrop(), new RoundedCorners(mContext.getResources().getDimensionPixelOffset(dimen.x10))});
        }

        RequestBuilder builder = Glide.with(mContext).load(url).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
    }

    //加載本地資源圖片
    public static void showImage(ImageView view, int resourceId, int isRound, Target target) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.ALL).timeout(60000).priority(Priority.HIGH);
        if (isRound == 1) {
            options.transform(new GlideCircleStrokeTransform());
        } else if (isRound == 2) {
            options.transform(new CircleCrop());
        } else if (isRound == 3) {
            options.transforms(new Transformation[]{new CenterCrop(), new RoundedCorners(mContext.getResources().getDimensionPixelOffset(dimen.x10))});
        }

        RequestBuilder builder = Glide.with(mContext).load(resourceId).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
    }

    //設(shè)置默認(rèn)占位圖和加載出現(xiàn)錯(cuò)誤時(shí)的缺省圖
    public static void showImage(ImageView view, String url, int placeHolder, int isRound, Target target) {
        RequestOptions options = (new RequestOptions()).timeout(60000).placeholder(placeHolder).error(placeHolder).diskCacheStrategy(DiskCacheStrategy.ALL).priority(Priority.HIGH);
        if (isRound == 1) {
            options.transform(new GlideCircleStrokeTransform());
        } else if (isRound == 2) {
            options.transform(new CircleCrop());
        } else if (isRound == 3) {
            options.transforms(new Transformation[]{new CenterCrop(), new RoundedCorners(mContext.getResources().getDimensionPixelOffset(dimen.x10))});
        }

        RequestBuilder builder = Glide.with(mContext).load(url).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
    }

    //加載Gif
    public static void showGifImage(ImageView view, String url) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.RESOURCE).timeout(60000).priority(Priority.HIGH);
        Glide.with(mContext).load(url).apply(options).into(view);
    }

    //加載Gif,可傳Target,Target可在此方法內(nèi)創(chuàng)建,也可由外部創(chuàng)建再傳入
    public static void showGifImage(ImageView view, String url, Target target) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.ALL).timeout(60000).priority(Priority.HIGH);
        RequestBuilder builder = Glide.with(mContext).asGif().load(url).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
    }

    //加載毛玻璃效果,BlurTransformation()括號(hào)內(nèi)數(shù)字為毛玻璃效果的程度
    public static void showGaosImage(ImageView view, int url, Target target) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.ALL).timeout(60000).transforms(new Transformation[]{new BlurTransformation(50)}).priority(Priority.HIGH);
        RequestBuilder builder = Glide.with(mContext).load(url).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
    }

    //加載本地圖片
    public static void showImage(ImageView view, int resourceId, Target target) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.ALL).timeout(60000).priority(Priority.HIGH);
        RequestBuilder builder = Glide.with(mContext).load(resourceId).apply(options);
        if (target == null) {
            builder.into(view);
        } else {
            builder.into(target);
        }
     }

    //ImageView加載本地圖片
     public static void showImage(ImageView view, String url) {
         showImageWithTarget(view, url, (Target)null);
     }

    //View加載網(wǎng)絡(luò)圖片
    public static void showImageWithTarget(ImageView view, String url, Target target) {
        if (!TextUtils.isEmpty(url)) {
            showImage(view, url, 0, target);
        }
    }

    //View加載本地圖片
    public static void showImageWithTarget(ImageView view, int resourceId, int isRound, Target target) {
        showImage(view, resourceId, isRound, target);
    }

    //內(nèi)部創(chuàng)建Target對(duì)象
    public static void showTargetImage(final View view, String url) {
        Target target = new SimpleTarget<Drawable>() {
            public void onResourceReady(Drawable resource, Transition transition) {
                view.setBackgroundDrawable(resource);
            }
        };
        showImageWithTarget((ImageView)null, url, target);
    }

    //View加載本地圖片資源
    public static void showTargetImage(final View view, int resourceId, int isRound) {
        Target target = new SimpleTarget<Drawable>() {
            public void onResourceReady(Drawable resource, Transition transition) {
                view.setBackgroundDrawable(resource);
            }
        };
        showImageWithTarget((ImageView)null, resourceId, isRound, target);
    }

    //View加載網(wǎng)絡(luò)圖片,圓形有邊框
    public static void showRoundImageWithStroke(ImageView view, String url) {
        if (!TextUtils.isEmpty(url)) {
            (new RequestOptions()).timeout(60000).placeholder(drawable.empty_photo).error(drawable.empty_photo).diskCacheStrategy(DiskCacheStrategy.ALL);
            RequestOptions options = RequestOptions.bitmapTransform(new GlideCircleStrokeTransform()).priority(Priority.HIGH);
            Glide.with(mContext).load(url).apply(options).into(view);
        }
    }

    //加載圖片圓形無邊框
    public static void showRoundImageNoStroke(ImageView view, String url) {
        if (!TextUtils.isEmpty(url)) {
            showImage(view, url, 2, (Target)null);
        }
    }

    //加載圖片圓形無邊框
    public static void showRoundImageNoStroke(ImageView view, String url, int placeHolder) {
        if (!TextUtils.isEmpty(url)) {
            showImage(view, url, placeHolder, 2, (Target)null);
        }
    }
}
自定義BitmapTransformation
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.support.annotation.ColorInt;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import java.security.MessageDigest;

public class GlideCircleStrokeTransform extends BitmapTransformation {
    private int mBorderWidth = 4;
    private int mBorderColor = -1;

    public GlideCircleStrokeTransform() {
    }

    public GlideCircleStrokeTransform(int borderWidth, @ColorInt int borderColor) {
        this.mBorderWidth = borderWidth;
        this.mBorderColor = borderColor;
    }

    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
        int x = (toTransform.getWidth() - size) / 2;
        int y = (toTransform.getHeight() - size) / 2;
        Bitmap squaredBitmap = Bitmap.createBitmap(toTransform, x, y, size, size);
        Bitmap bitmap = Bitmap.createBitmap(size, size, toTransform.getConfig() != null ? toTransform.getConfig() : Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap, TileMode.CLAMP, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);
        Paint mBorderPaint = new Paint();
        mBorderPaint.setStyle(Style.STROKE);
        mBorderPaint.setStrokeWidth((float)this.mBorderWidth);
        mBorderPaint.setColor(this.mBorderColor);
        mBorderPaint.setStrokeCap(Cap.ROUND);
        mBorderPaint.setAntiAlias(true);
        float r = (float)size / 2.0F;
        float r1 = (float)(size - 2 * this.mBorderWidth) / 2.0F;
        canvas.drawCircle(r, r, r1, paint);
        canvas.drawCircle(r, r, r1, mBorderPaint);
        squaredBitmap.recycle();
        return bitmap;
    }

    public void updateDiskCacheKey(MessageDigest messageDigest) {
    }
}

封裝已久,有待優(yōu)化.工具類的封裝仁者見仁智者見智,如果大家有更好的封裝方法,歡迎交流~

希望可以幫到大家~如文中有錯(cuò)誤請(qǐng)留言指正:一同交流,一起進(jìn)步。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【Android 庫 Glide】 引用 Android圖片加載框架最全解析(一),Glide的基本用法Andro...
    Rtia閱讀 5,909評(píng)論 0 22
  • Glide筆記 一、簡(jiǎn)介 在泰國舉行的谷歌開發(fā)者論壇上,谷歌為我們介紹了一個(gè)名叫Glide的圖片加載庫,作者是bu...
    AndroidMaster閱讀 4,090評(píng)論 0 27
  • 7.1 壓縮圖片 一、基礎(chǔ)知識(shí) 1、圖片的格式 jpg:最常見的圖片格式。色彩還原度比較好,可以支持適當(dāng)壓縮后保持...
    AndroidMaster閱讀 2,704評(píng)論 0 13
  • Glide4源碼解析系列 [Glide4源碼解析系列]--1.Glide初始化 [Glide4源碼解析系列]--...
    開發(fā)的貓閱讀 5,412評(píng)論 4 33
  • 文/蘇悸婉 我與唐楠相識(shí)在2010年冬季,記得那年北京的冬季特別的寒冷。 唐楠是陜西人,學(xué)金融專業(yè)畢業(yè)的,家里的人...
    蘇悸婉閱讀 950評(píng)論 0 8

友情鏈接更多精彩內(nèi)容