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.crazyapk.util.bitmap.ImageWorker.java

public 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  ww  w.j  a v a  2  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:com.azhansy.linky.view.PagerSlidingIndicator.java

/**
 * ??//  w w  w.  j av a2  s  . co m
 */
private void drawBackground(Canvas canvas) {
    float left = backgroundStrokeWidth;
    float top = backgroundStrokeWidth;
    float right = getWidth() - backgroundStrokeWidth;
    float bottom = getHeight() - backgroundStrokeWidth;

    mTempRectF.set(left, top, right, bottom);
    float radius = getHeight() / 2.0f;

    // 
    backgroundPaint.setStyle(Style.FILL);
    backgroundPaint.setColor(backgroundColor);
    canvas.drawRoundRect(mTempRectF, radius, radius, backgroundPaint);

    // ??
    backgroundPaint.setStyle(Style.STROKE);
    backgroundPaint.setColor(backgroundStrokeColor);
    canvas.drawRoundRect(mTempRectF, radius, radius, backgroundPaint);
}

From source file:org.mariotaku.twidere.text.OriginalStatusSpan.java

@Override
public void draw(final Canvas canvas, final CharSequence text, final int start, final int end, final float x,
        final int top, final int y, final int bottom, final Paint paint) {
    if (!(paint instanceof TextPaint))
        return;//from   ww  w. j a va 2 s. c o m
    final TextPaint tp = (TextPaint) paint;
    mBounds.left = x;
    mBounds.right = x + paint.measureText(text, start, end) + mPadding * 2;
    mBounds.top = top;
    mBounds.bottom = bottom;
    final int innerTextColor = TwidereColorUtils.getContrastYIQ(tp.linkColor, ThemeUtils.ACCENT_COLOR_THRESHOLD,
            mDarkLightColors[0], mDarkLightColors[1]);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(tp.linkColor);
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    mBounds.inset(-mPaint.getStrokeWidth() / 2, -mPaint.getStrokeWidth() / 2);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(
            ColorUtils.compositeColors(ColorUtils.setAlphaComponent(innerTextColor, 0x80), tp.linkColor));
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    paint.setColor(innerTextColor);
    canvas.drawText(text, start, end, x + mPadding,
            top + (bottom - top) / 2 - (paint.descent() + paint.ascent()) / 2, paint);
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

@Override
protected void onDraw(@NonNull Canvas canvas) {

    mDestination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
            getHeight() - getPaddingBottom());

    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(mDestination, mBackgroundPaint);
    } else {//from w w w . j av a  2s .  c o m
        final float radius = getCalculatedCornerRadius();
        canvas.drawRoundRect(mDestination, radius, radius, mBackgroundPaint);
    }

    if (OUTLINE_DRAW) {
        super.onDraw(canvas);
    } else {
        final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
                contentRight = getWidth() - getPaddingRight(), contentBottom = getHeight() - getPaddingBottom();
        final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
        final int size = Math.min(contentWidth, contentHeight);
        if (mShadowBitmap != null) {
            canvas.drawBitmap(mShadowBitmap, contentLeft + (contentWidth - size) / 2 - mShadowRadius,
                    contentTop + (contentHeight - size) / 2 - mShadowRadius, null);
        }
        Drawable drawable = getDrawable();
        BitmapDrawable bitmapDrawable = null;
        // support state list drawable by getting the current state
        if (drawable instanceof StateListDrawable) {
            if (drawable.getCurrent() != null) {
                bitmapDrawable = (BitmapDrawable) drawable.getCurrent();
            }
        } else if (drawable instanceof BitmapDrawable) {
            bitmapDrawable = (BitmapDrawable) drawable;
        } else if (drawable instanceof ColorDrawable) {
            mSolidColorPaint.setColor(((ColorDrawable) drawable).getColor());
        } else {
            mSolidColorPaint.setColor(0);
        }

        Bitmap bitmap = null;
        if (bitmapDrawable != null) {
            bitmap = bitmapDrawable.getBitmap();
        }
        if (bitmap != null) {
            mSource.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        drawBitmapWithCircleOnCanvas(bitmap, canvas, mSource, mDestination);
    }

    // Then draw the border.
    if (mBorderEnabled) {
        drawBorder(canvas);
    }
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Draw card boarder and white background
 * @param rectf four float rectangle/*from   w  w  w  . ja v  a  2  s  . co  m*/
 * @param canvas canvas to draw on
 * @param cardBorderPaint paint styled for card boarder
 * @param cardFrontPaint paint styled for card front
 */
private void drawCardBackground(final RectF rectf, final Canvas canvas, final Paint cardBorderPaint,
        final Paint cardFrontPaint) {
    rectf.set(0, 0, Card.WIDTH, Card.HEIGHT);
    canvas.drawRoundRect(rectf, mSuitsSizeHalf, mSuitsSizeHalf, cardBorderPaint);
    rectf.set(1, 1, Card.WIDTH - 1, Card.HEIGHT - 1);
    canvas.drawRoundRect(rectf, mSuitsSizeHalf, mSuitsSizeHalf, cardFrontPaint);
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

@Override
protected void onDraw(@NonNull Canvas canvas) {
    mDestination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
            getHeight() - getPaddingBottom());

    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(mDestination, mBackgroundPaint);
    } else {//from  ww w.  j av a 2s . co m
        final float radius = getCalculatedCornerRadius();
        canvas.drawRoundRect(mDestination, radius, radius, mBackgroundPaint);
    }

    if (OUTLINE_DRAW) {
        super.onDraw(canvas);
    } else {
        final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
                contentRight = getWidth() - getPaddingRight(), contentBottom = getHeight() - getPaddingBottom();
        final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
        final int size = Math.min(contentWidth, contentHeight);
        if (mShadowBitmap != null) {
            canvas.drawBitmap(mShadowBitmap, contentLeft + (contentWidth - size) / 2 - mShadowRadius,
                    contentTop + (contentHeight - size) / 2 - mShadowRadius, null);
        }
        Drawable drawable = getDrawable();
        BitmapDrawable bitmapDrawable = null;
        // support state list drawable by getting the current state
        if (drawable instanceof StateListDrawable) {
            if (drawable.getCurrent() != null) {
                bitmapDrawable = (BitmapDrawable) drawable.getCurrent();
            }
        } else if (drawable instanceof BitmapDrawable) {
            bitmapDrawable = (BitmapDrawable) drawable;
        } else if (drawable instanceof ColorDrawable) {
            mSolidColorPaint.setColor(((ColorDrawable) drawable).getColor());
        } else {
            mSolidColorPaint.setColor(0);
        }

        Bitmap bitmap = null;
        if (bitmapDrawable != null) {
            bitmap = bitmapDrawable.getBitmap();
        }
        if (bitmap != null) {
            mSource.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        drawBitmapWithCircleOnCanvas(bitmap, canvas, mSource, mDestination);
    }

    // Then draw the border.
    if (mBorderEnabled) {
        drawBorder(canvas, mDestination);
    }
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 *//*w w  w . jav a  2 s .c  o m*/
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                mBitmapPaint);
    } else {
        final float cornerRadius = getCalculatedCornerRadius();
        canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
    }
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Draw an empty anchor/*  ww w .  ja v  a 2  s.com*/
 * @param canvas canvas to draw on
 * @param x x coordinate of the anchor
 * @param y y coordinate of the anchor
 * @param done anchor done with any movement
 */
