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 getClip(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);//from  w w w.ja va2s . c  o m
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundImage(Bitmap oriImg, boolean recycleOld) {

    Bitmap targetBitmap = null;//  w  ww.j  a v a2 s.c om

    if (oriImg != null && !oriImg.isRecycled()) {
        int size = Math.min(oriImg.getWidth(), oriImg.getHeight());
        int targetWidth = size;
        int targetHeight = size;

        targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(targetBitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(targetWidth / 2f, targetHeight / 2f, targetWidth / 2f, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(oriImg, new Rect(0, 0, size, size), new Rect(0, 0, targetWidth, targetHeight), paint);

        if (recycleOld) {
            oriImg.recycle();
        }
    }
    return targetBitmap;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);//from   w  w w  . j  ava 2  s.com
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

/**
 * Frames the input bitmap in a circle.//from  ww  w .j  a  v a  2 s .  co  m
 */
public static Bitmap frameBitmapInCircle(Bitmap input) {
    if (input == null) {
        return null;
    }

    // Crop the image if not squared.
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int targetX, targetY, targetSize;
    if (inputWidth >= inputHeight) {
        targetX = inputWidth / 2 - inputHeight / 2;
        targetY = 0;
        targetSize = inputHeight;
    } else {
        targetX = 0;
        targetY = inputHeight / 2 - inputWidth / 2;
        targetSize = inputWidth;
    }

    // Create an output bitmap and a canvas to draw on it.
    Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Create a black paint to draw the mask.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);

    // Draw a circle.
    canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint);

    // Replace the black parts of the mask with the input image.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint);

    return output;
}

From source file:Main.java

/**
 * Create round, coloured bitmap with text embedded.
 * @param circleColor The color to use./*from   www . ja v a  2  s.c om*/
 * @param diameterDP The diameter of the circle.
 * @param text The text to embed.
 * @return Bitmap showing a text.
 */
public static Bitmap generateCircleBitmap(int circleColor, float diameterDP, String text) {
    /**
     *
     * http://stackoverflow.com/questions/31168636/rounded-quickcontactbadge-with-text
     */
    final int textColor = 0xffffffff;

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float diameterPixels = diameterDP * (metrics.densityDpi / 160f);
    float radiusPixels = diameterPixels / 2;

    // Create the bitmap
    Bitmap output = Bitmap.createBitmap((int) diameterPixels, (int) diameterPixels, Bitmap.Config.ARGB_8888);

    // Create the canvas to draw on
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);

    // Draw the circle
    final Paint paintC = new Paint();
    paintC.setAntiAlias(true);
    paintC.setColor(circleColor);
    canvas.drawCircle(radiusPixels, radiusPixels, radiusPixels, paintC);

    // Draw the text
    if (text != null && text.length() > 0) {
        final Paint paintT = new Paint();
        paintT.setColor(textColor);
        paintT.setAntiAlias(true);
        paintT.setTextSize(radiusPixels * 2);
        paintT.setTypeface(Typeface.SANS_SERIF);
        final Rect textBounds = new Rect();
        paintT.getTextBounds(text, 0, text.length(), textBounds);
        canvas.drawText(text, radiusPixels - textBounds.exactCenterX(),
                radiusPixels - textBounds.exactCenterY(), paintT);
    }

    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int border, int color) {
    Bitmap scaledBitmap;// w w w  .  ja va  2 s  .  com
    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        scaledBitmap = ThumbnailUtils.extractThumbnail(bmp, radius - 2, radius - 2);
    } else {
        scaledBitmap = bmp;
    }

    Bitmap output = Bitmap.createBitmap(scaledBitmap.getWidth(), scaledBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2, scaledBitmap.getWidth() / 2,
            paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    final Rect rect = new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());
    canvas.drawBitmap(scaledBitmap, rect, rect, paint);

    if (border > 0) {
        paint.setStrokeWidth(border);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2,
                scaledBitmap.getWidth() / 2, paint);
    }

    return output;
}

From source file:Main.java

public static void drawCircleBorder(Canvas canvas, int radius, int w, int y) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w ww.j ava  2  s . c  o m
    paint.setFilterBitmap(true);
    paint.setDither(true);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(mBorderThickness);
    canvas.drawCircle(w / 2, y / 2, radius, paint);
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);/*from w  w  w  .jav  a2s  .co m*/
    canvas.drawARGB(0, 0, 0, 0);
    //paint.setColor(0xff424242);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap punchAHoleInABitmap(Context context, Bitmap foreground, float x1, float y1) {
    Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(foreground, 0, 0, paint);
    paint.setAntiAlias(true);/*from   ww w  .j av  a 2  s  .com*/
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    float radius = (float) (getScreenSize(context).x * .06);
    canvas.drawCircle(x1, y1 - 450, radius, paint);
    return bitmap;
}

From source file:Main.java

/**
 * Method to get a rounded rectange bitmap
 * @param bitmap/*w w  w  . j a  va2 s  . com*/
 * @param pixels
 * @return
 */
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
    Bitmap result = null;
    try {
        result = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);

        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 200, 200);

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

    } catch (NullPointerException e) {
    } catch (OutOfMemoryError o) {
    }
    return result;
}