Android背景渐变xml

作者: 淡淡_孩子气 | 来源:发表于2018-08-08 15:05 被阅读23次

    使用shape的gradient属性实现渐变

    效果图

    线性渐变
    圆形渐变
    扫描渐变

    线性渐变

    在drawable文件夹下新建shape资源:

    属性值
    • android:angle (必须是45的整数倍. )
      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. 颜色渐变的结束颜色
      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"(必须设置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

    相关文章

      网友评论

        本文标题:Android背景渐变xml

        本文链接:https://www.haomeiwen.com/subject/jrmavftx.html