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 input, int dips, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    //make sure that our rounded corner is scaled appropriately
    final float roundPx = dips * densityMultiplier;

    paint.setAntiAlias(true);/* www.java 2s . c  o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    //draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, 0, 0, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    //make sure that our rounded corner is scaled appropriately
    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);//  w w  w  . j  a v a2  s . c o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    //draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, 0, 0, paint);

    return output;
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2 - 5;// www  . ja v  a  2s . co m
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2 - 5;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst_left + 15, dst_top + 15, dst_right - 20, dst_bottom - 20);

    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, src, dst, paint);
    return output;
}

From source file:Main.java

/**
 * Returns semi-rounded bitmap. This is used for displaying atn promotion images.
 * //from   w  w w.j  a  v  a 2 s . co  m
 * @param context
 * @param input
 * @return
 */
public static Bitmap getSemiRoundedBitmap(Context context, Bitmap input) {
    Bitmap output = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, input.getWidth(), input.getHeight());
    final RectF rectF = new RectF(rect);
    //make sure that our rounded corner is scaled appropriately
    final float roundPx = densityMultiplier * 10;
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    //draw rectangles over the corners we want to be square
    canvas.drawRect(input.getWidth() / 2, 0, input.getWidth(), input.getHeight() / 2, paint);
    canvas.drawRect(input.getWidth() / 2, input.getHeight() / 2, input.getWidth(), input.getHeight(), paint);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, 0, 0, paint);
    input.recycle();

    return output;
}

From source file:com.chalmers.feedlr.adapter.FeedAdapter.java

/**
 * //  ww  w  . j av a  2 s  .  c  o m
 * @param squareBitmap
 *            original image
 * @return image with rounded corners
 */
public static Bitmap getRoundedCornerBitmap(Bitmap squareBitmap) {
    Bitmap roundedBitmap = Bitmap.createBitmap(squareBitmap.getWidth(), squareBitmap.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, squareBitmap.getWidth(), squareBitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 8;

    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(squareBitmap, rect, rect, paint);

    return roundedBitmap;
}

From source file:Main.java

/**
 * Return rounded bitmap with top left corner as rounded using specified bitmap and bitmap size in pixels.
 * /*from   ww  w  .jav a 2 s .  c o m*/
 * @param bitmap to make rounded
 * @param pixels size of bitmap
 * @return slightly rounded bitmap.
 */
public static Bitmap getRoundedTopLeftCornerBitmap(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;
    final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2);
    final Rect bottomRect = new Rect(0, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    // Fill in upper right corner
    canvas.drawRect(topRightRect, paint);
    // Fill in bottom corners
    canvas.drawRect(bottomRect, paint);

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

    return output;
}

From source file:Main.java

/**
 * Gets the rounded corner bitmap.//from www . j a v a2 s.c  o  m
 * 
 * @param bitmap
 *            the bitmap
 * @return the rounded corner bitmap
 */
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, Boolean create_circle) {
    DisplayMetrics mMetrics = context.getResources().getDisplayMetrics();
    float mScaleFactor = mMetrics.density;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = Color.BLACK;
    final Paint paint = new Paint();
    int width = bitmap.getWidth();
    int height = (bitmap.getHeight() > width) ? width : bitmap.getHeight();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);
    final float roundPx = (create_circle) ? (bitmap.getWidth() > 360) ? bitmap.getWidth() : 360 : 2;

    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    // draw border
    paint.setColor(Color.parseColor("#cccccc"));
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(0.5F * mScaleFactor);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    return output;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float pixels) {
    if (bitmap == null) {
        Log.e(TAG, "getRoundedCornerBitmap - null bitmap");
        return null;
    }/*from w w w.ja v a  2 s .c  o m*/

    Bitmap 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);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, pixels, pixels, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    bitmapResult = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0 + 5, 0 + 5, w - 5, h - 5);
    final RectF rectF = new RectF(rect);

    // make sure that our rounded corner is scaled appropriately

    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);/* w ww.j a v a 2s .  c  o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawColor(Color.BLACK);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    // draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    // paint.setXfermode(new AvoidXfermode(Color.WHITE, 255,
    // AvoidXfermode.Mode.TARGET));
    canvas.drawBitmap(input, 0, 0, paint);

    return bitmapResult;
}

From source file:org.jared.synodroid.ds.utils.Utils.java

/**
 * Create a rounded bitmap/*from ww w  .  j a v  a  2s  .  co m*/
 * 
 * @param bitmap
 *            The original bitmap
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    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);

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