Home » Android » android – How to set bitmap in circular imageview?

android – How to set bitmap in circular imageview?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have 1 circular imageview and I want to place bitmap inside the imageview.
but when I set the compressed bitmap image it is always rectangle.
Please help me to set bitmap in circular imageview.

Thanks

How to&Answers:

I am curious about how you created a circular ImageView. Can you share that secret ??
As far as creating a circular Bitmap is concerned, create a BitmapShader from the bitmap you want to show. Then create a ShapeDrawable (Oval) and assign the bitmap shader to it. Draw the drawable. Bam! circular image!

Bitmap bitmap = getthebitmapyouwanttoshowinacirclefromsomewhere;
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
        paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);

myImageView.setImageBitmap(circleBitmap);