Skip to content Skip to sidebar Skip to footer

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 -

Solution 1:

publicstatic Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmapoutput= Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
    Canvascanvas=newCanvas(output);

    finalintcolor=0xff424242;
    finalPaintpaint=newPaint();
    finalRectrect=newRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    finalRectFrectF=newRectF(rect);
    finalfloatroundPx=12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
  }

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

Solution 2:

BitmapmyCoolBitmap= ... ; // <-- Your bitmap you want rounded    intw= myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();

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

Bitmaprounder= Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(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)

PaintxferPaint=newPaint(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);

Solution 3:

publicstatic Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {

    Bitmapoutput= Bitmap.createBitmap(bitmap.getWidth(), bitmap
            .getHeight(), Config.ARGB_8888);
    Canvascanvas=newCanvas(output);

    finalintcolor=0xff424242;
    finalPaintpaint=newPaint();
    finalRectrect=newRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    finalRectFrectF=newRectF(rect);
    finalfloatroundPx= pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

Solution 4:

Check out this previous thread, brilliant answer on how to get rounded edges for bitmap images.

Post a Comment for "Changing Image As Rounded Corner"