如何将渐变颜色居中 android

How to center the gradient color in android

您好,我正在尝试在 android 中进行渐变。 我正在尝试以下代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#EF5350"
        android:centerColor="#FFEBEE"
        android:endColor="#EF5350"
        android:angle="-270" />
</shape>

结果:

我想增加中心白色的大小。请帮助我解决问题。

试试这个:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="50dp">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#EF5350"
                android:endColor="#EF5350"
                android:centerColor="#FFFFFF"
                android:angle="270"/>
            <size
                android:height="100dp"
                android:width="80dp"/>
        </shape>
    </item>
    <item
        android:bottom="50dp">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#EF5350"
                android:endColor="#FFFFFF"
                android:centerColor="#FFFFFF"
                android:angle="270"
                />
            <size
                android:height="100dp"
                android:width="80dp"/>
        </shape>
    </item>
</layer-list>

您需要在渐变标签内指定类型,

例如在我的例子中,我已经为中心渐变效果定义了 radial

android:type="radial"

我创建了具有中心渐变效果的渐变形状(centergradietbg.xml),看看我的代码

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

    <gradient
        android:centerColor="#c1c1c1"
        android:endColor="#4f4f4f"
        android:gradientRadius="400"
        android:startColor="#c1c1c1"
        android:type="radial" >
    </gradient>

</shape>

在您的布局背景中使用此 xml 使用

android:background="@drawable/centergradietbg"