Search code examples
androidindexoutofboundsexceptionsweepgradient

SweepGradient ArrayIndexOutOfBoundsException


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


Solution

  •     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.