这篇文章不难,也没什么技术讲解,小弟不才,就简介的介绍一下LinearGradient、SweepGradient、RadialGradient这三种渲染的使用例子。具体代码分析此处就不写了,也是为自己做个记录,这三种的使用。
首先,为了方便。先自定一了个MyView 继承View ,然后重写onMeasure 和 onDraw 方法。为了更简便,我直接在onMeasure方法中直接定义了宽高为500的正方形。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(500,500);
}
就是如此简单的一个View, 好了。下面开始使用三种渲染吧...
LinearGradient
线性渲染,顾名思义,就是和LiearLayout相似的渲染方式,比较整齐的样子吧,一条一条的。
@Override
protected void onDraw(Canvas canvas) {
Paint paint1 = new Paint();
paint1.setShader(new LinearGradient(0,0,getWidth()/2,getHeight()/2,
new int[]{Color.RED, Color.WHITE, Color.BLUE},null, Shader.TileMode.MIRROR));
canvas.drawRect(0,0,getWidth(),getHeight(),paint1);
super.onDraw(canvas);
}
首先弄个画笔,然后设置他的setShader属性。我这里直接就new LiearGradient() 了。然后里面会需要传入几个参数:
/** Create a shader that draws a linear gradient along a line.
@param x0 The x-coordinate for the start of the gradient line
@param y0 The y-coordinate for the start of the gradient line
@param x1 The x-coordinate for the end of the gradient line
@param y1 The y-coordinate for the end of the gradient line
@param colors The colors to be distributed along the gradient line
@param positions May be null. The relative positions [0..1] of
each corresponding color in the colors array. If this is null,
the the colors are distributed evenly along the gradient line.
@param tile The Shader tiling mode
*/
public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
TileMode tile) {
if (colors.length < 2) {
throw new IllegalArgum