Example usage for android.graphics Canvas clipPath

List of usage examples for android.graphics Canvas clipPath

Introduction

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

Prototype

public boolean clipPath(@NonNull Path path) 

Source Link

Document

Intersect the current clip with the specified path.

Usage

From source file:Main.java

public static Bitmap GetBitmapClippedCircle(Bitmap bitmap) {

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    final Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Path path = new Path();
    path.addCircle((float) (width / 2), (float) (height / 2), (float) Math.min(width, (height / 2)),
            Path.Direction.CCW);//from  w w w .  j av a2  s .c o m

    final Canvas canvas = new Canvas(outputBitmap);
    canvas.clipPath(path);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

From source file:Main.java

public static Bitmap GetBitmapClippedCircle(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Path path = new Path();
    path.addCircle((float) (width / 2), (float) (height / 2), (float) Math.min(width, (height / 2)),
            Path.Direction.CCW);/*from  w  ww. jav  a2 s.  c om*/

    Canvas canvas = new Canvas(outputBitmap);
    canvas.clipPath(path);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);/* w ww  . j av a  2 s  . co  m*/
    int w = src.getWidth();
    int h = src.getHeight();
    int canvasW = Math.round(radius * 2);
    Bitmap bitmap = Bitmap.createBitmap(canvasW, canvasW, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Path path = new Path();
    path.addCircle(radius, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);

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

    Rect srcRect = new Rect(0, 0, w, h);
    Rect dstRect = new Rect(0, 0, canvasW, canvasW);

    canvas.drawBitmap(src, srcRect, dstRect, paint);

    return bitmap;
}

From source file:Main.java

public static void drawImage(Canvas canvas, Paint paint, Bitmap bitmap, float x, float y, float r) {
    canvas.save();//from  w w  w  . j  a va 2s. c  o m
    Path path = new Path();
    path.addCircle(x, y, r, Path.Direction.CCW);
    canvas.clipPath(path);
    canvas.drawBitmap(bitmap, x - r, y - r, paint);
    canvas.restore();
}

From source file:Main.java

/**
 * Get a circular (rounded) bitmap shape with the diameter is the smaller between target width and target height.
 * @param bitmap//from  w w  w. j  a va2 s.  com
 * @param width target width
 * @param height target height
 * @return Rounded (circular) bitmap width diameter is the smaller between target width and target height.
 */
public static Bitmap getRoundedBitmap(Bitmap bitmap, int width, int height) {
    int diameter = width < height ? width : height;
    Bitmap targetBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(width / 2f, height / 2f, diameter / 2f, Path.Direction.CCW);
    canvas.clipPath(path);

    Bitmap sourceBitmap = bitmap;
    Rect destinationRect = new Rect(0, 0, width, height);
    Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
    canvas.drawBitmap(sourceBitmap, sourceRect, destinationRect, null);
    return targetBitmap;
}

From source file:Main.java

/**
 * Crop bitmap image into a round shape/*from w  w  w .  j  av a  2 s .com*/
 *
 * @param bitmap   the bitmap
 * @param diameter the diameter of the resulting image
 * @return the rounded bitmap
 */
private static Bitmap getRoundedShape(@NonNull Bitmap bitmap, int diameter) {

    Bitmap resultBitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBitmap);
    Path path = new Path();
    path.addCircle(((float) diameter - 1) / 2, ((float) diameter - 1) / 2, (((float) diameter) / 2),
            Path.Direction.CCW);

    canvas.clipPath(path);
    resultBitmap.setHasAlpha(true);
    canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
            new Rect(0, 0, diameter, diameter), null);
    return resultBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCircleBitmap(Bitmap scaleBitmapImage) {
    int targetWidth = 150;
    int targetHeight = 150;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int width) {
    // TODO Auto-generated method stub
    int targetWidth = width;
    int targetHeight = width;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);
    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

/**Method to get round shaped bitmap. Mostly used for profile pictures
 *
 *     @param scaleBitmapImage/*w w w. j a va  2  s . c om*/
 *                      The source bitmap which has to converted to round shape
 *     @param context
 *                      The context
 *     @param targetWidthPixels
 *                      The width required for the target or returned  bitmap
 *     @param targetHeightPixels
 *                      The height required for the target or returned  bitmap
 *     @return
 *            round shaped bitmap
 */
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, Context context, int targetWidthPixels,
        int targetHeightPixels) {

    Bitmap targetBitmap = Bitmap.createBitmap(targetWidthPixels, targetHeightPixels, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidthPixels - 1) / 2, ((float) targetHeightPixels - 1) / 2,
            (Math.min(((float) targetWidthPixels), ((float) targetHeightPixels)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidthPixels, targetHeightPixels), null);
    return targetBitmap;
}

From source file:Main.java

/**
 * Returns a rounded bitmap using specified bitmap image.
 * //from   www .j  a  va2s  .  c  om
 * @param scaleBitmapImage bitmap to make round image.
 * @return rounded bitmap
 */
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    if (scaleBitmapImage == null)
        return null;

    int targetWidth = (int) DP;
    int targetHeight = (int) DP;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();

    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),

            Path.Direction.CCW);

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

    canvas.clipPath(path);
    canvas.drawBitmap(scaleBitmapImage,
            new Rect(0, 0, scaleBitmapImage.getWidth(), scaleBitmapImage.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), p);

    p.setARGB(255, 16, 18, 16);

    scaleBitmapImage.recycle();

    return targetBitmap;
}