3

I am trying to draw a gradient to the background of AndroidPlot, all I need is an Object of Paint.

So I would use this code:

int[] co = new int[]{Color.RED,Color.YELLOW,Color.GREEN,Color.YELLOW,Color.RED};

float[] coP = new float[]{0.1f,0.1f,0.6f,0.1f,0.1f};

      >Paint pa = new Paint();

      >pa.setAlpha(200);

      >pa.setShader(new LinearGradient(0,0,250,graphv.getHeight(),co,coP,Shader.TileMode.REPEAT));

But the background is only one color: RED.

I don't know why, or how to fix it..

Do you have any ideas?

Pommes9485
  • 75
  • 3
  • 7
  • Another option would be to create the gradient in Photoshop and stretch it horizontal. But I don`t know how to set a bitmap as the background of the plot or of the paint object. – Pommes9485 Aug 22 '13 at 14:02

3 Answers3

6

Doc says:

@param positions May be NULL. If this is NULL, the the colors are distributed evenly between the start and end point. If this is not null, the values must begin with 0, end with 1.0, and intermediate values must be strictly increasing.

So you can

1) set NULL

2) float[] positions = new float[]{ 0.1f, 0.3f, 0.5f, 0.7f, 0.9f };
Result:
|--------|-------------------------|-----------------------------|-----------------------------|-------------------------|--------|
RED 0.1 RED-YELLOW 0.3 YELLOW-GREEN 0.5 GREEN-YELLOW 0.7 YELLOW-RED 0.9 RED

OR

float[] positions = new float[]{ 0f, 0.3f, 0.5f, 0.7f, 1f };
Result:
|------------------------|-----------------------------|-----------------------------|-------------------------|
0 RED-YELLOW 0.3 YELLOW-GREEN 0.5 GREEN-YELLOW 0.7 YELLOW-RED 1

Artem M
  • 83
  • 1
  • 3
1

LinearGradient Documentation

You can specify an array of colors and the LinearGradient class will automatically draw them distributed evenly along the gradient line.

Example:

float[] positions = null;
int[] colors = {
    Color.BLACK,
    Color.RED,
    Color.GREEN
};
paint.setShader(new LinearGradient(0f, 0f, (float)bounds.width(), 0f, colors, positions, Shader.TileMode.MIRROR));
Elte156
  • 188
  • 2
  • 7
0

There is an example of this in the AndroidPlot demo code

// setup our line fill paint to be a slightly transparent gradient:
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.BLUE, Shader.TileMode.MIRROR));
stepFormatter.setFillPaint(lineFill);
buczek
  • 2,011
  • 7
  • 29
  • 40
  • 1
    Yeah, I know. But I would like to draw a gradient with 5 colors, as you can see in my example code. I use the other constructor, but it does not work.. – Pommes9485 Aug 21 '13 at 14:36