我有一个 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 度开始(类似于在绘制圆弧时完成的平移)。换句话说,如果我有一个从白色到蓝色的渐变,我需要将圆弧的顶部涂成白色,然后将圆弧的最后部分涂成蓝色。我该怎么做?
最佳答案
使用 Matrix.preRotate
旋转 SweepGradient 的原点:
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);
关于安卓扫描渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403649/