android 颜色渐变是指通知xml或者java代码,设置相关参数,是界面的某个指定的视图显示成从开始位置的颜色,逐渐过度到结尾位置的颜色的技术。
android颜色渐变的分类有:
LinearGradient线性渐变
RadialGradient镜像渐变
SweepGradient角度渐变
一、LinearGradient线性渐变
顾名思义,是只颜色在一个直线方向上逐渐改变。
文件代码:
android:shape="oval" >
android:endColor="#0000FF"
android:startColor="#FF0000"
android:type="linear" />
效果:
二、RadialGradient镜像渐变
镜像渐变就是楼主问的问题了:只要将type设置为oval,然后增加
android:gradientRadius
属性。
楼主特殊要求是圆形的话,需要在shape里面添加
android:shape="oval"
文件代码:
android:shape="oval" >
android:endColor="#0000FF"
android:gradientRadius="100%p"
android:startColor="#FF0000"
android:type="linear" />
效果:
三、 SweepGradient角度渐变
是指以中心点为射线的一个断点,顺时针旋转所扫过的区域,颜色逐渐改变的一种渐变方式
android:endColor="#0000FF"
android:startColor="#FF0000"
android:type="sweep" />
效果图:
Specifies a gradient color for the shape.
attributes:
android:angle
Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.
android:centerX
Float. The relative X-position for the center of the gradient (0 - 1.0). Does not apply when android:type="linear".
android:centerY
Float. The relative Y-position for the center of the gradient (0 - 1.0). Does not apply when android:type="linear".
android:centerColor
Color. Optional color that comes between the start and end colors, as a hexadecimal value or color resource.
android:endColor
Color. The ending color, as a hexadecimal value or color resource.
android:gradientRadius
Float. The radius for the gradient. Only applied when android:type="radial".
android:startColor
Color. The starting color, as a hexadecimal value or color resource.
android:type
Keyword. The type of gradient pattern to apply. Valid values are:
Value Description
"linear" A linear gradient. This is the default.
"radial" A radial gradient. The start color is the center color.
"sweep" A sweeping line gradient.
用 radial type 再设一下 android:centerY android:centerX
使用RadialGradient即可。
定义一个Paint,然后调用setShader方法将RadialGradient传进去。
然后调用canvas的draw方法将Paint对象传进去即可。