其他分享
首页 > 其他分享> > 渐变色Drawable

渐变色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

标签:linear,渐变色,渐变,radial,Drawable,type,android
来源: https://blog.csdn.net/sqf251877543/article/details/88064499