This code segment which makes your ImageView to a perfect circle or which angle do you want a rounded ImageView.
Gist:
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));
Markdown://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));
First, you should initialize your ImageView, then you should create a BitMap object and decode which image you want to use. Lastly, you should use setImageBitmap to call your special function. If you want a perfectly circular image, you should send “360” to the pixel parameter.if you have any question, ask me :)