Example usage for android.graphics Canvas drawOval

List of usage examples for android.graphics Canvas drawOval

Introduction

In this page you can find the example usage for android.graphics Canvas drawOval.

Prototype

public void drawOval(@NonNull RectF oval, @NonNull Paint paint) 

Source Link

Document

Draw the specified oval using the specified paint.

Usage

From source file:Main.java

/**
 * clip source bitmap into a circle bitmap
 *
 * @param src     the source bitmap/*  www  . j  av a  2 s.  c o  m*/
 * @param recycle whether recycle the source bitmap
 * @param config  bitmap config
 * @return clipped  circle bitmap
 */
public static Bitmap createCircleBitmap(Bitmap src, boolean recycle, Bitmap.Config config) {
    if (src == null) {
        return null;
    }
    if (config == null) {
        config = Bitmap.Config.ARGB_8888;
    }
    Bitmap out = Bitmap.createBitmap(src.getWidth(), src.getHeight(), config);
    final Rect rect = new Rect(0, 0, Math.min(src.getWidth(), src.getHeight()),
            Math.min(src.getWidth(), src.getHeight()));
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    Canvas canvas = new Canvas(out);
    canvas.drawARGB(0, 0, 0, 0);
    RectF rectF = new RectF(rect);
    canvas.drawOval(rectF, paint);

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

    if (recycle) {
        src.recycle();
    }

    return out;

}

From source file:Main.java

public static Bitmap toOvalBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w  w  w  .  ja  v a2  s  .  c  o  m
    Rect rect = new Rect(0, 0, bitmap.getHeight(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rectF, paint);
    return output;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *//*from   w ww  .j  a  va 2 s .co  m*/
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawOval(dst, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

public static Bitmap getCircularBitmap(Bitmap srcBitmap) {
    // Calculate the circular bitmap width with border
    int squareBitmapWidth = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight());

    //int squareBitmapWidth = 300;

    // Initialize a new instance of Bitmap
    Bitmap dstBitmap = Bitmap.createBitmap(squareBitmapWidth, // Width
            squareBitmapWidth, // Height
            Bitmap.Config.ARGB_8888 // Config
    );/*from w  w  w. j a  v  a2 s  .c  o  m*/

    Canvas canvas = new Canvas(dstBitmap);

    // Initialize a new Paint instance
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect rect = new Rect(0, 0, squareBitmapWidth, squareBitmapWidth);

    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);

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

    // Calculate the left and top of copied bitmap
    float left = (squareBitmapWidth - srcBitmap.getWidth()) / 2;
    float top = (squareBitmapWidth - srcBitmap.getHeight()) / 2;

    canvas.drawBitmap(srcBitmap, left, top, paint);

    srcBitmap.recycle();

    return dstBitmap;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.WHITE;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*ww w.j  a  v  a  2s .c o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap, boolean recycleable) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/* w  ww . java  2  s. c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    if (recycleable) {
        bitmap.recycle();
    }
    return output;
}

From source file:Main.java

public static Bitmap circleBitmap(Bitmap source, boolean recycle) {
    if (source == null) {
        return null;
    }/*from   w w w.  j  a va2  s .com*/

    // Create custom bitmap
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Compute sizes
    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    if (recycle) {
        source.recycle();
    }

    return output;
}

From source file:Main.java

public static Bitmap getCircularBitmap(Bitmap bm) {
    int size = 192;

    Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, size, size);

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final int color = 0xffff0000;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);//from   w  w  w . ja v  a 2 s .  c  o m
    paint.setDither(true);
    paint.setFilterBitmap(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setColor(Color.BLUE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) 4);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:csci310.parkhere.ui.fragments.PublicProfileFragment.java

public static Bitmap getRoundedBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*from ww w .j  av a2s. c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}

From source file:ar.uba.fi.splitapp.MockServer.java

private static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*from  w w  w.  j a va  2 s.co m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}