百度后,发现渐变色不仅可以根据xml来实现,也可以用java代码来实现,由于目前没有那么多时间,只记录xml实现的方法;以后在记录Java实现的代码。
通过Shape gradient标签来实现
首先来看gradient标签所有的渐变属性:
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
attributes:
- android:angle Integer,代表渐变颜色的角度, 0 is left to right, 90 is bottom to top. 必须是45的整数倍. 默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。
- android:startColor Color. 颜色渐变的开始颜色
- android:endColor Color. 颜色渐变的结束颜色
- android:centerColor Color. 颜色渐变的中间颜色,主要用于多彩。
- android:centerX Float.(0 - 1.0) 相对X的渐变位置。
- android:centerY Float.(0 - 1.0) 相对Y的渐变位置。
centerX和centerY,这两个属性只有在type不为linear情况下起作用。 - android:gradientRadius Float. 渐变颜色的半径,单位应该是像素点. 需要 android:type="radial". 如果android:type="radial",没有设置android:gradientRadius,将会报错,error inflating class.
- android:type |Value|Description| |:-|:-| |"linear"|线性渐变。| |"radial"|A radial gradient.圆形渐变,起始颜色从cenralX,centralY点开始。| |"sweep"|A sweeping line gradient.扇形渐变。|