Custom Glide

package com.wintec.huashang.ui;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

public class GlideRoundTransform  extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform(a) {
        this(4);
    }

    public GlideRoundTransform(int dp) {
        super(a);this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        // Cut while changing
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {}private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f.0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        // Keep only the upper left corner and upper right corner rounded (comment out the following two lines to make four corners rounded)
        RectF rectRound = new RectF(0f.100f, source.getWidth(), source.getHeight());
        canvas.drawRect(rectRound, paint);
        returnresult; }}Copy the code

use

public class GlideUtil {
    public static RequestBuilder<Drawable> GlideWithPlaceHolder(Context context, Object object) {
       / * the default strategy is DiskCacheStrategy. AUTOMATIC / / DiskCacheStrategy has five constants: // diskCacheStrategy. ALL uses DATA and RESOURCE to cache remote DATA. Only RESOURCE is used to cache local DATA. / / DiskCacheStrategy. NONE. Do not use the disk cache / / DiskCacheStrategy DATA before the source code is the original DATA to disk cache / / DiskCacheStrategy RESOURCE The data is written to the disk cache after the resource is decoded, that is, the transformed image resource after scaling and so on. / / DiskCacheStrategy. AUTOMATIC coding strategy according to the original image data and resources to automatically select disk caching strategies. * /

// Each render takes 1S
// return Glide.with(context).load(object).diskCacheStrategy(DiskCacheStrategy.ALL).apply(new RequestOptions().placeholder(R.drawable.camera).dontAnimate());
// One render in three takes 1S, default is Auto
// return Glide.with(context).load(object).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(new RequestOptions().placeholder(R.drawable.camera).dontAnimate());
// RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30)); // The rounded corner is 30
// return Glide.with(context).load(object).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().placeholder(R.drawable.camera).dontAnimate().bitmapTransform(new RoundedCorners(30)));
        return Glide.with(context).load(object).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().placeholder(R.drawable.camera).dontAnimate().transform(new GlideRoundTransform(15))); }}Copy the code

The actual call

GlideUtil.GlideWithPlaceHolder(MainActivity.this, data.toString()).into(iv);
Copy the code