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:Main.java

public static Bitmap getScaledCircleCroppedBitmap(Bitmap bitmap, int destSize) {
    if (bitmap == null)
        return null;
    Bitmap output = Bitmap.createBitmap(destSize, destSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
    final int srcX = (bitmap.getWidth() - srcSize) / 2;
    final int srcY = (bitmap.getHeight() - srcSize) / 2;
    final Rect srcRect = new Rect(srcX, srcY, srcX + srcSize, srcY + srcSize);
    final Rect destRect = new Rect(0, 0, destSize, destSize);
    final int color = 0xff424242;
    final Paint paint = new Paint();

    paint.setAntiAlias(true);/*from  ww  w .  ja  v  a  2  s  .  c o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(destSize / 2, destSize / 2, destSize / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, srcRect, destRect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12,
            Bitmap.Config.ARGB_8888);//from   ww  w .  ja va 2s .com

    Canvas canvas = new Canvas(output);
    //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor));

    Paint borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    borderPaint.setColor(borderColor);
    borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK);

    int centerWidth = output.getWidth() / 2;
    int centerHeight = output.getHeight() / 2;
    canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint);

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

    Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12);
    Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight());

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, rectS, rectD, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedRectBitmap(Bitmap bitmap) {
    Bitmap result = null;/*from w  ww .  ja  v  a2s  .c  o  m*/
    Canvas canvas;
    Paint paint;
    try {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        int color = 0xff424242;
        float radius = width > height ? width / 2 : height / 2;
        paint = new Paint();
        Rect rect = new Rect(0, 0, width, height);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (NullPointerException | OutOfMemoryError e) {
        e.printStackTrace();
    }
    return result;
}

From source file:org.telegram.ui.ActionBar.Theme.java

public static Drawable createBarSelectorDrawable(int color, boolean masked) {
    Drawable drawable;/* w  w w  .j av  a2 s  .  c  om*/
    if (Build.VERSION.SDK_INT >= 21) {
        Drawable maskDrawable = null;
        if (masked) {
            maskPaint.setColor(0xffffffff);
            maskDrawable = new Drawable() {
                @Override
                public void draw(Canvas canvas) {
                    android.graphics.Rect bounds = getBounds();
                    canvas.drawCircle(bounds.centerX(), bounds.centerY(), AndroidUtilities.dp(18), maskPaint);
                }

                @Override
                public void setAlpha(int alpha) {

                }

                @Override
                public void setColorFilter(ColorFilter colorFilter) {

                }

                @Override
                public int getOpacity() {
                    return PixelFormat.UNKNOWN;
                }
            };
        }
        ColorStateList colorStateList = new ColorStateList(new int[][] { new int[] {} }, new int[] { color });
        return new RippleDrawable(colorStateList, null, maskDrawable);
    } else {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_selected }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] {}, new ColorDrawable(0x00000000));
        return stateListDrawable;
    }
}

From source file:com.arisprung.tailgate.utilities.FacebookImageLoader.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    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());

    paint.setAntiAlias(true);/*www .  j a  va  2  s  .c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    int dens = mContext.getResources().getDisplayMetrics().densityDpi;
    Bitmap _bmp = Bitmap.createScaledBitmap(output, dens / 2, dens / 2, false);
    // return _bmp;
    Bitmap bit = TailGateUtility.drawWhiteFrame(_bmp);
    return bit;
}

From source file:com.silentcircle.common.util.ViewUtil.java

/**
 * Cut a circular image from provided bitmap.
 *
 * @param bitmap Bitmap from which to cut the circle.
 *
 *///from w w  w  .  j  a  v a2s.  co  m
public static Bitmap getCircularBitmap(final Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int dimens = width;
    if (width > height) {
        dimens = height;
    }
    Bitmap output = Bitmap.createBitmap(dimens, dimens, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final float radius = width / 2.0f;
    final int color = 0xffff00ff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawCircle(radius, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Clips the corners of a bitmap in order to transform it into a round shape. Additionally, the
 * bitmap is resized to a specific size. Bitmaps, whose width and height are not equal, will be
 * clipped to a square beforehand./*from  w  w w .  j a  v a2  s  .  co  m*/
 *
 * @param bitmap
 *         The bitmap, which should be clipped, as an instance of the class {@link Bitmap}. The
 *         bitmap may not be null
 * @param size
 *         The size, the bitmap should be resized to, as an {@link Integer} value in pixels. The
 *         size must be at least 1
 * @return The clipped bitmap as an instance of the class {@link Bitmap}
 */
public static Bitmap clipCircle(@NonNull final Bitmap bitmap, final int size) {
    Bitmap squareBitmap = clipSquare(bitmap, size);
    int squareSize = squareBitmap.getWidth();
    float radius = (float) squareSize / 2.0f;
    Bitmap clippedBitmap = Bitmap.createBitmap(squareSize, squareSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(clippedBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    canvas.drawCircle(radius, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(squareBitmap, new Rect(0, 0, squareSize, squareSize),
            new Rect(0, 0, squareSize, squareSize), paint);
    return clippedBitmap;
}

From source file:com.github.amlcurran.showcaseview.StandardShowcaseDrawer.java

@Override
public void drawShowcase(Bitmap buffer, float x, float y, float scaleMultiplier) {
    Canvas bufferCanvas = new Canvas(buffer);
    bufferCanvas.drawCircle(x, y, showcaseRadius, eraserPaint);
    int halfW = getShowcaseWidth() / 2;
    int halfH = getShowcaseHeight() / 2;
    int left = (int) (x - halfW);
    int top = (int) (y - halfH);
    showcaseDrawable.setBounds(left, top, left + getShowcaseWidth(), top + getShowcaseHeight());
    showcaseDrawable.draw(bufferCanvas);
}

From source file:com.androidzeitgeist.webcards.overlay.HandleView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawCircle(centerX, centerY, centerX, paint);

    canvas.drawBitmap(currentBitmap, centerX - currentBitmap.getWidth() / 2,
            centerY - currentBitmap.getHeight() / 2, paint);
}

From source file:com.androidmapsextensions.DefaultClusterOptionsProvider.java

private void drawCircle(Canvas canvas, int count, float iconSize) {
    canvas.drawCircle(iconSize / 2, iconSize / 2, iconSize / 2 - blurRadius, circleShadowPaint);
    for (int i = colors.length - 1; i >= 0; i--) {
        if (count >= Math.pow(10, i)) {
            circlePaint.setColor(colors[i]);
            break;
        }/*w  w w .j  a v a2  s .  c  om*/
    }
    canvas.drawCircle(iconSize / 2, iconSize / 2, iconSize / 2 - blurRadius, circlePaint);
}