gpt4 book ai didi

android - SweepGradient Android - 如何设置渐变的起始角度

转载 作者:行者123 更新时间:2023-12-04 10:34:53 27 4
gpt4 key购买 nike

我很难理解如何使用 SweepGradientCanvas 上从特定角度显示我的渐变颜色。

例如:如果我有一条从下午 1 点到 3 点的弧线,我想提供一个 Gradient 作为 ColorGradient 应该从下午 2 点开始。

我有以下代码显示 Color 没有应用任何渐变。

 SweepGradient sweepGradient = new
SweepGradient(provideRectF().width() / 2, provideRectF().height() / 2,
arcColors, new float[]{
0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f
});

Matrix matrix = new Matrix();
matrix.postRotate(currentAngle, provideRectF().width() / 2, provideRectF().height() / 2);
sweepGradient.setLocalMatrix(matrix);

如何让我的 Color 显示给定弧从下午 2 点开始的渐变(根据角度)?

最佳答案

此答案基于 excellent post on SweepGradient作者:Attila Tanyi。

为了更好地理解SweepGradient,让我们首先在整个屏幕上绘制一个SweepGradient,它位于屏幕中间并显示与时钟对应的部分面部(请注意下午 1 点到 2 点之间的区域是纯色,因为这是您设置的一部分):

fullscreen sweep gradient

职位:

// positions for a clock face
// note: we need an opening position 0.0f as well as a closing position 1.0f, both of which belong to 3 pm
float[] positions = {0.0f,
1/12f, 2/12f, 3/12f,
4/12f, 5/12f, 6/12f,
7/12f, 8/12f, 9/12f,
10/12f, 11/12f, 1.0f};

颜色:

int yellow = 0xFFFFFF88;
int blue = 0xFF0088FF;
int white = 0xFFFFFFFF;
int black = 0xFF000000;

// provide as many color values as there are positions
// we want to paint a "normal color" from 1pm to 2pm and then a gradient from 2pm to 3 pm
int[] colors = { black, // the first value is for 3 pm, the sweep starts here
yellow, // 4
white, // 5
black, // 6
white, // 7
yellow, // 8
blue, // 9
black, // 10
white, // 11
black, // 12
blue, // 1 constant color from 1pm to 2pm
blue, // 2
white // the last value also is at 3 pm, the sweep ends here
};

初始化PathPaint:

private Path circle = new Path();
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

// ...
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(30);

我尝试了自定义 View 并在 onLayout() 中配置了渐变。由于帖子是关于画弧的,所以接下来我尝试了一个完整的圆:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// use a square rectangle which does not touch the screen borders:
float squareMaxWidth = Math.min(right - left, bottom - top) - 20;
RectF circleRect = new RectF(left + 20, top + 20, left + squareMaxWidth, top + squareMaxWidth);
// draw a full circle
circle.addArc(circleRect, 180, 360);
// calculate its center
float centerH = (circleRect.right + circleRect.left)*0.5f;
float centerV = (circleRect.bottom + circleRect.top)*0.5f;

sweepGradient = new SweepGradient(centerH,centerV, colors, positions);
paint.setShader(sweepGradient);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(circle, paint);
}

结果:

full circle with sweep gradient

接下来,我们只绘制边框:更改Paint 样式

paint.setStyle(Paint.Style.STROKE);

enter image description here

最后,从下午 1 点到下午 3 点画一条弧线。请记住,下午 3 点的角度为零,钟面上的一个部分对应于 30 度。所以在 onLayout() 中我们有

circle.addArc(circleRect, 300, 60);

arc from 1 pm to 3 pm

另一个例子:如果你想画一条从下午 4:15 到下午 4:45 的弧,渐变从下午 4:33 开始,你会得到下图(在完整的 SweepGradient< 上绘制的彩色弧 在每个“小时”处用黑白交替的圆圈)

arc from 4:15 pm to 4:45 pm with a gradient starting at 4:33 pm

颜色:

int[] colors = {      
primaryColor,
primaryColor,
accentColor };

要计算位置,需要做一些数学运算:

60 minutes = one hour ~ 1/12f
3 minutes = one hour / 20 ~ 1/240f
15 minutes = 5 * 3 minutes ~ 5 / 240f = 1 / 48f
45 minutes = 3 * 15 minutes ~ 1 / 16f
33 minutes = 11 * 3 minutes ~ 11 / 240f

float[] positions = {
(1/12f + 1/48f), // 4:15 pm
(1/12f + 11/240f), // 4:33 pm
(1/12f + 1/16f) // 4:45 pm
};

类似地,可以计算起始角和扫描角的值:

one hour ~ 30 degrees
one minute ~ 0.5 degrees

// start at 4:15 pm:
float startAngle = (30 + 15*0.5f);
// sweep for 1/2 hour:
float sweepAngle = 15f;
circle.addArc(circleRect, startAngle, sweepAngle);

关于android - SweepGradient Android - 如何设置渐变的起始角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60232560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com