Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fobidlim
Created March 12, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fobidlim/fd3d4c781af73c03fcf3ffad74df338e to your computer and use it in GitHub Desktop.
Save fobidlim/fd3d4c781af73c03fcf3ffad74df338e to your computer and use it in GitHub Desktop.
Crop bitmap as circular
public static Bitmap getCircularBitmap(@NonNull Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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.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