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 roundCorners(final Bitmap source, final float radius) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w  w  w.  jav a2 s.  co  m
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

public static Bitmap roundCorners(final Bitmap bitmap, final int radiusX, final int radiusY) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  www .j a  v  a 2 s .  c o  m*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radiusX, radiusY, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    return rounded;
}

From source file:Main.java

private static void drawAndroidBody(Canvas canvas, Paint paint, float w, float h) {
    paint.setColor(Color.parseColor("#43A047"));
    canvas.drawRoundRect(new RectF(-w / 2, 0, w / 2, h), w / 10, w / 10, paint);
}

From source file:Main.java

public static Bitmap formatUserAvatar(Bitmap photo) {
    int maskColor = 0xff424242;
    Paint cornerPaint = new Paint();
    cornerPaint.setAntiAlias(true);/*from  w w  w . jav  a  2s  . co  m*/
    cornerPaint.setColor(maskColor);
    Rect roundedCornerRect = new Rect(0, 0, 256, 256);
    RectF roundedCornerRectF = new RectF(roundedCornerRect);
    Bitmap roundedCornerBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    Canvas roundedCornerCanvas = new Canvas(roundedCornerBitmap);
    roundedCornerCanvas.drawARGB(0, 0, 0, 0);
    roundedCornerCanvas.drawRoundRect(roundedCornerRectF, 128, 128, cornerPaint);
    cornerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(photo, 256, 256, true);
    roundedCornerCanvas.drawBitmap(scaledBitmap, roundedCornerRect, roundedCornerRect, cornerPaint);
    return roundedCornerBitmap;
}

From source file:Main.java

public static void drawRoundRect(@NonNull Canvas canvas, float left, float top, float right, float bottom,
        float rx, float ry, @NonNull Paint paint) {

    if (Build.VERSION.SDK_INT < 21) {
        TMP_RECT.set(left, top, right, bottom);
        canvas.drawRoundRect(TMP_RECT, rx, ry, paint);
    } else {/*w w w  .  jav  a 2s  .  c  o m*/
        canvas.drawRoundRect(left, top, right, bottom, rx, ry, paint);
    }
}

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
    Bitmap bmp;/*w ww  .ja v  a 2  s .c  om*/

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radiusDP,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop,
        int leftBottm, int rightBottom) {
    Bitmap bmp;// www. j ava  2s .  c  o m

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftTop,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);
    return bmp;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bmp, float roundDP) {
    //      roundDP *= Constants.screen_density;
    Bitmap bmpOut = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
    Canvas c = new Canvas(bmpOut);
    final Paint p = new Paint();
    final RectF rectF = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());

    p.setAntiAlias(true);/* w w  w  .  j  av a2 s.c  o m*/
    c.drawARGB(0, 0, 0, 0);
    p.setColor(Color.BLACK);
    c.drawRoundRect(rectF, roundDP, roundDP, p);
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    c.drawBitmap(bmp, 0, 0, p);
    return bmpOut;
}

From source file:Main.java

public static Bitmap bitmap2Round(Bitmap bitmap, float roundPx) {
    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(rect);
    paint.setAntiAlias(true);//from   w  w w  . j a  va  2  s  .  c o m
    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);
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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(rect);

    paint.setAntiAlias(true);//w  w  w .ja  va  2  s .c om
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, paint);

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