Android图形处理-百变Paint
Paint的基本属性
在Android图形处理-Canvas已经有了基本的使用,但是这节介绍几个好玩的属性
设置阴影和渐变
设置渐变
主要是给画笔(Paint)设置一个Shader
Paint paint = new Paint();
LinearGradient linearShader = new LinearGradient(0, 0, 100, 100,
new int[]{Color.RED, Color.BLACK, Color.BLUE, Color.DKGRAY},
null, Shader.TileMode.CLAMP);
paint.setShader(linearShader);
canvas.drawCircle(100,100,100,paint);
LinearGradient是Shader的子类。其他子类:
LinearGradient构造方法的含义:
/** 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
*/
前两个参数是开始点,接下来是结束点,colors是颜色的数组,positions是分布的模式,tile是渐变的模式比如可重复等
设置阴影
paint.setShadowLayer(100,20,20,Color.RED);
paint.setColor(getResources().getColor(R.color.colorAccent));
canvas.drawCircle(100,100,50,paint);
核心是setShadowLayer