Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Image as Rounded Corner

I've one listview with some items. In my listview i've using custom adapter for displaying the items with images. My images in items are coming from JSON My images are like this -

Needed

Now, I just need the image with rounded corner. How do i achieve this?

like image 678
Praveenkumar Avatar asked Mar 01 '12 13:03

Praveenkumar


People also ask

Can a JPEG have rounded corners?

As soon as you paste your JPG/JPEG picture in the input area, the program will create a new JPG/JPEG but with rounded corners. In the options, you can set the corner radius and select which of the four corners to make round.

How do I make rounded corners of an image in CSS?

The border-radius CSS property is what adds the rounded corners. You can experiment with different values to get it the way you like. border-radius: 75px; If you want it to be a circle, add border-radius: 50%; .


2 Answers

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;
  }

code extracted from http://ruibm.com/?p=184

like image 156
JuanMa Cuevas Avatar answered Oct 09 '22 13:10

JuanMa Cuevas


Bitmap myCoolBitmap = ... ; // <-- Your bitmap you want rounded    
int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();

We have to make sure our rounded corners have an alpha channel in most cases

Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(rounder);    

We're going to apply this paint eventually using a porter-duff xfer mode. This will allow us to only overwrite certain pixels. RED is arbitrary. This could be any color that was fully opaque (alpha = 255)

Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.RED);

We're just reusing xferPaint to paint a normal looking rounded box, the 20.f is the amount we're rounding by.

canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);

Now we apply the 'magic sauce' to the paint

xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
Now apply this bitmap ontop of your image:

canvas.drawBitmap(myCoolBitmap, 0,0, null);
canvas.drawBitmap(rounder, 0, 0, xferPaint);
like image 36
Dmytro Danylyk Avatar answered Oct 09 '22 11:10

Dmytro Danylyk