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.waz.zclient.pages.main.conversation.LocationFragment.java

private Bitmap getMarker() {
    if (marker != null) {
        return marker;
    }//from   ww w. j  a  v a 2 s  .c om
    int size = getResources().getDimensionPixelSize(R.dimen.share_location__current_location_marker__size);
    int outerCircleRadius = getResources()
            .getDimensionPixelSize(R.dimen.share_location__current_location_marker__outer_ring_radius);
    int midCircleRadius = getResources()
            .getDimensionPixelSize(R.dimen.share_location__current_location_marker__mid_ring_radius);
    int innerCircleRadius = getResources()
            .getDimensionPixelSize(R.dimen.share_location__current_location_marker__inner_ring_radius);

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

    Paint paint = new Paint();
    paint.setColor(getControllerFactory().getAccentColorController().getColor());
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setAlpha(
            getResources().getInteger(R.integer.share_location__current_location_marker__outer_ring_alpha));
    canvas.drawCircle(size / 2, size / 2, outerCircleRadius, paint);
    paint.setAlpha(
            getResources().getInteger(R.integer.share_location__current_location_marker__mid_ring_alpha));
    canvas.drawCircle(size / 2, size / 2, midCircleRadius, paint);
    paint.setAlpha(
            getResources().getInteger(R.integer.share_location__current_location_marker__inner_ring_alpha));
    canvas.drawCircle(size / 2, size / 2, innerCircleRadius, paint);
    marker = bitmap;
    return marker;
}

From source file:silent.kuasapmaterial.libs.ProgressWheel.java

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

    if (isCircleBackground) {
        if (isShadow) {
            canvas.drawCircle(circleBounds2.centerX() + mShadowSize / 2, circleBounds2.centerY() + mShadowSize,
                    circleBounds2.width() / 2 + mShadowSize / 2, shadowPaint);
        }/*from   w  w  w  . j a va 2 s  . co  m*/
        canvas.drawCircle(circleBounds2.centerX(), circleBounds2.centerY(), circleBounds2.width() / 2,
                circlePaint);
    }

    canvas.drawArc(circleBounds, 360, 360, false, rimPaint);

    boolean mustInvalidate = false;

    if (isSpinning) {
        //Draw the spinning bar
        mustInvalidate = true;

        long deltaTime = (SystemClock.uptimeMillis() - lastTimeAnimated);
        float deltaNormalized = deltaTime * spinSpeed / 1000.0f;

        updateBarLength(deltaTime);

        mProgress += deltaNormalized;
        if (mProgress > 360) {
            mProgress -= 360f;

            // A full turn has been completed
            // we run the callback with -1 in case we want to
            // do something, like changing the color
            runCallback(-1.0f);
        }
        lastTimeAnimated = SystemClock.uptimeMillis();

        float from = mProgress - 90;
        float length = barLength + barExtraLength;

        if (isInEditMode()) {
            from = 0;
            length = 135;
        }

        canvas.drawArc(circleBounds, from, length, false, barPaint);
    } else {
        float oldProgress = mProgress;

        if (mProgress != mTargetProgress) {
            //We smoothly increase the progress bar
            mustInvalidate = true;

            float deltaTime = (float) (SystemClock.uptimeMillis() - lastTimeAnimated) / 1000;
            float deltaNormalized = deltaTime * spinSpeed;

            mProgress = Math.min(mProgress + deltaNormalized, mTargetProgress);
            lastTimeAnimated = SystemClock.uptimeMillis();
        }

        if (oldProgress != mProgress) {
            runCallback();
        }

        float offset = 0.0f;
        float progress = mProgress;
        if (!linearProgress) {
            float factor = 2.0f;
            offset = (float) (1.0f - Math.pow(1.0f - mProgress / 360.0f, 2.0f * factor)) * 360.0f;
            progress = (float) (1.0f - Math.pow(1.0f - mProgress / 360.0f, factor)) * 360.0f;
        }

        if (isInEditMode()) {
            progress = 360;
        }

        canvas.drawArc(circleBounds, offset - 90, progress, false, barPaint);
    }

    if (mustInvalidate) {
        invalidate();
    }
}

From source file:yahier.exst.widget.CirclePageIndicator.java

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

    if (mViewPager == null) {
        return;//from  w  w  w  .ja va2  s .c  om
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();

    float dX;
    float dY = paddingTop + mRadius;

    float pageFillRadius = mRadius;
    if (mPaintStroke.getStrokeWidth() > 0) {
        pageFillRadius = mRadius - mPaintStroke.getStrokeWidth();
    }

    //Draw stroked circles
    for (int i = 0; i < count; i++) {

        dX = paddingLeft + mRadius + i * (mRadius * 2 + mGap);

        // Only paint fill if not completely transparent
        if (mPaintPageFill.getAlpha() > 0) {
            canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
        }

        // Only paint stroke if a stroke width was non-zero
        if (pageFillRadius != mRadius) {
            canvas.drawCircle(dX, dY, mRadius, mPaintStroke);
        }
    }

    dX = paddingLeft + mRadius + mCurrentPage * (mRadius * 2 + mGap);

    canvas.drawCircle(dX, dY, mRadius, mPaintFill);
}

