如何在画布中仅绘制弧的一部分

时间:2012-05-10 06:38:01

标签: android

我正在开发一个模拟时钟小部件,时钟拨号是一个图像。 我想在图像中绘制一个弧段(如橙色所示)。

paint.setColor(Color.GREEN);
paint.setStrokeWidth(20);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(100, 100, 200, paint);

我尝试使用drawcircle和drawArc,但由于我只想要一部分弧而不是一个完整的弧,所以无法继续。有什么想法吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

我让这堂课希望它对你有帮助,所有变量都是西班牙语,但很简单,

构造函数SemiCirculo用作参数半圆的rgb和分辨率(半圆的三角形数)

CalcularPuntosPorcentaje方法使用圆心,起始角度,角度宽度和无线电作为参数。

方法ImprimeCirculo使用画布作为参数,一旦用已经提到的方法对半圆的点进行了钙化,它就用于绘制半圆。

CalcularPuntosPorcentaje方法类似于CalcularPuntosPorcentaje,但是它使用的起始角度和宽度角度参数为0到100之间

最后,SetOffset和SetColor用于更改角度和半圆颜色的默认起始位置参考

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;


public class SemiCirculo {

    private Path circulo;
    private Paint color;
    private float px, py, radio, anguloI, anchoa,offset;
    private int r, g, b;
    private int resolucion;
    private float puntox[],puntoy[];


    public SemiCirculo(int r1, int g1, int b1, int resolucion1) {

        this.offset = 0;
        this.color = new Paint();
        this.r = r1;
        this.g = g1;
        this.b = b1;
        this.color.setColor(Color.rgb(r, g, b));
        color.setAntiAlias(true);
        circulo = new Path();
        this.resolucion = resolucion1;
        this.puntox = new float[this.resolucion];
        this.puntoy = new float[this.resolucion];
        this.anguloI = 0;
        this.anchoa = 1;

    }

    public void SetOffset(float off) {
        this.offset = off;
    }

    public void SetColor(int r1,int g1, int b1){        
        this.r=r1;
        this.g=g1;
        this.b=b1;
        this.color.setColor(Color.rgb(r, g, b));
    }

    public void CalcularPuntosPorcentaje(float px1, float py1,
            float porcentaje, float radio1) {
        this.anguloI = 0 + this.offset;
        this.px = px1;
        this.py = py1;
        this.radio = radio1;
        this.anguloI = 0;
        this.anchoa = porcentaje / 100 * 360;

        this.CalcularPuntos(px, py, anguloI, anchoa, radio);
    }

    public void CalcularPuntos(float px1, float py1, float anguloI1,
            float anchoangulo, float radio1) {
        this.anguloI = anguloI1 + this.offset;

        this.anchoa = anchoangulo;
        this.px = px1;
        this.py = py1;
        this.radio = radio1 ;

        float angulo = 360 - this.anguloI - this.anchoa;

        for (int i = 0; i < resolucion; i++) {
            this.puntox[i] = this.px - (float) Math.sin(Math.toRadians(angulo))
                    * this.radio;
            this.puntoy[i] = this.py - (float) Math.cos(Math.toRadians(angulo))
                    * this.radio;
            angulo = (360 - this.anguloI - this.anchoa)
                    + ((this.anchoa / (float) (this.resolucion)) * (i + 2));
        }

        this.circulo.reset();

        this.circulo.moveTo(this.px, this.py);
        for (int i = 0; i < resolucion; i++) {
            this.circulo.lineTo(this.puntox[i], this.puntoy[i]);
        }

    }

    public void ImprimeCirculo(Canvas canvas) {

        canvas.drawPath(this.circulo, this.color);

    }

}

答案 1 :(得分:0)

您需要使用此方法:

canvas.drawArc(innerRect, -11.0f, 11.0f + 6.0f, true, paintx);
文档的

见这里:

http://developer.android.com/reference/android/graphics/Canvas.html#drawArc%28android.graphics.RectF,%20float,%20float,%20boolean,%20android.graphics.Paint%29

务必正确设置角度参数!并使用浮动!

第一个角度是弧线的起点,第二个角度参数是扫掠角度,即角度应该是多少度 - 顺时针测量。

尝试一下,它肯定会起作用。只是需要一点点玩: - )