Skip to content

Instantly share code, notes, and snippets.

@blaswan
Created February 24, 2014 11:26
Show Gist options
  • Select an option

  • Save blaswan/9186725 to your computer and use it in GitHub Desktop.

Select an option

Save blaswan/9186725 to your computer and use it in GitHub Desktop.
public static Bitmap getCircularCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap scaledBitmap;
if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
scaledBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
} else {
scaledBitmap = bitmap;
}
Bitmap output = Bitmap.createBitmap(scaledBitmap.getWidth(), scaledBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(scaledBitmap.getWidth() / 2 + 0.7f, scaledBitmap.getHeight() / 2 + 0.7f, scaledBitmap.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(scaledBitmap, rect, rect, paint);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment