0

I'm trying to do what is described here "I want to crop a circular region from this bitmap. All pixels outside the circle should be transparent."

I already trying what's in that post but all of them don't offer the transparent background, only a circle image, I tried the below and it didn't work, any ideas?

public Bitmap getCroppedCircleImage(Bitmap bmp, View view) {
    Bitmap bitmap = getCroppedImage(bmp, view);

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff227722;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    //canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    Paint paint1 = new Paint();
    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Rect rectTransparent=new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRect(rectTransparent,paint1);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
     OutputStream stream = null;
    try {
        stream = new FileOutputStream("/sdcard/test.png");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    try {
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}
Community
  • 1
  • 1
Adroid Freak
  • 329
  • 3
  • 18

1 Answers1

0

You can use this library to achieve your goal

https://github.com/hdodenhof/CircleImageView