SoFunction
Updated on 2025-03-11

Glide loading round pictures and rounded corner pictures example code in Android

1. Introduction:

Introduce two methods to use BitmapTransformation to implement Glide loading circle and rounded corner pictures. Glide cannot directly support Round Pictures, and needs to be processed using BitmapTransformation.

2. Online implementation method

Here are two common methods on the Internet and the two methods of using RoundedBitmapDrawable, which are essentially similar:

  1. Use Canvas and Paint to draw
  2. use .

Implementing circular pictures:

/**
  *
  * Glide Circular Picture Transform
  */

public class GlideCircleTransform extends BitmapTransformation {
  public GlideCircleTransform(Context context) {
    super(context);
  }

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

  private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;
    int size = ((), ());
    int x = (() - size) / 2;
    int y = (() - size) / 2;
    Bitmap squared = (source, x, y, size, size);
    Bitmap result = (size, size, .ARGB_8888);
    if (result == null) {
      result = (size, size, .ARGB_8888);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    (new BitmapShader(squared, , ));
    (true);
    float r = size / 2f;
    (r, r, r, paint);
    return result;
  }

  @Override
  public String getId() {
    return getClass().getName();
  }
}

Implementing rounded corner pictures:

/**
  * Glide Rounded Corner Transform
  */

public class GlideRoundTransform extends BitmapTransformation {

  private static float radius = 0f;

  /**
  * Constructor Default Rounded Corner Radius 4dp
  *
  * @param context Context
  */
  public GlideRoundTransform(Context context) {
    this(context, 4);
  }

  /**
  * Constructor
  *
  * @param context Context
  * @param dp Rounded corner radius
  */
  public GlideRoundTransform(Context context, int dp) {
    super(context);
    radius = ().getDisplayMetrics().density * dp;
  }

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

  private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;

    Bitmap result = ((), (), .ARGB_8888);
    if (result == null) {
      result = ((), (), .ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    (new BitmapShader(source, , ));
    (true);
    RectF rectF = new RectF(0f, 0f, (), ());
    (rectF, radius, radius, paint);
    return result;
  }

  @Override
  public String getId() {
    return getClass().getName() + (radius);
  }
}

3. The simple implementation method I prefer

//Load the rounded corner picture   public static void loadRoundImage(final Context context, String url,final ImageView imageView){
     (context)
         .load(url)
         .asBitmap()
         .placeholder(placeholder)
         .error(placeholder)
         .diskCacheStrategy() //Set cache         .into(new BitmapImageViewTarget(imageView){
           @Override
           protected void setResource(Bitmap resource) {
             (resource);
             RoundedBitmapDrawable circularBitmapDrawable =
                 ((), resource);
             (10); //Set the rounded radian             (circularBitmapDrawable);
           }
         });
   }


  //Load the circular picture  public static void loadCirclePic(final Context context, String url, final ImageView imageView) {
    (context)
        .load(url)
        .asBitmap()
        .placeholder(placeholder)
        .error(placeholder)
        .diskCacheStrategy() //Set cache        .into(new BitmapImageViewTarget(imageView) {
          @Override
          protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                ((), resource);
            (true);
            (circularBitmapDrawable);
          }
        });

  }

The implementation of the source code of drawableToBitmap is like this

public static Bitmap drawableToBitmap(Drawable drawable) {
    // Take the length and width of the drawable    int w = ();
    int h = ();

    // Take the color format of drawable     config = () !=  ? .ARGB_8888
        : .RGB_565;
    // Create a corresponding bitmap    Bitmap bitmap = (w, h, config);
    // Create a canvas corresponding to bitmap    Canvas canvas = new Canvas(bitmap);
    (0, 0, w, h);
    // Draw drawable content into the canvas    (canvas);
    return bitmap;
  }
/**
  * RoundedBitmapDrawable is a class under V4, which cannot be simply passed: cast to BitmapDrawable
  * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
  */

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.