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 makeCircleBitmap(Bitmap original) {
    final int width = original.getWidth();
    final int height = original.getHeight();
    final float radius = Math.min(width, height) / 2;

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

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w  ww.  j ava2  s  . c  o  m*/
    paint.setShader(new BitmapShader(original, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(radius, radius, radius, paint);

    original.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedImage(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);//from   w  w  w .  j a v  a 2 s  .com

    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

/**
 * create a circle from cutout from a bitmap.
 * does not alter sizes./* w ww .jav  a  2  s. com*/
 *
 * @param bitmap the bitmap
 * @see #cutCircleFromBitmap(String, int)
 * @return a bitmap circle cutout
 */
public static Bitmap roundBitMap(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setShader(shader);

    Canvas c = new Canvas(circleBitmap);

    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

/**
 * Create a bitmap which is wrapped to circle
 * (similar to what you can see in G+ profile pic.)
 *
 * @param bitmap Original Bitmap/*from  ww w.j  a va 2  s .  c  om*/
 * @return Circled bitmap
 */
public static Bitmap createCircleBitmap(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint bitmapPaint = new Paint();
    bitmapPaint.setAntiAlias(true);
    bitmapPaint.setShader(bitmapShader);

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint);

    return output;
}

From source file:Main.java

public static Bitmap circleBitmap(final Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w  ww  .j av  a 2 s.  c  om*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    final float radius = width > height ? height / 2 : width / 2;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

public static Bitmap getUnreadMarker(int width, int radius, int color) {
    if (width <= 0) {
        return null;
    }/*from   www  . j  av  a2 s. com*/
    Bitmap dest = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    Paint p = new Paint();
    p.setColor(color);
    p.setAntiAlias(true);
    canvas.drawCircle(width / 2, width / 2, radius, p);
    return dest;
}

From source file:com.achep.acdisplay.graphics.IconFactory.java

@NonNull
private static Bitmap createEmptyIcon(@NonNull Resources res, int size) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);// w  ww.jav  a  2  s  .c  o m
    paint.setColor(0xDDCCCCCC); // white gray

    final float radius = size / 2f;

    Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(icon);
    canvas.drawCircle(radius, radius, radius, paint);

    Drawable drawable = res.getDrawable(R.drawable.ic_action_warning_white);
    drawable.setBounds(0, 0, size, size);
    drawable.draw(canvas);

    return icon;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap out = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(out);
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);//from  ww w. j  av a2s .  c o  m
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect rect = new Rect(0, 0, width, height);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return out;
}

From source file:Main.java

public static Bitmap toCircleBitmap(Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int r = width < height ? width : height;

    Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(buffer);
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w  w w  .  jav  a2s.  c  om
    RectF rect = new RectF(0, 0, r, r);

    canvas.drawCircle(r / 2, r / 2, r / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    bitmap.recycle();
    return buffer;
}

From source file:com.bullmobi.message.graphics.IconFactory.java

@NonNull
private static Bitmap createEmptyIcon(@NonNull Context context, int size) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//w ww  .  j av a  2s  . co m
    paint.setColor(0xDDCCCCCC); // white gray

    final float radius = size / 2f;

    Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(icon);
    canvas.drawCircle(radius, radius, radius, paint);

    Drawable drawable = ResUtils.getDrawable(context, R.drawable.ic_action_warning_white);
    assert drawable != null;
    drawable.setBounds(0, 0, size, size);
    drawable.draw(canvas);

    return icon;
}