我设置着色器这样如何更改着色器中的Dirrection渐变?
paint.setShader(new LinearGradient(0, 0, 0, height, Color.RED, Color.BLUE, Shader.TileMode.REPEAT));
在图像梯度依赖于高度。从底部到顶部的线条从蓝色变成红色。
我想要什么 - 颜色仍然应该依赖于高度,但从底部到顶部的整行必须具有相同的颜色。从最长点开始的垂直线在整个长度上应该是红色的。
我该怎么做?
我设置着色器这样如何更改着色器中的Dirrection渐变?
paint.setShader(new LinearGradient(0, 0, 0, height, Color.RED, Color.BLUE, Shader.TileMode.REPEAT));
在图像梯度依赖于高度。从底部到顶部的线条从蓝色变成红色。
我想要什么 - 颜色仍然应该依赖于高度,但从底部到顶部的整行必须具有相同的颜色。从最长点开始的垂直线在整个长度上应该是红色的。
我该怎么做?
我不认为你可以找到容易实现的东西。但无论如何,你必须使用另一个构造的梯度,则:
LinearGradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)
而且颜色应改变宽度的方式,但你必须填写颜色阵列,根据你的图表点颜色。但在这种情况下,你应该根据图上的值计算你自己的颜色。
您可以使用HSVToColor函数来计算颜色:
float hoffset = 240;
float h = yourvalue + hoffset; // Those value should be not more than 360, so yourvalue should be reduced to 0 (Blue) till 120 (Red)
float s = 1.0f;
float v = 1.0f;
float[] hsv = {h, s, v};
Color.HSVToColor(hsv);
不能帮助,同时进一步看不到自己的代码。
您正在使用对象的参数是:X0,Y0,X1,Y1
你告诉它在0,0,0,高度 开始,但它似乎像你想要的是:0 ,0,宽度,0
int width = yourObject.getWidth();
new LinearGradient(0, 0, width, 0, Color.RED, Color.BLUE, Shader.TileMode.REPEAT);
我更新了我的问题,使其更容易混淆。 – Yarh
首先设定梯度模式来Shader.TileMode.CLAMP
,那么你应该尝试矩阵转换。例如,
LinearGradient gradient = new LinearGradient(0, 0, 0, height, Color.RED, Color.BLUE, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setTranslate(0, 100);
matrix.postScale(1f, 0.75f);
gradient.setLocalMatrix(matrix);
paint.setShader(gradient);
其中一种方法是根本不使用LinearGradient。您应该计算从蓝色到红色的颜色值,并将它们存储在一个数组中。然后根据当前身高使用存储的颜色。如果可以更改,您可以修改此解决方案以适应您自己的高度和宽度。
int colorHeight = aHeight; // number of pixels. Should be the height of your view.
int[] shadeColors = new int[colorHeight];
float step = 255/colorHeight;
// Defining colors going from blue to red: 00 00 FF to FF 00 00
int blue = 255;
int red = 0;
for(int i = 0; i < colorHeight; i++){
shadeColors [i] = Color.rgb(red, 0, blue - red);
red+=step;
}
这会为您提供从蓝色到红色的渐变着色效果。你将不得不使用这种颜色的数组,以便在正确的索引处停下来。
示例如果你有一个高度或酒吧阵列显示。让我们说你把它们声明为int []吧;
for(int i = 0; i < bars.length; i++){
for(int j = 0; j < bars[i]; j++){
colorArray[bars[i]][j] = shadeColors [j];
}
}
在这里,你去。
我更新了我的问题,使其更容易混淆。 – Yarh