I want to transform a normal rectangular image to a circular image programmatically , but not using xml , see this link Navigation Drawer (Google+ vs. YouTube). So far I followed this link How to set bitmap in circular imageview?, but I just got it to paint dark circle on my image . What should I do?
Asked
Active
Viewed 1,582 times
2 Answers
0
Bitmap bitmap = BitmapFactory.decodeResource(convertView.getResources(), R.drawable.ic_launcher);
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);
imgProfilePic.setImageBitmap(circleBitmap);

Saeed-rz
- 1,435
- 1
- 20
- 38
0
public Bitmap dstBmp;
Bitmap getRoundedBitmapnow(Bitmap bitmap)
{
// convert rectangle to square
if (bitmap.getWidth() >= bitmap.getHeight()){
dstBmp = Bitmap.createBitmap(
bitmap,
bitmap.getWidth()/2 - bitmap.getHeight()/2,
0,
bitmap.getHeight(),
bitmap.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
bitmap,
0,
bitmap.getHeight()/2 - bitmap.getWidth()/2,
bitmap.getWidth(),
bitmap.getWidth()
);
}
// create circle
Bitmap output = Bitmap.createBitmap(dstBmp.getWidth(),
dstBmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect1 = new Rect(0, 0, dstBmp.getWidth(), dstBmp.getHeight());
final RectF rectF1 = new RectF(rect1);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF1, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(dstBmp, rect1, rect1, paint);
return output;
}

Jossy Paul
- 1,267
- 14
- 26