Skip to content

Instantly share code, notes, and snippets.

@bholota
Created February 6, 2017 19:59
Show Gist options
  • Save bholota/d174002d013ccca68559f57b782ff726 to your computer and use it in GitHub Desktop.
Save bholota/d174002d013ccca68559f57b782ff726 to your computer and use it in GitHub Desktop.
public class BitmapUtils {
public static Bitmap cropCircleCenterBitmap(Bitmap bitmap) {
int size;
int x;
int y;
// calculate square bitmap
if (bitmap.getWidth() >= bitmap.getHeight()) {
x = bitmap.getWidth() / 2 - bitmap.getHeight() / 2;
y = 0;
size = bitmap.getHeight();
} else {
x = 0;
y = bitmap.getHeight() / 2 - bitmap.getWidth() / 2;
size = bitmap.getWidth();
}
Rect source = new Rect(x, y, x + size, y + size);
Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
final Rect rect = new Rect(0, 0, size, size);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(size / 2, size / 2, size / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, source, rect, paint);
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment