content: package com.bihe0832.android.lib.media.image.blur;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.os.Build;
  import android.renderscript.RSRuntimeException;
  
  import com.bihe0832.android.lib.media.image.bitmap.FastBlur;
  import com.bihe0832.android.lib.media.image.bitmap.RSBlur;
  import com.bihe0832.android.lib.utils.os.BuildUtils;
  import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
  import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
  
  import java.security.MessageDigest;
  
  /**
   * @author zixie code@bihe0832.com Created on 2022/3/28.
   */
  public class BlurTransformation extends BitmapTransformation {
  
  
      private static final int VERSION = 1;
  
      private static final String ID = "com.bihe0832.android.common.image.blur.BlurTransformation." + VERSION;
  
      private static int DEFAULT_RADIUS = 25;
      private static int DEFAULT_DOWN_SAMPLING = 1;
  
      private Context mContext;
  
      private int mRadius;
      private int mSampling;
  
      public BlurTransformation(Context context) {
          this(context, DEFAULT_RADIUS, DEFAULT_DOWN_SAMPLING);
      }
  
      public BlurTransformation(Context context, int radius) {
          this(context, radius, DEFAULT_DOWN_SAMPLING);
      }
  
      public BlurTransformation(Context context, int radius, int sampling) {
          mContext = context.getApplicationContext();
          mRadius = radius;
          mSampling = sampling;
      }
  
      @Override
      protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
          int width = toTransform.getWidth();
          int height = toTransform.getHeight();
          int scaledWidth = width / mSampling;
          int scaledHeight = height / mSampling;
  
          Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
          if (bitmap == null) {
              bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
          }
          Canvas canvas = new Canvas(bitmap);
          canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
          Paint paint = new Paint();
          paint.setFlags(Paint.FILTER_BITMAP_FLAG);
          canvas.drawBitmap(toTransform, 0, 0, paint);
          if (BuildUtils.INSTANCE.getSDK_INT() >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
              try {
                  bitmap = RSBlur.blur(mContext, bitmap, mRadius);
              } catch (RSRuntimeException e) {
                  bitmap = FastBlur.blur(bitmap, mRadius, true);
              }
          } else {
              bitmap = FastBlur.blur(bitmap, mRadius, true);
          }
          return bitmap;
      }
  
      @Override
      public String toString() {
          return "BlurTransformation(radius=" + mRadius + ", sampling=" + mSampling + ")";
      }
  
      @Override
      public boolean equals(Object o) {
          return o instanceof BlurTransformation &&
                  ((BlurTransformation) o).mRadius == mRadius &&
                  ((BlurTransformation) o).mSampling == mSampling;
      }
  
      @Override
      public int hashCode() {
          return ID.hashCode() + mRadius * 1000 + mSampling * 10;
      }
  
      @Override
      public void updateDiskCacheKey(MessageDigest messageDigest) {
          messageDigest.update((ID + mRadius + mSampling).getBytes(CHARSET));
      }
  }