Example usage for android.graphics Canvas drawPath

List of usage examples for android.graphics Canvas drawPath

Introduction

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

Prototype

public void drawPath(@NonNull Path path, @NonNull Paint paint) 

Source Link

Document

Draw the specified path using the specified paint.

Usage

From source file:Main.java

/**
 * Helper method to draw a generic triangle onto the canvas
 * @param canvas the canvas being drawn to
 * @param a point 1/* w w w  .ja v  a2s .  c o m*/
 * @param b point 2
 * @param c point 3
 */
public static void drawTriangle(Canvas canvas, Point a, Point b, Point c, Paint paint) {

    Path path = new Path();
    path.setFillType(Path.FillType.EVEN_ODD);
    path.moveTo(b.x, b.y);
    path.lineTo(c.x, c.y);
    path.lineTo(a.x, a.y);
    path.close();

    canvas.drawPath(path, paint);
}

From source file:Main.java

public static void drawBorder(int width, int height, Canvas canvas, Paint paint, Path workingPath) {

    paint.setStyle(Paint.Style.STROKE);
    workingPath.reset();/*from   ww  w.  j a  v a  2  s.com*/
    workingPath.moveTo(0, 0);
    workingPath.lineTo(width, 0);
    workingPath.lineTo(width, height);
    workingPath.lineTo(0, height);
    workingPath.lineTo(0, 0);
    workingPath.close();

    canvas.drawPath(workingPath, paint);

}

From source file:Main.java

private static void drawAntiRoundRect(Canvas canvas, Paint paint, int radius, RectF rect, int direction) {
    if (direction == 1) {
        Path path = new Path();
        path.moveTo(rect.left, rect.top);
        path.lineTo(rect.left, rect.top + radius);
        path.addArc(new RectF(rect.left, rect.top, rect.left + radius * 2, rect.top + radius * 2), 180, 90);
        path.lineTo(rect.left, rect.top);
        path.close();//  w w  w.j av a 2  s  .c  o  m
        canvas.drawPath(path, paint);
    } else if (direction == 2) {
        Path path = new Path();
        path.moveTo(rect.right, rect.top);
        path.lineTo(rect.right - radius, rect.top);
        path.addArc(new RectF(rect.right - 2 * radius, rect.top, rect.right, rect.top + radius * 2), 270, 90);
        path.lineTo(rect.right, rect.top);
        path.close();
        canvas.drawPath(path, paint);
    } else if (direction == 3) {
        Path path = new Path();
        path.moveTo(rect.right, rect.bottom);
        path.lineTo(rect.right, rect.bottom - radius);
        path.addArc(new RectF(rect.right - 2 * radius, rect.bottom - 2 * radius, rect.right, rect.bottom), 0,
                90);
        path.lineTo(rect.right, rect.bottom);
        path.close();
        canvas.drawPath(path, paint);
    } else if (direction == 4) {
        Path path = new Path();
        path.moveTo(rect.left, rect.bottom);
        path.lineTo(rect.left + radius, rect.bottom);
        path.addArc(new RectF(rect.left, rect.bottom - 2 * radius, rect.left + 2 * radius, rect.bottom), 90,
                90);
        path.lineTo(rect.left, rect.bottom);
        path.close();
        canvas.drawPath(path, paint);
    }
}

From source file:Main.java

/**
 * Draws a circular arc on the given {@code Canvas}.
 *
 * @param canvas             The canvas to draw into.
 * @param circleCenter       The center of the circle on which to draw the arc.
 * @param circleRadius       The radius of the circle on which to draw the arc.
 * @param startAngle         Starting angle (in degrees) where the arc begins.
 * @param sweepAngle         Sweep angle (in degrees) measured clockwise.
 * @param paint              The paint to use then drawing the arc.
 * @param arcsPointsOnCircle See {@link #createBezierArcDegrees(PointF, float, float, float, int, boolean, Path)}.
 * @param arcsOverlayPoints  See {@link #createBezierArcDegrees(PointF, float, float, float, int, boolean, Path)}.
 *
 * @see #drawArc(Canvas, PointF, float, float, float, Paint)
 *//*  w  w w  .j  a  v a  2s.  c o m*/
