0

I create a SweepGradient like this

int[] colors = { Color.RED, Color.BLUE };
// float[] positions = {0,1}; => this will work without error
float[] positions = { 0 , 280f/360 };
SweepGradient gradient = new SweepGradient(width / 2, height / 2, colors, positions);

When I set float[] positions = {0,1}, it will work correctly (no error).
When I change to float[] positions = { 0 , 280f/360 }, in the Preview mode of AndroidStudio it displays an rendering error but in the simulator it works fine Why this happend? How can I fix it?

enter image description here

Ankita Shah
  • 1,866
  • 16
  • 31
Linh
  • 57,942
  • 23
  • 262
  • 279
  • 1
    the preview is inaccurate, an may render incorrectly. It is a common problem, and not going to be fixed soon. Just ignore the preview errors until it works on the device – Vladyslav Matviienko Dec 13 '16 at 06:02

2 Answers2

1
    int colors[] = {Color.RED, Color.BLUE, Color.BLUE};
    float positions[] = {0.f, 280f / 360, 1.f};

Make sure that the positoins array spans full range i.e. [0.f, 1.0f]. Now the preview will also be shown correctly.

user3161880
  • 1,037
  • 6
  • 13
  • I will check it, but I only want 2 colors not 3 three color. how can I achieve it – Linh Dec 13 '16 at 06:38
  • Please note that the last color is same as second-last color. That will show the constant color in that range. Could you please share the exact expected result via image? – user3161880 Dec 13 '16 at 06:42
0

May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0.

xcz1899
  • 1
  • 1