Android examples for Graphics:Canvas
Adds a circular arc to the given path by approximating it through a cubic Bezier curve on Canvas.
/**/*from ww w . j a va 2s.c o m*/ * ArcUtils.java * * Copyright (c) 2014 BioWink GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. **/ //package com.java2s; import android.graphics.Path; import android.graphics.PointF; import android.support.annotation.NonNull; import static java.lang.Math.sqrt; public class Main { /** * Adds a circular arc to the given path by approximating it through a cubic B?er curve. * <p/> * <p> * Note that this <strong>does not</strong> split the arc to better approximate it, for that see either: * <ul> * <li>{@link #createBezierArcDegrees(PointF, float, float, float, int, boolean, * Path)}</li> * <li>{@link #createBezierArcRadians(PointF, float, double, double, int, boolean, * Path)}</li> * </ul> * </p> * <p/> * For a technical explanation: * <a href="http://hansmuller-flex.blogspot.de/2011/10/more-about-approximating-circular-arcs.html"> * http://hansmuller-flex.blogspot.de/2011/10/more-about-approximating-circular-arcs.html * </a> * * @param path The path to add the arc to. * @param center The center of the circle. * @param start The starting point of the arc on the circle. * @param end The ending point of the arc on the circle. * @param moveToStart If {@code true}, move to the starting point of the arc * (see: {@link Path#moveTo(float, float)}). * * @see #createBezierArcDegrees(PointF, float, float, float, int, boolean, Path) * @see #createBezierArcRadians(PointF, float, double, double, int, boolean, Path) */ public static void addBezierArcToPath(@NonNull Path path, @NonNull PointF center, @NonNull PointF start, @NonNull PointF end, boolean moveToStart) { if (moveToStart) { path.moveTo(start.x, start.y); } if (start.equals(end)) { return; } final double ax = start.x - center.x; final double ay = start.y - center.y; final double bx = end.x - center.x; final double by = end.y - center.y; final double q1 = ax * ax + ay * ay; final double q2 = q1 + ax * bx + ay * by; final double k2 = 4d / 3d * (sqrt(2d * q1 * q2) - q2) / (ax * by - ay * bx); final float x2 = (float) (center.x + ax - k2 * ay); final float y2 = (float) (center.y + ay + k2 * ax); final float x3 = (float) (center.x + bx + k2 * by); final float y3 = (float) (center.y + by - k2 * bx); path.cubicTo(x2, y2, x3, y3, end.x, end.y); } }