旋转渐变而不是椭圆

Rotating the gradient not the oval

我有一个带有扫描渐变的椭圆形,我想旋转渐变而不是椭圆本身(因为当我旋转椭圆时它不再处于正确的位置)。我找不到任何关于此的信息。有任何想法吗?这就是我想要做的

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <stroke
                android:width="1dp"
                android:color="#585858" />
            <rotate
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="40">
                <gradient
                    android:endColor="#FF7DD6"
                    android:startColor="#FFFFFF"
                    android:type="sweep"
                    android:useLevel="false" />
            </rotate>

        </shape>

    </item>

</layer-list>

gradient 有一个属性 angle,它采用一个 int 值来为渐变提供方向。

根据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.

通过改变角度,这应该会旋转你的渐变。

例如:

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

如果您使用的是 SweepGradient,可以使用 setLocalMatrix 方法:

 float initial_rotation_angle_degrees = 30; 
 SweepGradient gradient = new SweepGradient(0,0
                        ,new int[]{
                        ,0xff0078be
                        ,0xfff4535c
                        }
                        ,null);
        Matrix matrix = new Matrix();
// 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);