Skip to content Skip to sidebar Skip to footer

Rotating The Gradient Not The Oval

I have an oval shape with a sweep gradient, I want to rotate the gradient not the oval itself (since when I rotate oval it is not in the right position anymore). I couldn't find an

Solution 1:

If you are using SweepGradient there is setLocalMatrix method that could be used:

floatinitial_rotation_angle_degrees=30; 
 SweepGradientgradient=newSweepGradient(0,0
                        ,newint[]{
                        ,0xff0078be
                        ,0xfff4535c
                        }
                        ,null);
        Matrixmatrix=newMatrix();
// Rotating the gradient 
        matrix.postRotate(initial_rotation_angle_degrees);
// Translating the gradient (The initial cx, cy values must be 0)
        matrix.postTranslate(cx,cy);
        gradient.setLocalMatrix(matrix);

Solution 2:

gradient has an attribute angle that takes an int value to give direction to a gradient.

According to the docs:

The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.

By changing the angle, this should rotate your gradient.

An example might be:

<gradient
     android:endColor="#FF7DD6"
     android:startColor="#FFFFFF"
     android:type="sweep"
     android:angle="270"
     android:useLevel="false" />

Post a Comment for "Rotating The Gradient Not The Oval"