我正在尝试在XY图上创建路径。我想为路径指明颜色,该颜色将指示用户的体重。
我尝试了以下代码:
private final float[] PERCENT_COLORS = {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f, 1.2f};
public float[] normalizedPercentColors = {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f, 1.2f};
private final int[] COLOR_SCALE = {
Color.rgb(210, 210, 210),
Color.rgb(255, 255, 0),
Color.rgb(102, 204, 51),
Color.rgb(51, 81, 220),
Color.rgb(171, 53, 133),
Color.rgb(210, 39, 39),
Color.rgb(0, 0, 0)};
@Override
public void onGenerateColors(TreadmillApp.TmData data) {
reportColors.add(generateColor((data.totalForce * .1f / (float) mApp.weightValue)));
}
public int generateColor(float percent) {
if (percent >= 1.20) {
return Color.rgb( 0, 0, 0);
} else if (percent < normalizedPercentColors[0]) {
return Color.rgb(100, 100, 100);
}
int i = 1;
for (; i < normalizedPercentColors.length - 1; i++) {
if (percent < normalizedPercentColors[i]) {
break;
}
}
float lower = normalizedPercentColors[i - 1];
float upper = normalizedPercentColors[i];
float range = upper - lower;
float percentUpper = (percent - lower) / range;
float percentLower = 1 - percentUpper;
int red = (int) Math.floor(Color.red(COLOR_SCALE[i - 1]) * percentLower + Color.red(COLOR_SCALE[i]) * percentUpper);
int green = (int) Math.floor(Color.green(COLOR_SCALE[i - 1]) * percentLower + Color.green(COLOR_SCALE[i]) * percentUpper);
int blue = (int) Math.floor(Color.blue(COLOR_SCALE[i - 1]) * percentLower + Color.blue(COLOR_SCALE[i]) * percentUpper);
return Color.rgb(red, green, blue);
}
public void setColors(ArrayList<Integer> reportColors) {
int[] colors = Ints.toArray(reportColors);
Shader shader = new LinearGradient(0, 0, 0, mPointArray.size(), colors,
null, Shader.TileMode.REPEAT);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
pathPaint.setShader(shader); //paint to set path
}
根据我在另一个问题中找到的示例,将路径设置为pathPaint
。但是,setColors
似乎不适用于复杂的路径。我想知道,是否有什么方法可以为路径设置多种颜色,而不必每次为路径设置颜色时都创建新路径?