tags:

views:

11

answers:

1

Hey everyone, 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?

+1  A: 

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;

so the x never changes in the gradient only the y does

so basically

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);

Graeme G
Works like a charm! I knew it had to be simple... and yet I don't think I'd ever get it. Thanks a lot!
akoprowski