wasabeef / glide-transformations

An Android transformation library providing a variety of image transformations for Glide.
Apache License 2.0
9.9k stars 1.41k forks source link

CropCircleWithBorderTransformation - border radius incorrect #186

Open fprt77 opened 3 years ago

fprt77 commented 3 years ago

TransformationUtils.circleCrop(...) is working with minimal edge (Math.min(destWidth, destHeight)), but CropCircleWithBorderTransformation is drawing border radius using maximal edge.

https://github.com/wasabeef/glide-transformations/blob/8144929b636e19323289a240a4d5e18949f00049/transformations/src/main/java/jp/wasabeef/glide/transformations/CropCircleWithBorderTransformation.java#L73

As a result border is not around image when target width and height are different.

fprt77 commented 3 years ago

Fixed transform function should look like this:

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

    Bitmap bitmap = TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);

    setCanvasBitmapDensity(toTransform, bitmap);

    Paint paint = new Paint();
    paint.setColor(borderColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(borderSize);
    paint.setAntiAlias(true);

    int outMinEdge = Math.min(outWidth, outHeight);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(
      outMinEdge / 2f,
      outMinEdge / 2f,
      outMinEdge / 2f - borderSize / 2f,
      paint
    );

    return bitmap;
  }

I would create PR myself, but I do not have rights to push, since I am not marked as contributor.