(转载)
在android的画图机制中,关于Paint的填充效果有三个:Paint.Style. STROKE、 Paint.Style.FILL和Paint.Style.FILL_AND_STROKE。
关于Paint.Style.FILL,
Geometryand text drawn with this style will be filled, ignoring all stroke-relatedsettings in the paint.
用这种风格进行绘制的几何图形和文本将会被填充,忽略了在paint里设置的与stroke相关的属性。
关于Paint.Style. STROKE
Geometryand text drawn with this style will be stroked, respecting the stroke-relatedfields on the paint.
用这种风格进行绘制的几何图形和文本将会被用笔进行绘制,遵从在paint里设置的与stroke相关的属性(例如 setStrokeWidth()、setStrokeJoin()中所设置的属性)。
关于Paint.Style.FILL_AND_STROKE
Geometryand text drawn with this style will be both filled and stroked at the sametime, respecting the stroke-related fields on the paint. This mode can giveunexpected results if the geometry is oriented counter-clockwise. Thisrestriction does not apply to either FILL or STROKE.
这种风格是同时应用前面两种风格进行绘制。
所以Paint.Style.FILL和Paint.Style. STROKE最大的区别应该是,前者指的是将几何体或文本填充满,而后者着重的是用笔一样的画,强调的是一种线条,当然这种线条可以设置粗细,所以后者常常跟paint.setStrokeWidth()一起使用。
测试代码:
测试效果:
//--------------------------------------------------------------------------------------------------------------------------------------------------
从这里可以看出,很多时候,设置某些属性之后,会屏蔽掉其他属性的作用。
这个要注意。比如说这里,设置了Style.FILL这个属性是大类别的属性,所以设置了之后,setStrokeWidth(xxx)属性自然不能生效了。
FILL和FILL_AND_STROKE这两个属性是有区别的。
public class MyView extends View {
Context mContext;
public MyView(Context context) {
super(context);
mContext =context;
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
}
//重写OnDraw()函数,在每次重绘时自主实现绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画笔基本属性
Paint paint=new Paint();
paint.setAntiAlias(true);//抗锯齿功能
paint.setColor(Color.RED); //设置画笔颜色
paint.setStyle(Paint.Style.FILL);//设置填充样式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
paint.setStrokeWidth(5);//设置画笔宽度
// paint.setShadowLayer(10, 15, 15, Color.GREEN);//设置阴影
//画圆
canvas.drawCircle(360, 540, 150, paint);
Paint paint2=new Paint();
paint2.setAntiAlias(true);//抗锯齿功能
paint2.setColor(Color.parseColor("#80808080")); //设置画笔颜色
paint2.setStyle(Paint.Style.FILL_AND_STROKE);//设置填充样式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
paint2.setStrokeWidth(150);//设置画笔宽度
// paint.setShadowLayer(10, 15, 15, Color.GREEN);//设置阴影
//画圆
canvas.drawCircle(360, 540, 150, paint2);
}
}
可以看到这里会对整个圆的大小有一定的影响的。
FILL与FILL_AND_STROKE 经过测试是存在区别的。FILL模式下,图像的大小由canvas指定。FILL_AND_STROKE模式下,图像的真实大小是canvas指定+画笔宽度/2。
http://stackoverflow.com/questions/3723545/whats-the-point-of-fill-and-stroke
afaik: FILL fills your circle, while FILL_AND_STROKE also draws the border. If you increase the size of the stroke, it should result in different circles sizes (only visual!)
Think about this: you draw a circle by hand with a small sized pencil. The radius is what you wanted. If you now take a big brush and draw the circle again, your radius is much bigger... (i hope its understandable O.o )