Android - 如何绘制基于弧的渐变

时间:2012-01-27 16:58:34

标签: android graphics 2d geometric-arc

我正在尝试创建一个逐渐从一种颜色到另一种颜色的弧(可变度数)。例如从蓝色到红色:

enter image description here

这是我的代码:

SweepGradient shader = new SweepGradient(center.x, center.y, resources.getColor(R.color.startColor),resources.getColor(R.color.endColor));
Paint paint = new Paint()
paint.setStrokeWidth(1);
paint.setStrokeCap(Paint.Cap.FILL);
paint.setStyle(Paint.Style.FILL);
paint.setShader(shader);
canvas.drawArc(rectF, startAngle, sweepAngle, true, paint);

但结果是整个圆弧都涂上了相同的颜色。

修改
经过更多的实验,我发现颜色的扩散是由弧的角度决定的。如果我绘制一个小角度的圆弧,则只显示第一种颜色。角度越大,绘制的颜色越多。如果角度很小,似乎没有渐变。这是一个例子。我正在绘制4个弧--90,180,270和360:

RectF rect1 = new RectF(50, 50, 150, 150);
Paint paint1 = new Paint();
paint1.setStrokeWidth(1);
paint1.setStrokeCap(Paint.Cap.SQUARE);
paint1.setStyle(Paint.Style.FILL);

SweepGradient gradient1 = new SweepGradient(100, 100,
        Color.RED, Color.BLUE);
paint1.setShader(gradient1);

canvas.drawArc(rect1, 0, 90, true, paint1);

RectF rect2 = new RectF(200, 50, 300, 150);
Paint paint2 = new Paint();
paint2.setStrokeWidth(1);
paint2.setStrokeCap(Paint.Cap.SQUARE);
paint2.setStyle(Paint.Style.FILL);

SweepGradient gradient2 = new SweepGradient(250, 100,
        Color.RED, Color.BLUE);
paint2.setShader(gradient2);

canvas.drawArc(rect2, 0, 180, true, paint2);

RectF rect3 = new RectF(50, 200, 150, 300);
Paint paint3 = new Paint();
paint3.setStrokeWidth(1);
paint3.setStrokeCap(Paint.Cap.SQUARE);
paint3.setStyle(Paint.Style.FILL);

SweepGradient gradient3 = new SweepGradient(100, 250,
        Color.RED, Color.BLUE);
paint3.setShader(gradient3);

canvas.drawArc(rect3, 0, 270, true, paint3);

RectF rect4 = new RectF(200, 200, 300, 300);
Paint paint4 = new Paint();
paint4.setStrokeWidth(1);
paint4.setStrokeCap(Paint.Cap.SQUARE);
paint4.setStyle(Paint.Style.FILL);

SweepGradient gradient4 = new SweepGradient(250, 250,
        Color.RED, Color.BLUE);
paint4.setShader(gradient4);

canvas.drawArc(rect4, 0, 360, true, paint4);

结果如下:

enter image description here

这是令人惊讶的,因为我希望RED处于弧线的起点,最后的BLUE和它们之间的均匀分布,无论角度如何。
我尝试使用positions参数手动分隔颜色但结果是相同的:

int[] colors = {Color.RED, Color.BLUE};
float[] positions = {0,1};
SweepGradient gradient = new SweepGradient(100, 100, colors , positions);

知道如何解决这个问题吗?

4 个答案:

答案 0 :(得分:29)

解决方法是设置BLUE的位置。这样做是这样的:

int[] colors = {Color.RED, Color.BLUE};
float[] positions = {0,1};
SweepGradient gradient = new SweepGradient(100, 100, colors , positions);

这里的问题是,当将BLUE的位置设置为' 1'这并不意味着它将位于绘制弧的末端,而是位于弧所属的圆的末端。 要解决这个问题,蓝色位置应考虑弧度中的度数。因此,如果我用X度绘制弧线,则位置将如此设置:

float[] positions = {0,Xf/360f};

因此,如果X为90,则渐变将BLUE置于圆的0.25处:

enter image description here

答案 1 :(得分:10)

添加到Yoav的解决方案。

在我的Galaxy Nexus 4.2股票Android上,给出的解决方案不起作用。

如果数组不包含0.0f和1.0f,则它似乎被忽略。

我的最终解决方案是:

int[] colors = {Color.RED, Color.BLUE, Color.RED}; 
float[] positions = {0, Xf/360f, 1};

答案 2 :(得分:1)

五年后,但正确的方法是使0.750f与0.750f的单独行,简单的代码如下:

    val colors = intArrayOf(0xffff0000.toInt(), 0xff0000ff.toInt(), 0xffff0000.toInt(), 0xffff0000.toInt())
    val positions = floatArrayOf(0.0f, 0.749f, 0.750f, 1.0f)

答案 3 :(得分:0)

使用SweepGradient,您可以为位置传递null(如果您需要默认渐变),您还可以通过setLocalMatrix 旋转渐变

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    val centerX = width.toFloat() / 2
    val centerY = height.toFloat() / 2

    val paint = Paint()
    paint.isAntiAlias = true

    val colors = intArrayOf(Color.RED, Color.BLUE)
    val gradient = SweepGradient(centerX, centerY, colors, null) // null position
    val matrix = Matrix()
    matrix.postRotate(270f, centerX, centerY) // rotate
    gradient.setLocalMatrix(matrix)
    paint.shader = gradient

    val radius = 400f
    val oval = RectF(centerX - radius,centerY - radius,centerX + radius,centerY + radius)
    canvas?.drawArc(oval, 0f, 360f, false, paint)
}