15

我有一个 SweepGradient 定义为

circle_paint.setShader(new SweepGradient(getWidth()/2, getHeight()/2, new int[] { circle_start_color, circle_end_color}, new float[] { 0f, 1f}))

应用于定义为的拱门

canvas.drawArc(circle_bounds, circle_start_perc*360f, circle_end_perc*360f, true, circle_paint);

这很好用,但我需要拱门从屏幕顶部开始绘制,即

canvas.drawArc(circle_bounds, ((circle_start_perc*360f)-90f)%360, circle_end_perc*360f, true, circle_paint);

问题是 SweepGradient 似乎仍然从 0 度开始,我需要它从 270 度开始(类似于弧线图上的平移)。换句话说,如果我有一个从白色到蓝色的渐变,我需要将弧的顶部涂成白色,然后将弧的最后部分涂成蓝色。我怎样才能做到这一点?

4

2 回答 2

22

使用 a 旋转 SweepGradient 的原点Matrix.preRotate

final int[] colors = {circle_start_color, circle_end_color};
final float[] positions = {0f, 1f};
Gradient gradient = new SweepGradient(circle_bounds.centerX(), circle_bounds.centerY(), colors, positions);
float rotate = 270f;
Matrix gradientMatrix = new Matrix();        
gradientMatrix.preRotate(rotate, circle_bounds.centerX(), circle_bounds.centerY());
gradient.setLocalMatrix(gradientMatrix);
mPaint.setShader(gradient);
于 2015-07-29T09:09:41.230 回答
20

您可以尝试使用getLocalMatrix()setLocalMatrix()SweepGradient着色器应用旋转。您可以获取当前矩阵,发布适当的旋转,postRotate()然后将其设置回着色器元素。

另一种选择是旋转Canvas。您可以预先旋转画布,绘制内容,然后恢复它;或者先绘制内容,然后再旋转画布。

于 2012-11-15T19:03:22.553 回答