Skip to content

Instantly share code, notes, and snippets.

@fada21
Created January 28, 2014 11:26
Show Gist options
  • Save fada21/8666038 to your computer and use it in GitHub Desktop.
Save fada21/8666038 to your computer and use it in GitHub Desktop.
Crops a circle out of the thumbnail photo for Android (by Daniel Olshansky @google )
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
int halfWidth = bitmap.getWidth() / 2;
int halfHeight = bitmap.getHeight() / 2;
canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);
paint.setXfermode(new PorterDuffXfermode(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