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.inetgoes.fangdd.view.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        //paint.setColor(Color.GRAY);
        paint.setColor(DEFAULT_COLOR_THEME);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);//from  ww  w .  j  a va2 s  .  c o m
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int radius;
        //radius = backRect.height() / 2 - RIM_SIZE;
        radius = backRect.height() / 2;
        //paint.setColor(Color.GRAY);
        paint.setColor(DEFAULT_COLOR_THEME);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}

From source file:com.audionote.widget.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        paint.setColor(color_back_theme);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);/*from   w  w  w  .  ja v  a  2 s . c  om*/
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, borderWidth, frontRect_left + backRect.height() - 2 * borderWidth,
                backRect.height() - borderWidth);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int backBorderRadius = backBorderRect.height() / 2;
        int backRadius = backBorderRadius - borderWidth;

        // 
        paint.setColor(border_color);
        backBorderCircleRect.set(backBorderRect);
        canvas.drawRoundRect(backBorderCircleRect, backBorderRadius, backBorderRadius, paint);

        // 
        paint.setColor(color_back_theme);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, backRadius, backRadius, paint);

        // ??
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backBorderCircleRect, backBorderRadius, backBorderRadius, paint);

        // ?
        frontBorderRect.set(frontRect_left, 0, frontRect_left + backBorderRect.height(),
                backBorderRect.height());
        frontBorderCircleRect.set(frontBorderRect);
        paint.setColor(border_color);
        canvas.drawRoundRect(frontBorderCircleRect, backBorderRadius, backBorderRadius, paint);

        // ?
        frontRect.set(frontRect_left + borderWidth, borderWidth,
                frontRect_left + backBorderRect.height() - borderWidth, backBorderRect.height() - borderWidth);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, backRadius, backRadius, paint);
    }
}

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

private void updateShadowBitmap() {
    if (USE_OUTLINE)
        return;//from www  .  j av  a2 s . co m
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFF000000 | mBackgroundPaint.getColor());
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(rect, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawOval(rect, paint);
    } else {
        final float cr = getCalculatedCornerRadius();
        canvas.drawRoundRect(rect, cr, cr, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawRoundRect(rect, cr, cr, paint);
    }
    invalidate();
}

From source file:org.mariotaku.twidere.view.ShapedImageView.java

private void updateShadowBitmap() {
    if (useOutline())
        return;/* w w w.  j a  v a  2s  .  c om*/
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFF000000 | mBackgroundPaint.getColor());
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(rect, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawOval(rect, paint);
    } else {
        final float cr = getCalculatedCornerRadius();
        canvas.drawRoundRect(rect, cr, cr, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawRoundRect(rect, cr, cr, paint);
    }
    invalidate();
}

From source file:com.adkdevelopment.e_contact.utils.UnderlinePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//from   ww  w .ja  v  a2  s.c  o m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    mRectF.set(left, top, right, bottom);
    canvas.drawRoundRect(mRectF, mCornerRadius, mCornerRadius, mPaint);
}

From source file:com.e.common.widget.viewpager.UnderlinePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;/*from ww w .j a v a  2s.  c  o m*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    if (isRound) {
        int radius = getHeight() / 2;
        canvas.drawRoundRect(new RectF(left, top, right, bottom), radius, radius, mPaint);
    } else {
        canvas.drawRect(left, top, right, bottom, mPaint);
    }
}

From source file:lollipop.iconics.IconicsDrawable.java

@Override
public void draw(Canvas canvas) {
    if (mIcon != null || mPlainIcon != null) {
        final Rect viewBounds = getBounds();

        updatePaddingBounds(viewBounds);
        updateTextSize(viewBounds);/*from w w  w . ja  v  a 2  s.c o  m*/
        offsetIcon(viewBounds);

        if (mBackgroundPaint != null && mRoundedCornerRy > -1 && mRoundedCornerRx > -1) {
            canvas.drawRoundRect(new RectF(0, 0, viewBounds.width(), viewBounds.height()), mRoundedCornerRx,
                    mRoundedCornerRy, mBackgroundPaint);
        }

        mPath.close();

        if (mDrawContour) {
            canvas.drawPath(mPath, mContourPaint);
        }

        mIconPaint.setAlpha(mAlpha);

        canvas.drawPath(mPath, mIconPaint);
    }
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ??//from   ww w . j av a  2s .c  o  m
 * 
 * @param bitmap
 *            Bitmap
 * @return Bitmap
 */
public static Bitmap getRoundBitmap(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;
        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;
        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, 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);

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

