Android - How to draw an arc based gradient Android - How to draw an arc based gradient android android

Android - How to draw an arc based gradient


The solution for this is to set the position of BLUE. This is done like so:

int[] colors = {Color.RED, Color.BLUE};float[] positions = {0,1};SweepGradient gradient = new SweepGradient(100, 100, colors , positions);

The problem here is that when setting the position of BLUE to '1' this does not mean that it will be positioned at the end of the drawn arc, but instead at the end of the circle of which the arc is part of.To solve this, BLUE position should take into account the number of degrees in the arc. So if I'm drawing an arc with X degrees, position will be set like so:

float[] positions = {0,Xf/360f};

So if X is 90, the gradient will place BLUE at 0.25 of the circle:

enter image description here


To add to Yoav's solution.

On my Galaxy Nexus 4.2 stock Android, the solution given doesn't work.

If the array doesn't contain a 0.0f and 1.0f, it appears to be ignored.

My final solution was:

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


five years later, but the right way is make 0.749f with 0.750f of the separate line, simple code is like:

    val colors = intArrayOf(0xffff0000.toInt(), 0xff0000ff.toInt(), 0xffff0000.toInt(), 0xffff0000.toInt())    val positions = floatArrayOf(0.0f, 0.749f, 0.750f, 1.0f)