本文介绍了如何绘制不同的填充和笔触颜色为每个面上有一个弧形段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已得到另一SO质疑。我想知道我怎么可以修改它,使连接圈,内圈周长线有不同的颜色比外颜色peremiter。说黑色的外圈和所有其他行程是灰色的。

 私有静态最终浮动CIRCLE_LIMIT = 359.9999f;
/ **
 *绘制定义的角度之间的厚弧线,见{@link帆布#drawArc}更多。
 *此方法等效于
 *< pre>< code>
 *浮动RMID =(林恩+罗村)/ 2;
 * paint.setStyle(Style.STROKE); //没有什么填
 * paint.setStrokeWidth(罗村 - 林恩); //厚度
 * canvas.drawArc(新RectF(CX - RMID,CY - RMID,CX + RMID,CY + RMID),由startAngle,sweepAngle,假漆);
 *< / code>< / pre>
 *但是支持不同的填充和笔触油漆。
 *
 * @参数帆布
 椭圆形的*参数CX水平中点
 * @参数CY垂直椭圆形的中间点
 * @参数RINN弧段的内半径
 *弧段的@参数狂胜外半径
 * @参数startAngle开始看到{@link帆布#drawArc}
 * @参数sweepAngle见{@link帆布#drawArc},AT&安培封顶; plusmn; 360
 * @参数填充灌装油漆,可与LT; code>空< / code>
 * @参数行程行程漆,可与LT; code>空< / code>
 * @see帆布#drawArc
 * /
公共静态无效drawArcSegment(帆布油画,浮CX,CY浮动,浮动林恩,浮溃败,浮startAngle开始,
        浮sweepAngle,补漆,油漆中风){
    如果(sweepAngle> CIRCLE_LIMIT){
        sweepAngle = CIRCLE_LIMIT;
    }
    如果(sweepAngle< -CIRCLE_LIMIT){
        sweepAngle = -CIRCLE_LIMIT;
    }    RectF outerRect =新RectF(CX - 罗村,CY - 罗村,CX +溃败,CY +狂胜);
    RectF innerRect =新RectF(CX - 林恩,CY - 林恩,CX +林恩,CY +林恩);    路径segmentPath =新路径();
    双启动= toRadians(startAngle开始);
    segmentPath.moveTo((浮点)(CX +林恩* COS(开始)),(浮点)(CY +林恩*罪(开始)));
    segmentPath.lineTo((浮点)(CX +狂胜* COS(开始)),(浮点)(CY +狂胜*罪(开始)));
    segmentPath.arcTo(outerRect,startAngle开始,sweepAngle);
    双端= toRadians(由startAngle + sweepAngle);
    segmentPath.lineTo((浮点)(CX +林恩* COS(完)),(浮点)(CY +林恩*罪(完)));
    segmentPath.arcTo(innerRect,由startAngle + sweepAngle,-sweepAngle);
    如果(个够!= NULL){
        canvas.drawPath(segmentPath,填写);
    }
    如果(行程!= NULL){
        canvas.drawPath(segmentPath,中风);
    }
}

基本上功能参数在理想情况下是这样的:

 公共静态无效drawArcSegment(帆布油画,浮CX,CY浮动,浮动林恩,浮溃败,浮startAngle开始,浮sweepAngle,油漆填料,涂料strokeOutCircle,油漆StrokeAll)


解决方案

您需要用不同的涂料直接通过分别绘制环段的部分绘制* 从方法相同的计算端点作为填充的边界。在路径尚需虽然填充它。我进了一步,并分割你的 strokeAll strokeSides + strokeOuter 更容易重用。请看下面的一堆线:

  / **
 *绘制定义的角度之间的厚弧线,见{@link帆布#drawArc}更多。
 *此方法等效于
 *< pre>< code>
 *浮动RMID =(林恩+罗村)/ 2;
 * paint.setStyle(Style.STROKE); //没有什么填
 * paint.setStrokeWidth(罗村 - 林恩); //厚度
 * canvas.drawArc(新RectF(CX - RMID,CY - RMID,CX + RMID,CY + RMID),由startAngle,sweepAngle,假漆);
 *< / code>< / pre>
 *但是支持不同的填充和笔触油漆。
 *
 椭圆形的*参数CX水平中点
 * @参数CY垂直椭圆形的中间点
 * @参数RINN弧段的内半径
 *弧段的@参数狂胜外半径
 * @参数startAngle开始看到{@link帆布#drawArc}
 * @参数sweepAngle见{@link帆布#drawArc},AT&安培封顶; plusmn; 360
 * @参数填充灌装油漆,可与LT; code>空< / code>
 *为内环段@param strokeInner中风漆,可与LT; code>空< / code>
 *为外环段@param strokeOuter中风漆,可与LT; code>空< / code>
 * @参数strokeSides中风漆用于连接环段的两端线,可与LT; code>空< / code>
 * @see帆布#drawArc
 * /
公共静态无效drawArcSegment(帆布油画,浮CX,CY浮动,浮动林恩,浮溃败,浮startAngle开始,
        浮sweepAngle,补漆,油漆strokeInner,油漆strokeOuter,油漆strokeSides){
    如果(sweepAngle> CIRCLE_LIMIT){
        sweepAngle = CIRCLE_LIMIT;
    }
    如果(sweepAngle< -CIRCLE_LIMIT){
        sweepAngle = -CIRCLE_LIMIT;
    }    RectF outerRect =新RectF(CX - 罗村,CY - 罗村,CX +溃败,CY +狂胜);
    RectF innerRect =新RectF(CX - 林恩,CY - 林恩,CX +林恩,CY +林恩);    如果(个够!= NULL || strokeSides!= NULL){//为prevent计算这个地段的花车
        双启动= toRadians(startAngle开始);
        双端= toRadians(由startAngle + sweepAngle);
        浮动innerStartX =(浮点)(CX +林恩* COS(开始));
        浮动innerStartY =(浮点)(CY +林恩*罪(开始));
        浮动innerEndX =(浮点)(CX +林恩* COS(结束));
        浮动innerEndY =(浮点)(CY +林恩*罪(结束));
        浮动outerStartX =(浮点)(CX +狂胜* COS(开始));
        浮动outerStartY =(浮点)(CY +狂胜*罪(开始));
        浮动outerEndX =(浮点)(CX +狂胜* COS(结束));
        浮动outerEndY =(浮点)(CY +狂胜*罪(结束));
        如果(个够!= NULL){
            路径segmentPath =新路径();
            segmentPath.moveTo(innerStartX,innerStartY);
            segmentPath.lineTo(outerStartX,outerStartY);
            segmentPath.arcTo(outerRect,startAngle开始,sweepAngle);
            //目前outerEndX,outerEndY路径
            segmentPath.lineTo(innerEndX,innerEndY);
            segmentPath.arcTo(innerRect,由startAngle + sweepAngle,-sweepAngle); //向后拉
            canvas.drawPath(segmentPath,填写);
        }
        如果(strokeSides!= NULL){
            canvas.drawLine(innerStartX,innerStartY,outerStartX,outerStartY,strokeSides);
            canvas.drawLine(innerEndX,innerEndY,outerEndX,outerEndY,strokeSides);
        }
    }
    如果(strokeInner!= NULL){
        canvas.drawArc(innerRect,startAngle开始,sweepAngle,假的,strokeInner);
    }
    如果(strokeOuter!= NULL){
        canvas.drawArc(outerRect,startAngle开始,sweepAngle,假的,strokeOuter);
    }
}

I have got this code from another SO question. I was wondering how I could modify it so that the lines connecting the circles and inner circle perimeter have a different color than the outer color peremiter. Say Black color for the outer circle and every other stroke is grey.

private static final float CIRCLE_LIMIT = 359.9999f;
/**
 * Draws a thick arc between the defined angles, see {@link Canvas#drawArc} for more.
 * This method is equivalent to
 * <pre><code>
 * float rMid = (rInn + rOut) / 2;
 * paint.setStyle(Style.STROKE); // there's nothing to fill
 * paint.setStrokeWidth(rOut - rInn); // thickness
 * canvas.drawArc(new RectF(cx - rMid, cy - rMid, cx + rMid, cy + rMid), startAngle, sweepAngle, false, paint);
 * </code></pre>
 * but supports different fill and stroke paints.
 *
 * @param canvas
 * @param cx horizontal middle point of the oval
 * @param cy vertical middle point of the oval
 * @param rInn inner radius of the arc segment
 * @param rOut outer radius of the arc segment
 * @param startAngle see {@link Canvas#drawArc}
 * @param sweepAngle see {@link Canvas#drawArc}, capped at &plusmn;360
 * @param fill filling paint, can be <code>null</code>
 * @param stroke stroke paint, can be <code>null</code>
 * @see Canvas#drawArc
 */
public static void drawArcSegment(Canvas canvas, float cx, float cy, float rInn, float rOut, float startAngle,
        float sweepAngle, Paint fill, Paint stroke) {
    if (sweepAngle > CIRCLE_LIMIT) {
        sweepAngle = CIRCLE_LIMIT;
    }
    if (sweepAngle < -CIRCLE_LIMIT) {
        sweepAngle = -CIRCLE_LIMIT;
    }

    RectF outerRect = new RectF(cx - rOut, cy - rOut, cx + rOut, cy + rOut);
    RectF innerRect = new RectF(cx - rInn, cy - rInn, cx + rInn, cy + rInn);

    Path segmentPath = new Path();
    double start = toRadians(startAngle);
    segmentPath.moveTo((float)(cx + rInn * cos(start)), (float)(cy + rInn * sin(start)));
    segmentPath.lineTo((float)(cx + rOut * cos(start)), (float)(cy + rOut * sin(start)));
    segmentPath.arcTo(outerRect, startAngle, sweepAngle);
    double end = toRadians(startAngle + sweepAngle);
    segmentPath.lineTo((float)(cx + rInn * cos(end)), (float)(cy + rInn * sin(end)));
    segmentPath.arcTo(innerRect, startAngle + sweepAngle, -sweepAngle);
    if (fill != null) {
        canvas.drawPath(segmentPath, fill);
    }
    if (stroke != null) {
        canvas.drawPath(segmentPath, stroke);
    }
}

Basically the function parameter would ideally look like:

   public static void drawArcSegment(Canvas canvas, float cx, float cy, float rInn, float rOut, float startAngle, float sweepAngle, Paint fill, Paint strokeOutCircle, Paint StrokeAll)
解决方案

You need to draw the parts of the ring segment separately with different paints directly via draw* methods from the same calculated endpoints as the filling's bounds. The Path is still needed for filling it though. I went one step further and split your strokeAll to strokeSides+strokeOuter for easier reuse. Consider the following bunch of lines:

/**
 * Draws a thick arc between the defined angles, see {@link Canvas#drawArc} for more.
 * This method is equivalent to
 * <pre><code>
 * float rMid = (rInn + rOut) / 2;
 * paint.setStyle(Style.STROKE); // there's nothing to fill
 * paint.setStrokeWidth(rOut - rInn); // thickness
 * canvas.drawArc(new RectF(cx - rMid, cy - rMid, cx + rMid, cy + rMid), startAngle, sweepAngle, false, paint);
 * </code></pre>
 * but supports different fill and stroke paints.
 *
 * @param cx horizontal middle point of the oval
 * @param cy vertical middle point of the oval
 * @param rInn inner radius of the arc segment
 * @param rOut outer radius of the arc segment
 * @param startAngle see {@link Canvas#drawArc}
 * @param sweepAngle see {@link Canvas#drawArc}, capped at &plusmn;360
 * @param fill filling paint, can be <code>null</code>
 * @param strokeInner stroke paint for inner ring segment, can be <code>null</code>
 * @param strokeOuter stroke paint for outer ring segment, can be <code>null</code>
 * @param strokeSides stroke paint for lines connecting the ends of the ring segments, can be <code>null</code>
 * @see Canvas#drawArc
 */
public static void drawArcSegment(Canvas canvas, float cx, float cy, float rInn, float rOut, float startAngle,
        float sweepAngle, Paint fill, Paint strokeInner, Paint strokeOuter, Paint strokeSides) {
    if (sweepAngle > CIRCLE_LIMIT) {
        sweepAngle = CIRCLE_LIMIT;
    }
    if (sweepAngle < -CIRCLE_LIMIT) {
        sweepAngle = -CIRCLE_LIMIT;
    }

    RectF outerRect = new RectF(cx - rOut, cy - rOut, cx + rOut, cy + rOut);
    RectF innerRect = new RectF(cx - rInn, cy - rInn, cx + rInn, cy + rInn);

    if (fill != null || strokeSides != null) { // to prevent calculating this lot of floats
        double start = toRadians(startAngle);
        double end = toRadians(startAngle + sweepAngle);
        float innerStartX = (float)(cx + rInn * cos(start));
        float innerStartY = (float)(cy + rInn * sin(start));
        float innerEndX = (float)(cx + rInn * cos(end));
        float innerEndY = (float)(cy + rInn * sin(end));
        float outerStartX = (float)(cx + rOut * cos(start));
        float outerStartY = (float)(cy + rOut * sin(start));
        float outerEndX = (float)(cx + rOut * cos(end));
        float outerEndY = (float)(cy + rOut * sin(end));
        if (fill != null) {
            Path segmentPath = new Path();
            segmentPath.moveTo(innerStartX, innerStartY);
            segmentPath.lineTo(outerStartX, outerStartY);
            segmentPath.arcTo(outerRect, startAngle, sweepAngle);
            // Path currently at outerEndX,outerEndY
            segmentPath.lineTo(innerEndX, innerEndY);
            segmentPath.arcTo(innerRect, startAngle + sweepAngle, -sweepAngle); // drawn backwards
            canvas.drawPath(segmentPath, fill);
        }
        if (strokeSides != null) {
            canvas.drawLine(innerStartX, innerStartY, outerStartX, outerStartY, strokeSides);
            canvas.drawLine(innerEndX, innerEndY, outerEndX, outerEndY, strokeSides);
        }
    }
    if (strokeInner != null) {
        canvas.drawArc(innerRect, startAngle, sweepAngle, false, strokeInner);
    }
    if (strokeOuter != null) {
        canvas.drawArc(outerRect, startAngle, sweepAngle, false, strokeOuter);
    }
}

这篇关于如何绘制不同的填充和笔触颜色为每个面上有一个弧形段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:57
查看更多