android对颜色渐变的处理和C#WPF很像。
先看一下xml的表达:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:angle="0"
android:gradientRadius="150dp"
android:startColor="#33f56685"
android:endColor="#00f98483"
android:centerX="0.5"
android:centerY="0.3" />
</shape>
核心就是<gradient/>标签。
1. android:type, 指的是颜色渐变的方向。它有三种类型:
- linear,线性渐变,以直线的方式渐变。
- radial,中心渐变, 以圆心为出发点,向四周均匀渐变。
- sweep,环扫渐变, 扇形或者环形的方式渐变。
2. android:gradientRadius, 是指整个渐变空间区域的半径范围。只用于radial渐变。简单的认为就是整个颜色的宽高大小。 取值可以是具体的dp长度, 也可以是float值,代表占View大小的百分比。
3. android:startColor,android:centerColor,android:endColor, 是指,渐变的起始颜色,中间颜色,结束颜色。相当于,先从起始颜色变化到中间颜色,再从中间颜色变化到结束颜色。android:centerColor是可选设置,其两个必须设置。
4. android:centerX和android:centerY, 它们的值其实是比例值[0,1],代表中心在View中位值。可以把渐变的区域,上下左右的移动。对与Linear渐变,可以设置centerColor的位置。
5. android:angle="0", 就是渐变的方向。只能用于Linear渐变,必须是45的倍数[0, 315]。可以做到从各个方向渐变。
6. android:userlevel, 适用于LevelListDrawable。详见LevelListDrawable。