Search code examples
androidautomatic-ref-countingpaintgradient

Android: draw arc with gradient from top


The gradient starts from 3 o'clock - however I want it to start from 12.

 int color1 = Color.RED;
 int color2 = Color.BLUE;
 int[] colors = {color1, color2};
 Shader gradient = new SweepGradient(width / 2, height / 2, colors, null);
 mArcPaint.setShader(gradient);

Any suggestions on how to rotate the start to 12? I tried with

 Matrix matrix = new Matrix();
 matrix.postRotate(270f);
 gradient.setLocalMatrix(matrix);

but it did not work for me.


Solution

  • You need to specify Rotate pointX, pointY
    In your case:

    Matrix matrix = new Matrix();
    matrix.postRotate(270f,width / 2, height / 2);
    gradient.setLocalMatrix(matrix);
    

    I use shader draw Circle for showing different case below

    enter image description here