画布填充两行之间的颜色

时间:2015-10-13 22:27:59

标签: android android-layout android-canvas android-view

我有两条线,每次在同一点绘制一条线,每次视图无效时另一条线旋转一定程度。

代码如下所示:

 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);
        }

一切正常,但我想用不同的颜色填充两条线之间的空间。我怎么做?感谢。

1 个答案:

答案 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);