Example usage for android.graphics Canvas drawRoundRect

List of usage examples for android.graphics Canvas drawRoundRect

Introduction

In this page you can find the example usage for android.graphics Canvas drawRoundRect.

Prototype

public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) 

Source Link

Document

Draw the specified round-rect using the specified paint.

Usage

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, int color) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    color = 0xff424242; // FIXME
    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 = convertDipsToPixels(context, ROUND_DIPS);

    paint.setAntiAlias(true);//ww w .  j  av a2  s .c  om
    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;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

    paint.setAntiAlias(true);// ww  w .  j  av  a  2  s .  c o m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    try {/*from  www.jav a 2 s.co  m*/
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        final float roundPx = 15;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
    try {//from w ww  .j  a va 2  s.c  o m
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        final float roundPx = 14;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null)
        return bitmap;
    maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

    paint.setAntiAlias(true);//from  w w  w. j  av  a2 s . c  om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

/**
 * Create a rounded corner bitmap from current bitmap
 *
 * @param bitmap/* w  w  w .j a  v a  2 s. c  om*/
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    try {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        final float roundPx = 50;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

/**
 * Creates a bitmap with rounded corners
 *
 * @param bitmap    source bitmap//ww w.j  av a 2  s  .  c om
 * @param maxHeight maximal height for result bitmap
 * @param pixels    corner radius
 * @return a new bitmap if succeed, nil instead
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null || sApplicationContext == null) {
        return bitmap;
    }
    maxHeight = (int) (sApplicationContext.getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

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

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

    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 = pixels;

    paint.setAntiAlias(true);//from  w  w  w.  j av  a 2 s .  com
    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;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    if (bitmap == null)
        return null;
    Bitmap output;/*ww w .ja v  a  2 s  . c  o  m*/
    try {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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 = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (Exception ex) {

        //adding as a precaution because finding a few crashes in bitmap modification
        return bitmap;
    }
    return output;
}

From source file:Main.java

static private Bitmap smallCoverPostProc(Bitmap smallBitmap) {
    try {/*from w w w . j av  a2s .  c om*/
        Bitmap smallBitmapPostProc = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas();
        canvas.setBitmap(smallBitmapPostProc);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap bitmapToShade = Bitmap.createBitmap(smallBitmap, 2, 2, smallBitmap.getWidth() - 4,
                smallBitmap.getHeight() - 4);
        BitmapShader bmShader = new BitmapShader(bitmapToShade, TileMode.CLAMP, TileMode.CLAMP);
        paint.setShader(bmShader);
        canvas.drawRoundRect(new RectF(2, 2, smallBitmap.getWidth() - 2, smallBitmap.getHeight() - 2), 4, 4,
                paint);
        return smallBitmapPostProc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}