Example usage for android.graphics Path addArc

List of usage examples for android.graphics Path addArc

Introduction

In this page you can find the example usage for android.graphics Path addArc.

Prototype

public void addArc(RectF oval, float startAngle, float sweepAngle) 

Source Link

Document

Add the specified arc to the path as a new contour.

Usage

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();//from   w ww .  ja v  a2  s  .  c  om
        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:com.larvalabs.svgandroid.SVGParser.java

/**
 * Elliptical arc implementation based on the SVG specification notes
 * Adapted from the Batik library (Apache-2 license) by SAU
 *///w  ww  . j a v a  2 s .  c  o m

private static void drawArc(Path path, double x0, double y0, double x, double y, double rx, double ry,
        double angle, boolean largeArcFlag, boolean sweepFlag) {
    double dx2 = (x0 - x) / 2.0;
    double dy2 = (y0 - y) / 2.0;
    angle = Math.toRadians(angle % 360.0);
    double cosAngle = Math.cos(angle);
    double sinAngle = Math.sin(angle);

    double x1 = (cosAngle * dx2 + sinAngle * dy2);
    double y1 = (-sinAngle * dx2 + cosAngle * dy2);
    rx = Math.abs(rx);
    ry = Math.abs(ry);

    double Prx = rx * rx;
    double Pry = ry * ry;
    double Px1 = x1 * x1;
    double Py1 = y1 * y1;

    // check that radii are large enough
    double radiiCheck = Px1 / Prx + Py1 / Pry;
    if (radiiCheck > 1) {
        rx = Math.sqrt(radiiCheck) * rx;
        ry = Math.sqrt(radiiCheck) * ry;
        Prx = rx * rx;
        Pry = ry * ry;
    }

    // Step 2 : Compute (cx1, cy1)
    double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
    double sq = ((Prx * Pry) - (Prx * Py1) - (Pry * Px1)) / ((Prx * Py1) + (Pry * Px1));
    sq = (sq < 0) ? 0 : sq;
    double coef = (sign * Math.sqrt(sq));
    double cx1 = coef * ((rx * y1) / ry);
    double cy1 = coef * -((ry * x1) / rx);

    double sx2 = (x0 + x) / 2.0;
    double sy2 = (y0 + y) / 2.0;
    double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
    double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);

    // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
    double ux = (x1 - cx1) / rx;
    double uy = (y1 - cy1) / ry;
    double vx = (-x1 - cx1) / rx;
    double vy = (-y1 - cy1) / ry;
    double p, n;

    // Compute the angle start
    n = Math.sqrt((ux * ux) + (uy * uy));
    p = ux; // (1 * ux) + (0 * uy)
    sign = (uy < 0) ? -1.0 : 1.0;
    double angleStart = Math.toDegrees(sign * Math.acos(p / n));

    // Compute the angle extent
    n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
    p = ux * vx + uy * vy;
    sign = (ux * vy - uy * vx < 0) ? -1.0 : 1.0;
    double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
    if (!sweepFlag && angleExtent > 0) {
        angleExtent -= 360f;
    } else if (sweepFlag && angleExtent < 0) {
        angleExtent += 360f;
    }
    angleExtent %= 360f;
    angleStart %= 360f;

    RectF oval = new RectF((float) (cx - rx), (float) (cy - ry), (float) (cx + rx), (float) (cy + ry));
    path.addArc(oval, (float) angleStart, (float) angleExtent);
}