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

一、線形漸變

屬性值

  • android:type="linear"

漸變類型為線形漸變

  • android:angle

Integer:漸變顏色的角度(必須是45的整數(shù)倍)
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. 顏色漸變的結束顏色
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>
android:angle="0" 左到右
android:angle="90" 下到上
android:angle="180" 右到左
android:angle="270" 上到下

二、圓形/放射形漸變

屬性值

  • android:type="radial"

漸變類型為圓形/放射形漸變(必須設置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>
圓形漸變

添加屬性android:centerColor="@android:color/holo_blue_light"

圓形漸變 中間色為藍色

三、掃描角度漸變

屬性值

  • 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>
中心點順時針掃描
<?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>
下邊中點順時針掃描
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容