Android背景漸變xml

使用shape的gradient屬性實現(xiàn)漸變

效果圖

線性漸變

圓形漸變

掃描漸變

線性漸變

在drawable文件夾下新建shape資源:

屬性值
  • android:angle (必須是45的整數(shù)倍. )
    Integer:漸變顏色的角度
    android:angle="0": left to right
    android:angle="90": bottom to top
    android:angle="180": right to left
    android:angle="270": top to bottom
    默認是 0.該屬性只有在type=linear情況下起作用,默認的type為linear。
    默認情況下,從左到右:
  • android:startColor
    Color. 顏色漸變的開始顏色
    android:startColor="#000000"
  • android:endColor
    Color. 顏色漸變的結(jié)束顏色
    android:endColor="#ffffff"
  • android:centerColor
    Color. 顏色漸變的中間顏色,主要用于多彩。
從左到右漸變
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--left to right-->
    <gradient
        android:angle="0"
        android:endColor="@android:color/white"
        android:startColor="@android:color/black" />
    <corners android:radius="4dp" />
</shape>
左到右

依次改變angle角度,90(下到上)、180(右到左)、270(上到下)


下到上.png

右到左.png

image.png

加入 android:centerColor="@android:color/holo_blue_light"


上到下.png

圓形|放射形漸變

屬性值
  • android:type="radial"(必須設(shè)置gradientRadius屬性值,否則會報錯)
    漸變類型
  • android:gradientRadius
    Float.漸變顏色半徑
  • android:centerY
    Float.(0~1.0)相對Y的漸變位置
  • android:centerX
    Float.(0~1.0)相對X的漸變位置
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerX="0.5"
        android:centerY="0.5"
        android:endColor="@android:color/white"
        android:gradientRadius="50"
        android:startColor="@android:color/black"
        android:type="radial" />
</shape>
image.png

加入android:centerColor="@android:color/holo_blue_light"


image.png

掃描角度漸變

屬性值
  • android:type="sweep"(默認centerX=0.5,centerY=0.5由中心點順時針開始掃描)
    漸變類型
  • android:centerY
    Float.(0~1.0)相對Y的漸變位置
  • android:centerX
    Float.(0~1.0)相對X的漸變位置
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="@android:color/white"
        android:startColor="@android:color/black"
        android:type="sweep" />
</shape>
image.png
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerX="0.5"
        android:centerY="1"
        android:endColor="@android:color/white"
        android:startColor="@android:color/black"
        android:type="sweep" />
</shape>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。