From source file:com.keylesspalace.tusky.view.ProgressImageView.java

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

    float angle = (progress / 100f) * 360 - 90;
    float halfWidth = canvas.getWidth() / 2;
    float halfHeight = canvas.getHeight() / 2;
    progressRect.set(halfWidth * 0.75f, halfHeight * 0.75f, halfWidth * 1.25f, halfHeight * 1.25f);
    biggerRect.set(progressRect);//from   ww w  .j  av  a 2  s .  c  o  m
    int margin = 8;
    biggerRect.set(progressRect.left - margin, progressRect.top - margin, progressRect.right + margin,
            progressRect.bottom + margin);
    canvas.saveLayer(biggerRect, null, Canvas.ALL_SAVE_FLAG);
    if (progress != -1) {
        canvas.drawOval(progressRect, circlePaint);
        canvas.drawArc(biggerRect, angle, 360 - angle - 90, true, clearPaint);
    }
    canvas.restore();

    int circleRadius = Utils.dpToPx(getContext(), 14);
    int circleMargin = Utils.dpToPx(getContext(), 14);

    int circleY = canvas.getHeight() - circleMargin - circleRadius / 2;
    int circleX = canvas.getWidth() - circleMargin - circleRadius / 2;

    canvas.drawCircle(circleX, circleY, circleRadius, markBgPaint);

    captionDrawable.setBounds(canvas.getWidth() - circleMargin - circleRadius,
            canvas.getHeight() - circleMargin - circleRadius, canvas.getWidth() - circleMargin,
            canvas.getHeight() - circleMargin);
    DrawableCompat.setTint(captionDrawable, Color.WHITE);
    captionDrawable.draw(canvas);
}

From source file:de.telekom.pde.codelibrary.ui.layout.PDESwipeRefreshLayout.java

@Override
public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);

    int width = mBounds.width();
    int height = mBounds.height();
    int cx = width / 2;
    int cy = height / 2;

    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);//from  w w w  .  j a  va 2 s.  c  om

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % mAnimationDuration;
        float currentAngle = (elapsed / (mAnimationDuration / 100f)) * 3.6f;

        canvas.drawCircle(cx, cy, PDEBuildingUnits.BU(), mPaint);

        canvas.save();
        canvas.rotate(currentAngle, cx, cy);
        canvas.drawPath(mCirclePath, mWhitePaint);
        canvas.restore();

        ViewCompat.postInvalidateOnAnimation(this);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:org.uoyabause.android.PadButton.java

public void draw(Canvas canvas, int sx, int sy, Paint nomal_back, Paint active_back, Paint front) {
    super.draw(canvas, nomal_back, active_back, front);
    //canvas.drawCircle(rect.centerX(), rect.centerY(), width * this.scale, back);
    //front.setTextSize(textsize);
    //front.setTextAlign(Paint.Align.CENTER);
    //canvas.drawText(text, rect.centerX() , rect.centerY() , front);
    double dx = ((sx - 128.0) / 128.0) * ((width * this.scale) / 2);
    double dy = ((sy - 128.0) / 128.0) * ((width * this.scale) / 2);
    canvas.drawCircle(rect.centerX() + (int) dx, rect.centerY() + (int) dy, (width * this.scale / 2), paint);

}

From source file:org.linphone.ContactEditorFragment.java

