Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active December 14, 2019 16:05
Show Gist options
  • Save WrathChaos/f97762f5e34b39e976d3 to your computer and use it in GitHub Desktop.
Save WrathChaos/f97762f5e34b39e976d3 to your computer and use it in GitHub Desktop.
How to circle ImageView in Java
// Circle ImageView Function (Could be custom rounded)
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    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());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

// USAGE
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.awesome);
imageView.setImageBitmap(getRoundedCornerBitmap(bitmap, 360));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment