如何定位径向渐变背景

22
我可以使用一个线性布局放置一个径向渐变形状作为背景吗?这是我现在拥有的:
这是形状:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:endColor="#e6e6e6"
        android:gradientRadius="800"
        android:startColor="#fafaf9"
        android:type="radial"/>
</shape>

LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/accueil_bg_gradient">
</LinearLayout>

我只想让我的渐变从屏幕左上角开始,并在右下角结束。

非常感谢!


如果想要改变渐变的角度,请尝试在<gradient>元素中使用android:angle。<gradient> - m039
谢谢您的回答,但它是一个径向渐变。 - alxscms
2个回答

49

您可以使用渐变的"centerX"和 "centerY"属性将径向渐变的中心移动到可绘制对象的不同位置。 它们是浮点值,范围从0到1.0,其中(相应的centerX,centerY值) 0,0代表左上角,1,1代表右下角。

默认值为0.5,0.5,即可绘制空间的中心位置。例如,对于一个半径为100px,黑白渐变的示例,其中心起始点为左上角:

    <shape android:shape="rectangle">
        <gradient
            android:type="radial"
            android:startColor="#ffffff"
            android:endColor="#000000"
            android:gradientRadius="100"
            android:angle="270"
            android:centerX="0"
            android:centerY="0"/>

    </shape>

-1

将这个文件设置为背景 mybackground.xml:

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

    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="10dp" android:color="@color/darkBlue" />
            <solid android:color="#00000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <gradient android:startColor="@color/grayBlue"
                android:endColor="@color/lightBlue" android:angle="270"
                android:centerColor="@color/lightBlue" />
            <!-- border width and color -->
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
            <padding android:left="10dp" android:top="8dp" android:right="10dp"
                android:bottom="8dp" />
        </shape>
    </item>

</layer-list>

嗨,谢谢,但你给我的代码是线性渐变的,我想要一个以左上角为中心,右下角为结束点的径向渐变。 - alxscms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接