Example usage for android.graphics Canvas drawCircle

List of usage examples for android.graphics Canvas drawCircle

Introduction

In this page you can find the example usage for android.graphics Canvas drawCircle.

Prototype

public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) 

Source Link

Document

Draw the specified circle using the specified paint.

Usage

From source file:com.zhi.android.libs.pager.PageIndicatorView.java

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

    // Get the center point location.
    final float centerX = (getRight() - getLeft()) / 2;
    final float centerY = (getBottom() - getTop()) / 2;

    // Calculate the start offset relative the start
    final float offset = centerX - (mIndicatedPages / 2 - ((mIndicatedPages & 1) == 0 ? 0.5f : 0f)) * mSpacing;

    // draw normal indicator.
    mPaint.setColor(mNormalColor);//w ww.j  ava2 s  . c o  m
    for (int i = 0; i < mIndicatedPages; i++) {
        canvas.drawCircle(offset + i * mSpacing, centerY, mRadius, mPaint);
    }
    // draw selected indicator.
    mPaint.setColor(mSelectedColor);
    canvas.drawCircle(offset + (mIndicatedPosition + mIndicatedOffset) * mSpacing, centerY, mRadius, mPaint);
}

From source file:com.github.dfa.diaspora_android.ui.BadgeDrawable.java

@Override
public void draw(Canvas canvas) {
    if (!shouldDraw) {
        return;/* ww  w  . j ava 2s  . co m*/
    }
    Rect bounds = getBounds();
    float width = bounds.right - bounds.left;
    float height = bounds.bottom - bounds.top;

    // Position the badge in the top-right quadrant of the icon.
    float radius = ((Math.max(width, height) / 2)) / 2;
    float centerX = (width - radius - 1) + 5;
    float centerY = radius - 5;
    if (badgeValue.length() <= 2) {
        // Draw badge circle.
        canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), badgeStroke);
        canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), badgeBackground);
    } else {
        canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), badgeStroke);
        canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), badgeBackground);
        //canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, badgeBackground);
    }
    // Draw badge count message inside the circle.
    badgeText.getTextBounds(badgeValue, 0, badgeValue.length(), textRect);
    float textHeight = textRect.bottom - textRect.top;
    float textY = centerY + (textHeight / 2f);
    if (badgeValue.length() > 2)
        canvas.drawText(BADGE_VALUE_OVERFLOW, centerX, textY, badgeText);
    else
        canvas.drawText(badgeValue, centerX, textY, badgeText);
}

From source file:com.github.pockethub.android.ui.view.DotPageIndicator.java

/**
 * Draws a row of dots//  www. jav a 2 s.  c om
 *
 * @param canvas The canvas to draw on
 * @param columns Number of dots in the row
 * @param w Width of the view to draw on
 * @param y Y position of the row (center)
 */
private void drawRow(Canvas canvas, int columns, float w, float y) {
    float center = w / 2;
    float start = center - getWidthOfDots(columns / 2);
    start -= columns % 2 == 0 ? dotSpacing / 2 : dotRadius + dotSpacing;

    for (int i = 0; i < columns; i++) {
        canvas.drawCircle(start + dotRadius, y, dotRadius, PAINT_NOT_SELECTED);
        start += (dotRadius * 2) + dotSpacing;
    }
}

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.ja v  a  2 s .c om*/
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.hwqgooo.douknow.view.widget.CircleImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mDisableCircularTransformation) {
        super.onDraw(canvas);
        return;/*w  w w.j a  va  2s  .  c  o m*/
    }

    if (mBitmap == null) {
        return;
    }

    if (mFillColor != Color.TRANSPARENT) {
        canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mFillPaint);
    }
    canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mBitmapPaint);
    if (mBorderWidth > 0) {
        canvas.drawCircle(mBorderRect.centerX(), mBorderRect.centerY(), mBorderRadius, mBorderPaint);
    }
}

From source file:com.yuhaiyang.looping.viewpager.LoopingViewPager.java

private void drawIndicator(Canvas canvas) {
    final int count = mAdapter.getRealCount();
    final int realWidth = mIndicatorWidth * count;
    final int startX = getWidth() / 2 - realWidth / 2 + getScrollX();
    final int y = getBottom() - mIndicatorHeight / 2;
    for (int i = 0; i < count; i++) {
        // ??/*from  w  w w  . ja v a  2  s.com*/
        int x = startX + i * mIndicatorWidth + (mIndicatorWidth - mIndicatorRadius) / 2;
        if (i == mRealCurrentPosition) {
            canvas.drawCircle(x, y, mCurrentIndicatorRadius, mIndicatorPaint);
            canvas.drawCircle(x, y, mCurrentIndicatorRadius, mIndicatorBorderPaint);
        } else if (i == mRealCurrentPosition + 1) {
            canvas.drawCircle(x, y, mNextIndicatorRadius, mIndicatorPaint);
            canvas.drawCircle(x, y, mNextIndicatorRadius, mIndicatorBorderPaint);
        } else {
            canvas.drawCircle(x, y, mIndicatorRadius, mIndicatorPaint);
            canvas.drawCircle(x, y, mIndicatorRadius, mIndicatorBorderPaint);
        }
    }
}

From source file:org.stockchart.series.LinearSeries.java

private void drawPoint(Canvas c, float x, float y, float r) {
    this.fTempRectF.set(x - r, y - r, x + r, y + r);

    switch (fPointStyle) {
    case SQUARE:/*w w  w. ja  va2 s.c  om*/

        getPointAppearance().applyFill(fPaint, fTempRectF);
        c.drawRect(fTempRectF, fPaint);
        getPointAppearance().applyOutline(fPaint);
        c.drawRect(fTempRectF, fPaint);
        break;
    case CIRCLE:
        getPointAppearance().applyFill(fPaint, fTempRectF);
        c.drawCircle(x, y, r, fPaint);
        getPointAppearance().applyOutline(fPaint);
        c.drawCircle(x, y, r, fPaint);
        break;
    }
}

From source file:org.getlantern.firetweet.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.
 *//*from  w  w  w .  ja  va  2s . 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 (mBorderEnabled) {
        final float inset = mBorderPaint.getStrokeWidth() / 2;
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - inset / 2;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            dest.inset(inset, inset);
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
            dest.inset(-inset, -inset);
        }
    } else {
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
        }
    }

}

From source file:com.mallapp.utils.ImageLoader.java

/** 
* Crops a circle out of the thumbnail photo. 
*///  ww w  . j  av a2  s.  c  o m
public Bitmap getCroppedBitmap(final Bitmap bitmap) {
    Bitmap output = null;

    try {

        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        final Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        paint.setAntiAlias(true);

        final int halfWidth = bitmap.getWidth() / 2;
        final int halfHeight = bitmap.getHeight() / 2;

        canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        canvas.drawBitmap(bitmap, rect, rect, paint);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output;
}

From source file:com.forrestguice.suntimeswidget.LightMapView.java

private void drawPoint(Calendar calendar, int radius, Canvas c, Paint p) {
    if (calendar != null) {
        int w = c.getWidth();
        int h = c.getHeight();

        double minute = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
        int x = (int) Math.round((minute / MINUTES_IN_DAY) * w);
        int y = h / 2;

        p.setStyle(Paint.Style.FILL);
        p.setColor(colorPointFill);//from  w w w  .  j  a v a2  s.  com
        c.drawCircle(x, y, radius, p);

        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(pointStrokeWidth);
        p.setColor(colorPointStroke);
        c.drawCircle(x, y, radius, p);
    }
}