From source file:cn.com.hgh.view.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        //         paint.setColor(Color.GRAY);
        paint.setColor(0xffededed);//
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);/*ww w  .  j a v  a 2s  . co  m*/
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        int radius;
        radius = backRect.height() / 2 - RIM_SIZE;
        //         paint.setColor(Color.GRAY);//
        paint.setColor(0xffededed);//

        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(Color.BLACK);
        paint.setTextSize(AbViewUtil.sp2px(getContext(), 15));
        //          paint.setAlpha(alpha);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);

        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
        if (isOpen) {

            //            canvas.drawText("?", radius, radius + 23, paint);//
            paint.setColor(Color.BLACK);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("?", width / 4 - RIM_SIZE, radius + 23, paint);//

        } else {
            //            canvas.drawText("?", getMeasuredWidth() - radius - 40 * 3,radius + 23, paint);
            paint.setColor(Color.BLACK);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("?", width - frontCircleRect.width() / 2 - RIM_SIZE, radius + 23, paint);
        }

        if (isOpen) {
            paint.setColor(0xff14c4c4);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.getTextBounds("?", 0, "?".length(), frontRect);
            paint.setTextSize(AbViewUtil.sp2px(getContext(), 15));
            //              paint.setAlpha(alpha1);
            //            canvas.drawText("?", getMeasuredWidth() - radius - 40 * 3,radius + 23, paint);
            canvas.drawText("?", frontCircleRect.centerX(), radius + 23, paint);

        } else {

            paint.setColor(0xff14c4c4);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.getTextBounds("?", 0, "?".length(), frontRect);
            paint.setTextSize(AbViewUtil.sp2px(getContext(), 15));
            //            paint.setAlpha(alpha1);
            //            canvas.drawText("?", radius, radius + 23, paint);
            canvas.drawText("?", frontCircleRect.centerX(), radius + 23, paint);

        }

    }
}

From source file:org.mozilla.gecko.GeckoAppShell.java

static private Bitmap getLauncherIcon(Bitmap aSource) {
    final int kOffset = 6;
    final int kRadius = 5;
    int kIconSize;
    int kOverlaySize;
    switch (getDpi()) {
    case DisplayMetrics.DENSITY_MEDIUM:
        kIconSize = 48;/*from   ww  w .  ja v a  2s . com*/
        kOverlaySize = 32;
        break;
    case DisplayMetrics.DENSITY_XHIGH:
        kIconSize = 96;
        kOverlaySize = 48;
        break;
    case DisplayMetrics.DENSITY_HIGH:
    default:
        kIconSize = 72;
        kOverlaySize = 32;
    }

    Bitmap bitmap = Bitmap.createBitmap(kIconSize, kIconSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // draw a base color
    Paint paint = new Paint();

    if (aSource == null) {
        float[] hsv = new float[3];
        hsv[0] = 32.0f;
        hsv[1] = 1.0f;
        hsv[2] = 1.0f;
        paint.setColor(Color.HSVToColor(hsv));
        canvas.drawRoundRect(new RectF(kOffset, kOffset, kIconSize - kOffset, kIconSize - kOffset), kRadius,
                kRadius, paint);
    } else {
        int color = BitmapUtils.getDominantColor(aSource);
        paint.setColor(color);
        canvas.drawRoundRect(new RectF(kOffset, kOffset, kIconSize - kOffset, kIconSize - kOffset), kRadius,
                kRadius, paint);
        paint.setColor(Color.argb(100, 255, 255, 255));
        canvas.drawRoundRect(new RectF(kOffset, kOffset, kIconSize - kOffset, kIconSize - kOffset), kRadius,
                kRadius, paint);
    }

    // draw the overlay
    Bitmap overlay = BitmapFactory.decodeResource(GeckoApp.mAppContext.getResources(), R.drawable.home_bg);
    canvas.drawBitmap(overlay, null, new Rect(0, 0, kIconSize, kIconSize), null);

    // draw the bitmap
    if (aSource == null)
        aSource = BitmapFactory.decodeResource(GeckoApp.mAppContext.getResources(), R.drawable.home_star);

    if (aSource.getWidth() < kOverlaySize || aSource.getHeight() < kOverlaySize) {
        canvas.drawBitmap(aSource, null,
                new Rect(kIconSize / 2 - kOverlaySize / 2, kIconSize / 2 - kOverlaySize / 2,
                        kIconSize / 2 + kOverlaySize / 2, kIconSize / 2 + kOverlaySize / 2),
                null);
    } else {
        canvas.drawBitmap(aSource, null,
                new Rect(kIconSize / 2 - aSource.getWidth() / 2, kIconSize / 2 - aSource.getHeight() / 2,
                        kIconSize / 2 + aSource.getWidth() / 2, kIconSize / 2 + aSource.getHeight() / 2),
                null);
    }

    return bitmap;
}