Rotating the gradient not the oval

不打扰是莪最后的温柔 提交于 2019-12-11 18:37:34

问题


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 anything about this. Any ideas? This is what I'm trying to do

<?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>

回答1:


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" />



回答2:


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

 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);


来源:https://stackoverflow.com/questions/38383722/rotating-the-gradient-not-the-oval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!