Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make an ImageView in Circular Shape? [duplicate]

I want to do something like that.

image in List view

This is a list view row with name and the image of user.

I have done some searching and have done the image circular,but not the perfect solution. Any help will be helping me.

my code added to the Image Loader class

public Bitmap processBitmap(Bitmap bitmap) {     int pixels = 0;     if (mRound == 0)         pixels = 120;     else         pixels = mRound;     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),             bitmap.getHeight(), 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(Mode.SRC_IN));     canvas.drawBitmap(bitmap, rect, rect, paint);      return output; } 

Thanks.

like image 735
shailesh Avatar asked Aug 22 '13 11:08

shailesh


People also ask

How do I make an image round in Android?

Simply put a circular_crop. png in your drawable folder which is in the shape of your image dimensions (a square in my case) with a white background and a transparent circle in the center. You can use this image if you have want a square imageview.

How do I turn an ImageView into a bitmap?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap();

What is difference between ImageView and ImageButton?

ImageButton has the same property as ImageView . Only one feature is extra, which is, images set through ImageButton are clickable, and actions can be attached with them upon clicking. Just like a button, the setOnClickListener() can be used to set an event listener for click event on this.


1 Answers

you can use this function also ... Its work for me..

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {     int targetWidth = 50;     int targetHeight = 50;     Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,                          targetHeight,Bitmap.Config.ARGB_8888);      Canvas canvas = new Canvas(targetBitmap);     Path path = new Path();     path.addCircle(((float) targetWidth - 1) / 2,         ((float) targetHeight - 1) / 2,         (Math.min(((float) targetWidth),          ((float) targetHeight)) / 2),         Path.Direction.CCW);      canvas.clipPath(path);     Bitmap sourceBitmap = scaleBitmapImage;     canvas.drawBitmap(sourceBitmap,          new Rect(0, 0, sourceBitmap.getWidth(),         sourceBitmap.getHeight()),          new Rect(0, 0, targetWidth, targetHeight), null);     return targetBitmap; } 
like image 98
Piyush Avatar answered Sep 16 '22 23:09

Piyush