Applies rounded corners to the specified Bitmap . - Android Graphics

Android examples for Graphics:Bitmap Round Corner

Description

Applies rounded corners to the specified Bitmap .

Demo Code


//package com.java2s;
import static android.graphics.Bitmap.Config.ARGB_8888;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;

public class Main {
    /**/*w  w w  .  ja v  a  2s  .c om*/
     * Applies rounded corners to the specified {@code Bitmap}.
     * @param bitmap the source {@code Bitmap}, can and should be {@code recycle()}ed if not needed after this.
     * @return the output {@code Bitmap}.
     * @source http://stackoverflow.com/questions/2459916/how-to-make-an-imageview-to-have-rounded-corners
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Bitmap output = Bitmap.createBitmap(width, height, 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 = Math.max(width, height) / 10.0f;

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

Related Tutorials