Glide tool entry

public class ImageLoadUtils {

    private volatile static ImageLoadUtils sInstance;

    private ImageLoadUtils() {
    }

    public static ImageLoadUtils getInstance() {
        if (null == sInstance) {
            synchronized (ImageLoadUtils.class) {
                if(null == sInstance) { sInstance = new ImageLoadUtils(); }}}returnsInstance; } public void loadImage(ImageView view, String url){loadImage(view, url, 0); } @param view @param url @param defaultRes public void loadImage(ImageView view, String url) @DrawableRes int defaultRes){ loadImage(view, url, defaultRes, 0); } /** * public void loadImage(int radius, ImageView view, String url){ loadImage(view, url, 0, radius); } public void loadImage(ImageView) * @param view * @param URL * @param defaultRes * @param radius */ public void loadImage(ImageView view, String url, @DrawableRes int defaultRes, int radius){ RequestOptions options = new RequestOptions(); Transformation transformation = null;if(radius > 0 ){
            transformation = new MultiTransformation(
                    new CenterCrop(),
                    new RoundedCorners(radius)
            );
        }else{ transformation = new CenterCrop(); } options.transform(transformation); options.error(defaultRes); options.placeholder(defaultRes); Glide.with(view).load(url) .apply(options) .into(view); } public void loadCircle(ImageView view, String url){loadCircle(view, url, 0); } @param view @param url @param defaultRes */ public void loadCircle(ImageView view, String url, @DrawableRes int defaultRes){ RequestOptions options = RequestOptions.centerCropTransform() .placeholder(defaultRes) .error(defaultRes) .circleCrop(); Glide.with(view).load(url).apply(options).into(view); } /** * load round image (with border) * @param view * @param URL * @param borderWidth * @param borderColor */ public void loadCircleWithBorder(ImageView view,String url,int borderWidth,int borderColor) { loadCircleWithBorder(view, url, 0, borderWidth, borderColor); } /** * Loads round images (with default images and borders) * @param View * @param URL * @param defaultRes * @param borderWidth * @param borderColor */ public void loadCircleWithBorder(ImageView view, String url, @DrawableRes int defaultRes, int borderWidth, int borderColor){ RequestOptions options = RequestOptions .bitmapTransform(new GlideCircleWithBorder(borderWidth,borderColor)) .placeholder(defaultRes) .error(defaultRes); Glide.with(view).load(url).apply(options).into(view); } /** * public void loadBlurImage(ImageView view, ImageView, ImageView) The String url) {loadBlurImage (view, url, 15, 1); } @param view @param URL @param radius @param sampling */ public void loadBlurImage(ImageView) view, String url, int radius, int sampling){ loadBlurImage(view,url,0,radius,sampling); } @param view * @param url * @param cornRadius * @param sampling */ public void loadBlurImage(ImageView view, String url, int cornRadius, int radius, int sampling){if(radius == 0){
            radius = 15;
        }
        if(sampling == 0){
            sampling = 1;
        }
        Transformation transformation = null;
        if(cornRadius > 0 ){
            transformation = new MultiTransformation(
                    new BlurTransformation(radius, sampling),
                    new RoundedCorners(cornRadius)
            );
        }else{ transformation = new BlurTransformation(radius,sampling); } RequestOptions options = RequestOptions.bitmapTransform(transformation); Glide.with(view) .load(url) .apply(options) .into(view); } /** * public void loadBlurCircleImage(ImageView view, ImageView) The String url) {loadBlurCircleImage (view, url, 15, 1); } @param view @param URL @param radius @param sampling */ public void sampling loadBlurCircleImage(ImageView view, String url, int radius, int sampling){if(radius == 0){
            radius = 15;
        }
        if(sampling == 0){ sampling = 1; } MultiTransformation transformation = new MultiTransformation( new BlurTransformation(radius, sampling), new CircleCrop()); RequestOptions options = RequestOptions.bitmapTransform(transformation); Glide.with(view) .load(url) .apply(options) .into(view); }}Copy the code

Glide round frame class

public class GlideCircleWithBorder extends BitmapTransformation {

    private Paint mBorderPaint;
    private float mBorderWidth;
    
    public GlideCircleWithBorder(int borderWidth, int borderColor) {
        super();
        mBorderWidth = Resources.getSystem().getDisplayMetrics().density * borderWidth;
        mBorderPaint = new Paint();
        mBorderPaint.setDither(true);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(borderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);
    }

    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        if(mBorderPaint ! = null) {float borderRadius = r - mBorderWidth / 2;
            canvas.drawCircle(r, r, borderRadius, mBorderPaint);
        }
        return result;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }
}
Copy the code

Glide Gauss mode

public class BlurTransformation extends BitmapTransformation {

  private static final int VERSION = 1;
  private static final String ID =
      "com.xuetian.common.util.image.blur.BlurTransformation." + VERSION;

  private static int MAX_RADIUS = 25;
  private static int DEFAULT_DOWN_SAMPLING = 1;

  private int radius;
  private int sampling;

  public BlurTransformation() {
    this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
  }

  public BlurTransformation(int radius) {
    this(radius, DEFAULT_DOWN_SAMPLING);
  }

  public BlurTransformation(int radius, int sampling) {
    this.radius = radius;
    this.sampling = sampling;
  }

  @Override
  protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
                             @NonNull Bitmap toTransform, int outWidth, int outHeight) {

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

    Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);

    setCanvasBitmapDensity(toTransform, bitmap);

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    bitmap = FastBlur.blur(bitmap, radius, true);

    return bitmap;
  }

  @Override
  public String toString() {
    return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
  }

  @Override
  public boolean equals(Object o) {
    return o instanceof BlurTransformation &&
        ((BlurTransformation) o).radius == radius &&
        ((BlurTransformation) o).sampling == sampling;
  }

  @Override
  public int hashCode() {
    returnID.hashCode() + radius * 1000 + sampling * 10; } @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { messageDigest.update((ID + radius + sampling).getBytes(CHARSET)); }}Copy the code