Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set bitmap in circular imageview?

Tags:

android

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

like image 298
Monali Avatar asked May 04 '11 10:05

Monali


1 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);
like image 86
pumpkee Avatar answered Nov 15 '22 22:11

pumpkee