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:com.amazon.android.utils.Helpers.java

/**
 * Rounds the corners of an image./*  w  w w.ja va 2s  .c  om*/
 *
 * @param activity The activity.
 * @param raw      The raw bitmap image to round.
 * @param round    The radius for the round corners.
 * @return The rounded image.
 */
public static Bitmap roundCornerImage(Activity activity, Bitmap raw, float round) {

    int width = raw.getWidth();
    int height = raw.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(ContextCompat.getColor(activity, android.R.color.black));

    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    canvas.drawRoundRect(rectF, round, round, paint);

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

    return result;
}

From source file:com.android.mail.ui.FolderDisplayer.java

public static void drawFolder(Canvas canvas, float x, float y, int width, int height, String name, int fgColor,
        int bgColor, FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter, Paint paint) {
    canvas.save();/*from www .ja  va  2  s. co  m*/
    canvas.translate(x, y + res.folderVerticalOffset);

    // Draw the box.
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.FILL);
    final RectF rect = new RectF(0, 0, width, height);
    canvas.drawRoundRect(rect, res.folderRoundedCornerRadius, res.folderRoundedCornerRadius, paint);

    // Draw the text based on the language locale and layout direction.
    paint.setColor(fgColor);
    paint.setStyle(Paint.Style.FILL);

    // Compute the text/gradient indices
    final int textLength = (int) paint.measureText(name);
    final int gradientX0;
    final int gradientX1;
    final int textX;

    /***************************************************************************************************
     * width               - the actual folder chip rectangle.                                         *
     * textLength          - the length of the folder's full name (can be longer than                  *
     *                         the actual chip, which is what overflow gradient is for).               *
     * innerPadding        - the padding between the text and the chip edge.                           *
     * overflowPadding     - the padding between start of overflow and the chip edge.                  *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a RTL language                                                                       *
     *                                                                                                 *
     *                   index-0                                                                       *
     *                      |<---------------------------- width ---------------------------->|        *
     *        |<-------------------------textLength------------------>|                       |        *
     *        |             |<----- overflowPadding ----->|                                   |        *
     *        |             |<- innerPadding ->|<-------->|<--------->|<- horizontalPadding ->|        *
     *       textX                            gX1        gX0                                           *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a LTR language.                                                                      *
     *                                                                                                 *
     *     index-0                                                                                     *
     *        |<------------------------------ width ------------------------------->|                 *
     *        |                       |<-------------------------textLength-------------------->|      *
     *        |                                   |<-------- overflowPadding ------->|                 *
     *        |<- horizontalPadding ->|<--------->|<-------->|<- horizontalPadding ->|                 *
     *                              textX        gX0        gX1                                        *
     *                                                                                                 *
     **************************************************************************************************/
    if (formatter.isRtl(name)) {
        gradientX0 = res.overflowGradientPadding;
        gradientX1 = res.folderHorizontalPadding;
        textX = width - res.folderHorizontalPadding - textLength;
    } else {
        gradientX0 = width - res.overflowGradientPadding;
        gradientX1 = width - res.folderHorizontalPadding;
        textX = res.folderHorizontalPadding;
    }

    // Draw the text and the possible overflow gradient
    // Overflow happens when the text is longer than the chip width minus side paddings.
    if (textLength > width - 2 * res.folderHorizontalPadding) {
        final Shader shader = new LinearGradient(gradientX0, 0, gradientX1, 0, fgColor,
                Utils.getTransparentColor(fgColor), Shader.TileMode.CLAMP);
        paint.setShader(shader);
    }
    final int textY = height / 2 - (int) (paint.descent() + paint.ascent()) / 2;
    canvas.drawText(name, textX, textY, paint);
    paint.setShader(null);

    canvas.restore();
}

From source file:net.archenemy.archenemyapp.presenter.BitmapUtility.java

/**
 * Transforms a bitmap into a circle shape.
 * /*  ww  w. j  av a 2 s. co  m*/
 * @param bitmap
 * @param pixels
 * @return bitmap as circle shape
 */