public static void drawArc(@NonNull Canvas canvas, PointF circleCenter, float circleRadius, float startAngle,
        float sweepAngle, @NonNull Paint paint, int arcsPointsOnCircle, boolean arcsOverlayPoints) {
    if (sweepAngle == 0f) {
        final PointF p = pointFromAngleDegrees(circleCenter, circleRadius, startAngle);
        canvas.drawPoint(p.x, p.y, paint);
    } else {
        canvas.drawPath(createBezierArcDegrees(circleCenter, circleRadius, startAngle, sweepAngle,
                arcsPointsOnCircle, arcsOverlayPoints, null), paint);
    }
}

From source file:Main.java

private static Bitmap decorate(Bitmap bitmap, char label, int color, boolean glossy) {

    Canvas canvas = new Canvas(bitmap);

    canvas.drawColor(color);//from   w  w  w. ja v  a2  s  .  co  m

    String labelString = String.valueOf(label);
    Paint textPaint = getTextPaint(bitmap.getHeight() * 0.75f);
    float cx = bitmap.getWidth() / 2;
    float cy = bitmap.getHeight() / 2;
    float oy = 0;

    synchronized (junkRectangle) {
        textPaint.getTextBounds(labelString, 0, 1, junkRectangle);
        oy = (junkRectangle.top + junkRectangle.bottom) / 2;
    }

    canvas.drawText(labelString, cx, cy - oy, textPaint);

    if (glossy) {
        canvas.drawPath(gloss(bitmap.getWidth()), getGlossPaint());
    }

    return bitmap;

}

From source file:Main.java

private static void drawRoundRect(Canvas canvas, RectF rect, Paint paint, int radius, boolean leftTop,
        boolean rightTop, boolean leftBottom, boolean rightBottom) {
    float roundRadius[] = new float[8];
    roundRadius[0] = leftTop ? 0 : radius;
    roundRadius[1] = leftTop ? 0 : radius;
    roundRadius[2] = rightTop ? 0 : radius;
    roundRadius[3] = rightTop ? 0 : radius;
    roundRadius[4] = rightBottom ? 0 : radius;
    roundRadius[5] = rightBottom ? 0 : radius;
    roundRadius[6] = leftBottom ? 0 : radius;
    roundRadius[7] = leftBottom ? 0 : radius;

    Path path = new Path();
    path.addRoundRect(rect, roundRadius, Direction.CCW);
    canvas.drawPath(path, paint);
}

From source file:Main.java

private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());

    // Paint used for scaling the bitmap and drawing the rounded rect.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);/*from w  ww  .  ja v  a  2s.  co  m*/
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);

    // Construct a path from a round rect. This will allow drawing with
    // an inverse fill so we can punch a hole using the round rect.
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(1, 1);
    path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);

    // Reuse the paint and clear the outside of the rectangle.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

/**
 * Use touch-icon or higher-resolution favicon and round the corners.
 * @param context    Context used to get resources.
 * @param touchIcon  Touch icon bitmap./*from   w  w  w.  j a  v  a 2 s.co  m*/
 * @param canvas     Canvas that holds the touch icon.
 */
private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);
    // Convert dp to px.
    int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOUCHICON_BORDER_RADII,
            context.getResources().getDisplayMetrics());
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
    path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:uk.org.ngo.squeezer.util.ImageWorker.java

/**
 * Adds a debug swatch to a canvas. The marker is a triangle pointing north-west
 * on the top left corner, the edges are 25% of the canvas' width and height.
 *
 * @param canvas The canvas to draw on.//from   w  w w.  j  a va  2 s  .c om
 * @param color The colour to use for the swatch.
 */
public static void addDebugSwatch(Canvas canvas, int color) {
    float width = canvas.getWidth();
    float height = canvas.getHeight();

    Path path = new Path();
    path.lineTo(width / 4, 0);
    path.lineTo(0, height / 4);
    path.lineTo(0, 0);

    // Draw the swatch.
    mCacheDebugPaint.setColor(color);
    mCacheDebugPaint.setStyle(Paint.Style.FILL);
    canvas.drawPath(path, mCacheDebugPaint);

    // Stroke the swatch with a white hairline.
    mCacheDebugPaint.setColor(Color.WHITE);
    mCacheDebugPaint.setStyle(Paint.Style.STROKE);
    mCacheDebugPaint.setStrokeWidth(0);
    canvas.drawPath(path, mCacheDebugPaint);
}

From source file:com.jaspersoft.android.jaspermobile.widget.AnnotationView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    for (Pair<Paint, Path> paintPathPair : mDrawingCache) {
        canvas.drawPath(paintPathPair.second, paintPathPair.first);
    }/* w w w .  jav  a2s  .com*/
}