I know there are a lot of examples on this question but l still couldn't finish following them, Is there an easy way how to make circle ImageView on android studio?
Asked
Active
Viewed 2,956 times
3 Answers
9
You not need to use any external libraries for make ImageView
circle. Use this class for make your ImageView
circle.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
public class CircleImage {
Context context;
public RoundImage(Context context) {
this.context = context;
}
public Bitmap transform(Bitmap source) {
try {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap
.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
// source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size,
squaredBitmap.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
// canvas.drawArc(rectf, -90, 360, false, lightRed);
// squaredBitmap.recycle();
return bitmap;
} catch (Exception e) {
// TODO: handle exception
}
return BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
}
}
And use your ImageView in Java file
your_imageview.setImageBitmap(new RoundImage(
getApplicationContext()).transform(your_image_bitmap));

Jaydip
- 592
- 5
- 27
0
Use below code to make Bitmap
to Circle Bitmap
for ImageView
:
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
Bitmap result = null;
try {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int pl = 1;
int pr = 1;
int pt = 1;
int pb = 1;
int usableWidth = w - (pl + pr);
int usableHeight = h - (pt + pb);
int radius = Math.min(usableWidth, usableHeight) / 2;
int cx = pl + (usableWidth / 2);
int cy = pt + (usableHeight / 2);
canvas.drawCircle(cx, cy, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
} catch (NullPointerException e) {
} catch (OutOfMemoryError o) {
}
return result;
}

Satan Pandeya
- 3,747
- 4
- 27
- 53

Jotiram Chavan
- 375
- 1
- 8