public static Bitmap getCircleBitmap(Bitmap bitmap, int diameterPixels) {
    if (bitmap != null) {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        final 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 = diameterPixels;

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

From source file:Main.java

private static synchronized Bitmap createScaledBitmap(Bitmap bitmap, final int width, final int height,
        float cornerRadius) {

    if (bitmap == null) {
        return null;
    }/*from   w  ww. ja  va2  s. c o m*/

    int adjustedWidth = width;
    int adjustedHeight = height;

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    //int inBytes = bitmap.getByteCount();
    if (width >= bitmapWidth && height >= bitmapHeight)
        return bitmap;

    if (width > 0 && height > 0) {
        //if (width < bitmapWidth || height < bitmapHeight) {
        final float ratio = (float) bitmapWidth / bitmapHeight;

        if (bitmapWidth > bitmapHeight) {
            adjustedHeight = (int) (width / ratio);
        } else if (bitmapHeight > bitmapWidth) {
            adjustedWidth = (int) (height * ratio);
        }

        final Bitmap.Config c = Bitmap.Config.ARGB_8888;
        final Bitmap thumb = Bitmap.createBitmap(width, height, c);
        final Canvas canvas = sScaleCanvas;
        final Paint paint = sPaint;
        canvas.setBitmap(thumb);
        paint.setDither(false);
        paint.setFilterBitmap(true);

        Rect sBounds = new Rect();
        Rect sOldBounds = new Rect();

        sBounds.set((width - adjustedWidth) >> 1, (height - adjustedHeight) >> 1, adjustedWidth,
                adjustedHeight);
        sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);

        if (cornerRadius != 0) {
            //Path p = new Path();
            RectF rect = new RectF(sBounds);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            //p.addRoundRect(rect, cornerRadius, cornerRadius, Direction.CCW);
            //canvas.clipPath(p, Op.REPLACE);
        } else {
            paint.setXfermode(null);
            //canvas.clipRect(0, 0, thumb.getWidth(), thumb.getHeight());
        }

        canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);

        canvas.setBitmap(Bitmap.createBitmap(1, 1, Config.ALPHA_8));

        return thumb;

    }
    return bitmap;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

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

    final float roundPx = 12;

    paint.setAntiAlias(true);/*w  w w.  j a  v  a  2s. c om*/

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

    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips,
        Context context, boolean recycleOrig) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
            context.getResources().getDisplayMetrics());
    final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
            context.getResources().getDisplayMetrics());
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    // prepare canvas for transfer
    paint.setAntiAlias(true);/*from   ww  w .j a va  2s  .c  om*/
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

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

    // draw border
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) borderSizePx);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    if (recycleOrig && bitmap != null && !bitmap.isRecycled())
        bitmap.recycle();

    return output;
}

From source file:ua.com.spacetv.mycookbook.fragments.FragTextRecipe.java

/**
 * Rounding corners on bitmap//from www  .  j a  va  2s  . co m
 *
 * @param bitmap image of recipe
 * @param pixels - radius
 * @return processed bitmap
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    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:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar.//from  www.  j  a v  a 2s  . c o  m
 *
 * @param rawBitmap bitmap to round corners
 */
public static Bitmap publishAvatar(Bitmap rawBitmap, int roundK) {
    if (rawBitmap == null)
        return null;

    try {
        int size = 0;
        if (rawBitmap.getHeight() > rawBitmap.getWidth()) {
            size = rawBitmap.getWidth();
        } else {
            size = rawBitmap.getHeight();
        }
        Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
        final RectF rectF = new RectF(rect);
        final float roundPx = roundK;

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

        rawBitmap.recycle();

        return output;
    } catch (Exception e) {

    }

    return null;
}

From source file:com.efunds.moa.component.customview.process.BackgroundLayout.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./*  w  w w  .ja  va  2s.co  m*/
 *
 * @param filePath file path to avatar image
 */
public static Bitmap publishAvatar(String filePath) {
    Bitmap bitmap = null;

    if (!TextUtils.isEmpty(filePath)) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filePath, opts);
            int size = 0;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                size = bitmap.getWidth();
            } else {
                size = bitmap.getHeight();
            }
            Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
            final RectF rectF = new RectF(rect);
            final float roundPx = 12;

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

            bitmap.recycle();

            return output;
        } catch (Exception e) {

        }
    }
    return null;
}