How to center the gradient color in android

时间:2017-12-18 06:19:26

标签: android gradient

Hi I am trying to gradient in android. I am trying the following code

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

Result:

OP image

I want to increase the size of white in center. Please help me to solve my issue.

2 个答案:

答案 0 :(得分:4)

Try this :

enter image description here

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

答案 1 :(得分:1)

You need to specify type inside the gradient tag,

For e.g. in my case I have defined radial for center gradient effect,

android:type="radial"

I have created gradient shape (centergradietbg.xml) with center gradient effect, look it my code

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

Use this xml in your layout background using

android:background="@drawable/centergradietbg"