我有两条线,每次在同一点绘制一条线,每次视图无效时另一条线旋转一定程度。
代码如下所示:
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
int centerx = width/2;
int centery = height/2;
canvas.drawLine(centerx, 0, centerx, height, paint);
canvas.drawLine(0, centery, width, centery, paint);
if (angle_in_degrees!= null)
canvas.rotate(angle_in_degrees, centerx, centery);
paint.setColor(0xff0000ff);
canvas.drawLine(centerx, -1000, centerx, +1000, paint);
canvas.drawLine(-1000, centery, 1000, centery, paint);
paint.setColor(0xff00ff00);
}
一切正常,但我想用不同的颜色填充两条线之间的空间。我怎么做?感谢。
答案 0 :(得分:0)
此question的接受答案听起来好像适用于您的情况。基本上,作者使用Path
对象连接所有需要填充的点之间的点,最后调用canvas.drawPath()
以填充内部区域。
在您的情况下,这可能看起来像:
Path path = new Path();
path.lineTo(centerX, 0);
path.lineTo(centerX, height);
path.lineTo(0, centerY);
//etc. Essentially mirroring what your line does, but in one dimension (x,y) points.
canvas.drawPath(path, paint);
修改:您还需要确保您使用的Paint
对象具有FILL
样式:
paint.setStyle(Paint.Style.FILL);