private void editContactPicture(final String filePath, final Bitmap image) {
    int SIZE_SMALL = 256;
    int COMPRESSOR_QUALITY = 100;
    Bitmap bitmapUnknown = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
    Bitmap bm = null;//from  w  w  w . j a  v a  2 s .  com

    if (filePath != null) {
        int pixelsMax = SIZE_SMALL;
        //Resize image
        bm = BitmapFactory.decodeFile(filePath);
        if (bm != null) {
            if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) {
                bm = Bitmap.createScaledBitmap(bm, 256, 256, false);
            }
        }
    } else if (image != null) {
        bm = image;
    }

    // Rotate the bitmap if possible/needed, using EXIF data
    try {
        if (imageToUploadUri != null && filePath != null) {
            ExifInterface exif = new ExifInterface(filePath);
            int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Matrix matrix = new Matrix();
            if (pictureOrientation == 6) {
                matrix.postRotate(90);
            } else if (pictureOrientation == 3) {
                matrix.postRotate(180);
            } else if (pictureOrientation == 8) {
                matrix.postRotate(270);
            }
            bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Bitmap bitmapRounded;
    if (bm != null) {
        bitmapRounded = Bitmap.createScaledBitmap(bm, bitmapUnknown.getWidth(), bitmapUnknown.getWidth(),
                false);

        Canvas canvas = new Canvas(bitmapRounded);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(bitmapRounded, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.drawCircle(bitmapRounded.getWidth() / 2 + 0.7f, bitmapRounded.getHeight() / 2 + 0.7f,
                bitmapRounded.getWidth() / 2 + 0.1f, paint);
        contactPicture.setImageBitmap(bitmapRounded);

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, COMPRESSOR_QUALITY, outStream);
        photoToAdd = outStream.toByteArray();
    }
}

From source file:com.cuelogic.android.WheelIndicatorView.java

private void draw(String tag, WheelIndicatorItem indicatorItem, RectF surfaceRectF, float angle,
        Canvas canvas) {
    //        itemArcPaint.setColor(Color.parseColor("#FF9000"));
    itemEndPointsPaint.setColor(indicatorItem.getColor());

    // Draw arc//from   ww w. jav a 2  s.  c o  m
    //        canvas.drawArc(surfaceRectF, ANGLE_INIT_OFFSET, angle, false, itemArcPaint);

    float cxTop = minDistViewSize / 2;
    float cyTop = 0 + itemsLineWidth;

    // Draw top circle
    //        canvas.drawCircle(cxTop, cyTop, itemsLineWidth, itemEndPointsPaint);
    //        Log.v(tag, "cxTop: "+cxTop + " cyTop: "+cyTop);

    int topPosition = minDistViewSize / 2 - itemsLineWidth;

    float cxEnd = (float) (Math.cos(Math.toRadians(angle + ANGLE_INIT_OFFSET)) * topPosition + topPosition
            + itemsLineWidth);
    float cyEnd = (float) (Math.sin(Math.toRadians((angle + ANGLE_INIT_OFFSET))) * topPosition + topPosition
            + itemsLineWidth);

    // Draw end circle
    canvas.drawCircle(cxEnd, cyEnd, itemsLineWidth, itemEndPointsPaint);
    Log.v(tag, "cxEnd: " + cxEnd + " cyEnd: " + cyEnd);

    int leftRect = (int) cxEnd - itemsLineWidth;
    int topRect = (int) cyEnd - itemsLineWidth;
    int rightRect = (int) cxEnd + itemsLineWidth;
    int bottomRect = (int) cyEnd + itemsLineWidth;

    //        if(null == indicatorItem.getRect()) {
    //
    //        }

    Rect rect = new Rect(leftRect, topRect, rightRect, bottomRect);
    indicatorItem.setRect(rect);

}

From source file:com.concavenp.artistrymuse.BaseAppCompatActivity.java

private BitmapImageViewTarget createBitmapImageViewTarget(final ImageView imageView) {

    return new BitmapImageViewTarget(imageView) {
        @Override/*  www . j  a  v a 2s .c o m*/
        protected void setResource(Bitmap bitmap) {

            int bitmapWidth = bitmap.getWidth();
            int bitmapHeight = bitmap.getHeight();
            int borderWidthHalf = 10;
            int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
            int newBitmapSquare = bitmapSquareWidth + borderWidthHalf;

            Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquare, newBitmapSquare,
                    Bitmap.Config.ARGB_8888);

            // Initialize a new Canvas to draw empty bitmap
            Canvas canvas = new Canvas(roundedBitmap);

            // Calculation to draw bitmap at the circular bitmap center position
            int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
            int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

            canvas.drawBitmap(bitmap, x, y, null);

            // Initializing a new Paint instance to draw circular border
            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidthHalf * 2);
            borderPaint.setColor(ResourcesCompat.getColor(getResources(), R.color.myApp_accent_700, null));

            canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquare / 2, borderPaint);

            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),
                    roundedBitmap);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);

        }
    };
}

From source file:info.awesomedevelopment.tvgrid.library.TVGridView.java

/**
 * Helper method to paint the canvas used in generate bitmap
 *
 * @param canvas the canvas used to draw onto
 * @param rectF size//  www .j  a va 2s .  com
 * @param paint paint
 */
private void paintCanvas(Canvas canvas, RectF rectF, Paint paint) {
    if (mSelectorShape == RECTANGLE) {
        canvas.drawRoundRect(rectF, mCornerRadiusX, mCornerRadiusY, paint);
    } else if (mSelectorShape == CIRCLE) {
        canvas.drawCircle(rectF.centerX(), rectF.centerY(), rectF.width() / 2, paint);
    } else {
        throw new IllegalArgumentException("Selector shape must be one of RECTANGLE or CIRCLE");
    }
}