如何定位的径向渐变背景背景

2023-09-05 07:12:05 作者:只为等一个答案

我如何定位一个径向渐变形状为背景的的LinearLayout?以下是我presently有:

How can I position a radial gradient shape as background in a LinearLayout ? Here is what I presently have :

形状:

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

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

我只是想有从屏幕的左上角我的梯度开始,并在右下角结束。

I just want to have my gradient starting from the left upper corner of the screen, and ending at the right lower corner.

非常感谢!

推荐答案

您可以在绘制中使用的centerX和梯度centerY属性的不同位置移动的径向渐变的中间。它们是一个浮点值范围从0到1.0,其中(的centerX的值,相应地centerY)0,0是左上和1,1-是右下角。

You can move the middle of the radial gradient at a different position of the drawable using "centerX" and "centerY" attributes of the gradient. They are a floating point values ranging from 0 to 1.0 where (values of centerX,centerY accordingly) 0,0 is upper left and 1,1 is bottom right corner.

默认为0.5,0.5这是可拉伸/分配空间的中间。 例如,对于一个100像素长(半径),黑白渐变的中间开始于左上角是:

Default is 0.5,0.5 which is the middle of the drawable / assigned space. Example for a 100px long (radius), black-white gradient whose middle starts at upper left corner would be:

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