渐变色Drawable

一、线性渐变( android:type=linear)

先说下android:angle属性,该属性代表渐变颜色的角度,默认是0,设值时必须是45的整数倍。该属性只有在type=linear情况下起作用,默认的type为linear。angle=0表示渐变色从左到右; angle=45表示渐变色从左下角到右上角;90表示渐变色从下到上;…

渐变色从左到右的drawable文件linear_gradient:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <gradient
        android:angle="0"
        android:type="linear"
        android:startColor="@color/red"
        android:endColor="@color/yellow"/>
    
</shape>
<ImageView
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:src="@drawable/linear_gradient"
        android:layout_gravity="center_horizontal"/>

在这里插入图片描述

二、放射渐变(type=radial)

1、说下android:centerX、android:centerY这个两个属性,这两个属性表示渐变中心点的x、y坐标,取值范围在0.0到1.0之间,默认为0.5,表示在正中间。只有type=radial,即为放射渐变时设置才有效。

2、android:gradientRadius为渐变的半径,只有type=radial,即为放射渐变时设置才有效。

放射渐变的drawable文件radial_gradient:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <gradient
        android:type="radial"
        android:centerX="0.5"
        android:centerY="0.5"
        android:startColor="@color/red"
        android:endColor="@color/yellow"
        android:gradientRadius="100dp"/>

</shape>
 <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/radial_gradient"
        android:layout_gravity="center_horizontal"/>

在这里插入图片描述

将ImageView的宽设置为400dp时:
在这里插入图片描述

三、扫描渐变(type=sweep)

1、扫描渐变的drawable文件sweep_gradient_circle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">

    <gradient
        android:type="sweep"
        android:endColor="@color/yellow"
        android:centerColor="@color/blue"
        android:startColor="@color/red"/>

</shape>
 <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/sweep_gradient_circle"
        android:layout_gravity="center_horizontal"/>

在这里插入图片描述

shape内各个属性的说明:https://www.cnblogs.com/lang-yu/p/6112052.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值