I have a ListView
in which there is an ImageView
, the image in the ImageView
gets loaded dynamically after its fetched from the server.
Now, I want these images, of any size, to fit into a circular frame, how to do that?
Here's a sample pic of what I want
Asked
Active
Viewed 4.9k times
25

Niranj Patel
- 32,980
- 10
- 97
- 133

Sanghita
- 1,307
- 3
- 16
- 28
-
1Try this. http://stackoverflow.com/questions/5882180/how-to-set-bitmap-in-circular-imageview hope it will help you – Dec 27 '12 at 08:06
-
Does this answer your question? [How to set bitmap in circular imageview?](https://stackoverflow.com/questions/5882180/how-to-set-bitmap-in-circular-imageview) – tripleee Jan 24 '20 at 06:30
7 Answers
24
With the help of previous answer I came up with this solution.Hope it help others:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;
public class CircleImage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_layout);
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.hair_four);
Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
img1.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
}
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
Bitmap result = null;
try {
result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, 200, 200);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(50, 50, 50, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
} catch (NullPointerException e) {
} catch (OutOfMemoryError o) {
}
return result;
}
}
-
4For a slightly more efficient implemention, refer to Romain Guy's recent [**Android Recipe #1, image with rounded corners**](http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/) blog post. The main difference is it uses a `BitmapShader` to directly control the visible 'textured' area, which simplifies the drawing logic to a single draw call. – MH. Dec 27 '12 at 09:17
-
Hey Sanghita ,I came across the same requirement and happened to use your code though it works fine as for displaying the image i have a issue.I replaced the following line of code with mine to increase the image view as canvas.drawCircle(90, 90,90, paint); since canvas.drawCircle(50, 50,50, paint); gave a small display view.However only 1 quarter of the image was being displayed in the imageview.Can u please help? – Raj May 02 '13 at 12:40
-
change the following lines as: Bitmap resized = Bitmap.createScaledBitmap(bm, 600, 600, true); Bitmap conv_bm = getRoundedRectBitmap(resized, 600); result = Bitmap.createBitmap(950, 950, Bitmap.Config.ARGB_8888); Rect rect = new Rect(0, 0, 650, 650); canvas.drawCircle(300, 300, 300, paint); – Sanghita May 02 '13 at 13:05
-
@Sanghita,is it necesssary to specify the dimensions here.Bcoz i am targetting multiple device sizes i dont want to set the dimensions as u mentioned since it takes up fixed size.Can the image be set as per the size of the imageview defined in the xml file?Also i changed to this canvas.drawCircle(130, 130,130, paint); But i dont understand how to manipulate other parameters ;doing so does not render entire image. – Raj May 03 '13 at 11:25
-
@joy-since the circle is created through coding match_parent/wrap_content will not be available,changing only your canvas size doesnot effect the image's size – Sanghita May 03 '13 at 12:02
-
@Sanghita,you are right.However when i try to set the image into this circle only part of the image is displayed into that circle after i modified the other values.So i asked how to identify what parameter values do i need to set.like in for Bitmap resized = Bitmap.createScaledBitmap(bm, 600, 600, true); Bitmap conv_bm = getRoundedRectBitmap(resized, 600); result = Bitmap.createBitmap(950, 950, Bitmap.Config.ARGB_8888); Rect rect = new Rect(0, 0, 650, 650);if the canvas drawn is canvas.drawCircle(130, 130,130, paint); – Raj May 03 '13 at 12:43
-
i want to get the circle bigger in size with image also bigger how can i do that. – Mayur May 15 '14 at 07:12
-
-
@sanghita - can you guys please explain how to change the size of the circle been drawn with this code ! the image cropping and everything else is perfect but cant understand how to increase or decrease the radius of the circle – Khay Jun 04 '15 at 11:30
-
the image is positioned from the top left. how do I center the image? I want it to be scaleType:centerCrop. – stanley santoso Aug 26 '15 at 14:17
-
9
Try this code:
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
try {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
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);
} catch (NullPointerException e) {
// return bitmap;
} catch (OutOfMemoryError o){}
return result;
}
If you want an actual circle then you can pass 100px
as parameter.

Ricky Khatri
- 952
- 2
- 16
- 42
-
thanks a ton for your help , the solution you provided was for pictures in a rounder corners rectangle without size modification.it needed just a few lines of changes – Sanghita Dec 27 '12 at 08:10
-
@ricintech- can you guys please explain how to change the size of the circle been drawn with this code ! the image cropping and everything else is perfect but cant understand how to increase or decrease the radius of the circle – Khay Jun 04 '15 at 11:29
8
Update
There is a CircleImageView
available on Github.
You can get latest version from Maven repository as add as a gradle dependency.

S.D.
- 29,290
- 3
- 79
- 130
4
There are lots of tutorial regarding this. I think it will help.
https://github.com/lopspower/CircularImageView

Yogendra
- 4,817
- 1
- 28
- 21
1
We can manage the height and width of an image from xml code and draw circle/oval from java code like
<ImageView
android:id="@+id/imageView1"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
/>
for oval view
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.user_image);
Bitmap conv_bm = getRoundedBitmap(bm);
img1.setImageBitmap(conv_bm);
public static Bitmap getRoundedBitmap(Bitmap bitmap)
{
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}

ranjan
- 136
- 1
- 12
0
Add following dependancies
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
implementation 'de.hdodenhof:circleimageview:3.0.0'
CircularImageView available for image fit in circle also if image is not looking proper .resize is for image resizing in circular image view.
CircleImageView img;
String Imageid;
Imageid="ImageName"; //String is not compulsary it may be drawable
Picasso.with(mContext)
.load(Imageid.get(position)) //Load the image
.error(R.drawable.ic_launcher_background) //Image resource for error
.resize(20, 20) // Post processing - Resizing the image
.into(img); // View where image is loaded.

Madhuri Pawar
- 51
- 2
- 2
-1
public static Bitmap getCircleBitmap(Bitmap bitmap) {
final Bitmap circuleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getWidth(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(circuleBitmap);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return circuleBitmap;
}

Biswajit Karmakar
- 9,799
- 4
- 39
- 41