1

I refer to this question which links to this article on how to create rounded corners on an image.

It works fine for image I am downloading from the web, but when I read the image from the Resources/Drawable folder the image is not getting rounded.

When getting image from the web I use:

Bitmap img = BitmapFactory.decodeStream(inputStream);

And when decoding from resources I use:

Bitmap img = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.profile_photo);

When decoding from resources the returned bitmap is not null.

Any ideas on where I am going wrong with this one?

Community
  • 1
  • 1
jim
  • 8,670
  • 15
  • 78
  • 149

2 Answers2

0

Add a "shape"(XML in drawable).

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#ffffffff"/>    

<stroke android:width="3dp"
        android:color="#ff000000"
        />

<padding android:left="1dp"
         android:top="1dp"
         android:right="1dp"
         android:bottom="1dp"
         /> 

<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
 android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 

& use this shape with your drawable. or use this code

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
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 = 12;

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;
}
Kamal
  • 1,415
  • 13
  • 24
0

I just figured out the problem was due to the input images being different resolutions.

The image coming from resources was much bigger than the web and therefore I needed to increase the radius size to get the rounding effect.

jim
  • 8,670
  • 15
  • 78
  • 149