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

/**
 * This method takes a square bitmap and clips it into a circle
 *
 * @param bitmap : the image to clip/*from w  w w  .  ja  v a2  s. c om*/
 * @return the clipped bitmap
 */
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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);
    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(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

    Bitmap sbmp;//  www  .j av a  2s.  c  om
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    //final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bitmap) {
    if (bitmap == null)
        return null;

    bitmap = cropToSquare(bitmap);//from ww  w. j av a  2 s .  co  m

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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);
    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(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //      Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //      return _bmp;
    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(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);/*from  ww w . j a va  2s.co m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    } else {
        // Yatay
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getHeight() / 2, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        output = Bitmap.createBitmap(output, 0, output.getHeight() / 2 - output.getWidth() / 2,
                output.getWidth(), output.getWidth());
    } else {
        // Yatay
        output = Bitmap.createBitmap(output, output.getWidth() / 2 - output.getHeight() / 2, 0,
                output.getHeight(), output.getHeight());
    }

    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;//from   ww w . java2 s.  c o m

    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;

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

    //final int color = 0xffa19774;
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(Color.parseColor("#BAB399"));

    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);

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

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

    return output;
}

From source file:Main.java

/**
 * Create circle image./*from  ww  w  . j a  va 2 s .co m*/
 *
 * @param bitmap      Bitmap to be cropped
 * @param resColor    Resource color
 * @param strokeWidth Thickness of stroke
 * @return Returns the circle image with border
 */
public static Bitmap getCircleImage(Bitmap bitmap, int resColor, int strokeWidth) {
    // create Bitmap to draw
    Bitmap mBitmap = Bitmap.createBitmap(bitmap.getWidth() + 8, bitmap.getHeight() + 8,
            Bitmap.Config.ARGB_8888);

    // create Rect to hold image
    final Rect mRec = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    // create Canvas
    Canvas mCanvas = new Canvas(mBitmap);
    mCanvas.drawARGB(0, 0, 0, 0);

    // create Paint
    final Paint mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);

    // get the half size of the image
    int mHalfWidth = bitmap.getWidth() / 2;
    int mHalfHeight = bitmap.getHeight() / 2;

    // draw circle
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // unknown
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // draw the image
    mCanvas.drawBitmap(bitmap, mRec, mRec, mPaint);

    // set border mode
    mPaint.setXfermode(null);

    // set stroke
    mPaint.setStyle(Paint.Style.STROKE);

    // set stroke color
    mPaint.setColor(resColor);

    // set stroke width
    mPaint.setStrokeWidth(strokeWidth);

    // draw stroke
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // return the circle image
    return mBitmap;
}

From source file:Main.java

public static Bitmap createCircularClip(Bitmap input, int width, int height) {
    if (input == null)
        return null;

    final int inWidth = input.getWidth();
    final int inHeight = input.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    paint.setAntiAlias(true);/*from   www .  j  av a2  s . c  o m*/
    final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
    final RectF dstRect = new RectF(0, 0, width, height);
    final Matrix m = new Matrix();
    m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
    canvas.setMatrix(m);
    canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
    return output;
}

From source file:Main.java

public static Bitmap get_circular_bitmap(Bitmap bitmap) {
    Bitmap output;//w w w . ja va 2  s .c o  m

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.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());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

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

    return output;
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Applies a circular mask to a bitmap.//from ww  w . j ava  2 s  . co m
 *
 * @param bitmap The bitmap to apply the mask to.
 */
public static Bitmap applyCircularMask(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setAntiAlias(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

/**
 * cut a circle from a bitmap/*from w w w  .  ja  v  a2  s  .c o m*/
 *
 * @param picturePath complete path name for the file.
 * @param destCube the cube dimension of dest bitmap
 * @return the bitmap
 */
public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inDither = false; //Disable Dithering mode
    opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    opts.inTempStorage = new byte[32 * 1024];

    Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts);

    int cube = destCube;

    if (bitmapImg == null)
        return null;

    int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight());

    Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();

    int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5);
    int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5);

    final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest);
    final Rect rectDest = new Rect(0, 0, cube, cube);

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);

    canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint);

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

    canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint);

    bitmapImg.recycle();

    return output;
}