Skip to content

Instantly share code, notes, and snippets.

@Gongcu
Forked from jewelzqiu/CropBitmapToCircle.java
Created February 12, 2020 12:00
Show Gist options
  • Select an option

  • Save Gongcu/2ffc758ae6d989a5585310a324f7301f to your computer and use it in GitHub Desktop.

Select an option

Save Gongcu/2ffc758ae6d989a5585310a324f7301f to your computer and use it in GitHub Desktop.
Crop a bitmap to circle in Android
public static Bitmap getCircledBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
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);
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);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment