使用shape的gradient属性实现渐变色

一、线形渐变

属性值

  • android:type="linear"

渐变类型为线形渐变

  • android:angle

Integer:渐变颜色的角度(必须是45的整数倍)
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>
下边中点顺时针扫描
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容