public void drawEmptyAnchor(final Canvas canvas, final float x, final float y, final boolean done) {
    RectF pos = new RectF(x, y, x + Card.WIDTH, y + Card.HEIGHT);
    if (!done) {
        canvas.drawRoundRect(pos, mSuitsSizeHalf, mSuitsSizeHalf, mEmptyAnchorPaint);
    } else {
        canvas.drawRoundRect(pos, mSuitsSizeHalf, mSuitsSizeHalf, mDoneEmptyAnchorPaint);
    }
}

From source file:com.ruesga.rview.widget.TagEditTextView.java

private Bitmap createTagChip(Tag tag) {
    // Create the tag string (prepend/append spaces to better ux). Create a clickable
    // area for deleting the tag in non-readonly mode
    String tagText = String.format(" %s " + (mReadOnly || !isEnabled() ? "" : CHIP_REMOVE_TEXT), tag.mTag);

    // Create a new color for the tag if necessary
    if (tag.mColor == 0) {
        if (mChipBackgroundColor == 0) {
            tag.mColor = newRandomColor();
        } else {//from  ww w .j a v  a 2  s. c o m
            tag.mColor = mChipBackgroundColor;
        }
    }
    mChipBgPaint.setColor((isEnabled()) ? tag.mColor : Color.LTGRAY);

    // Measure the chip rect
    Rect bounds = new Rect();
    mChipFgPaint.getTextBounds("|", 0, 1, bounds);
    int minHeight = bounds.height();
    mChipFgPaint.getTextBounds(tagText, 0, tagText.length(), bounds);
    int padding = (int) ONE_PIXEL * 2;
    int w = (int) (mChipFgPaint.measureText(tagText) + (padding * 2));
    int h = Math.max(bounds.height() + (padding * 4), minHeight + (padding * 4));
    float baseline = h / 2 + bounds.height() / 2;

    // Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(w + padding, h + padding, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // Draw the bitmap
    canvas.drawRoundRect(new RectF(0, (padding / 2), w, h), 6, 6, mChipBgPaint);
    canvas.drawText(tagText, (padding / 2), baseline, mChipFgPaint);
    return bitmap;
}

From source file:com.linhnv.apps.maxim.utils.ImageWorker.java

private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

    Bitmap bitmap = scaledBitmap.getBitmap();

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

    int color = 0xff424242;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
    int roundPx = i;
    paint.setAntiAlias(true);/*from  www . j ava  2  s  .  c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    // paint.setColor(Color.BLUE);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    BitmapDrawable finalresult = new BitmapDrawable(mResources, result);
    return finalresult;
}