这必须是一个简单的人,但我真的很茫然......下面code绘制一个矩形,由左到右,从白到黑的线性渐变去,
This must be an easy one but I'm really at a loss... The following code draws a rectangle with a linear gradient going from left to right, from white to black,
int x1 = 0, y1 = 0, x2 = 100, y2 = 40;
Shader shader = new LinearGradient(x1, y1, x2, y2, Color.WHITE, Color.BLACK, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
canvas.drawRect(new RectF(x1, y1, x2, y2), paint);
好了,挺好的。现在我想要做的就是要改变这种渐变成水平一体,使颜色变从白到黑,从上到下。我试图做的是增加:
Ok, fine. Now what I'd like to do is to change this gradient into a horizontal one, so that the color goes from white to black, from top to bottom. What I tried to do is to add:
Matrix trans = new Matrix();
trans.setRotate(90);
shader.setLocalMatrix(trans);
而是梯度推移在一个有趣的天使,或有只是一个单一的颜色......我也试过用各种方式渐变的坐标打(想,也许他们应该转变)无无济于事。我在想什么?
but instead the gradient goes at a funny angel, or there is just a single color... I also tried to play with the coordinates of the gradient in all sorts of way (thinking that maybe they should be transformed) to no avail. What am I missing?
这并不是说我已经做了很多的Android代码,但肯定所有你需要做的是:
Not that I've done much android coding, but surely all you need to do is:
int x1 = 0, y1 = 0, x2 = 0, y2 = 40;
把X永远不会改变渐变只在y一样。
So the x never changes in the gradient only the y does.
所以基本上:
Shader shader = new LinearGradient(0, 0, 0, 40, Color.WHITE, Color.BLACK, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
canvas.drawRect(new RectF(0, 0, 100, 40), paint);