android customview覆盖onDraw():canvas将所有矩形绘制成黑色

时间:2017-06-21 17:27:59

标签: android canvas paint ondraw

我使用的自定义视图出现问题。它绘制了一个网格,用于表示平面图,其上有一个开始和当前位置(彩色矩形)。 (代码在这里:https://pastebin.com/8SExmtAp)。

简而言之,我会初始化不同的颜料:

 private void initPaints()
{
    waypointPaint = new Paint(Color.parseColor("#800080"));
    currentCoordinatePaint = new Paint(Color.RED);
    linePaint = new Paint(Color.BLACK);
    startCoordinatePaint = new Paint(Color.BLUE);
}

并在onDraw()中使用它们:

    // color the current coordinates
    Coordinates currentCoords = Model.getCurrentCoordinates();
    if (currentCoords != null)
    {
                canvas.drawRect((float) currentCoords.getX() * cellWidth, (float) currentCoords.getY() * cellHeight,
                        (float) (currentCoords.getX() + 1) * cellWidth, (float) (currentCoords.getY() + 1) * cellHeight,
                        currentCoordinatePaint);

    }

    Coordinates startCoordinate = Model.startCoordinate;
    if (startCoordinate != null && startCoordinate != currentCoords)
    {
        canvas.drawRect((float) startCoordinate.getX() * cellWidth, (float) startCoordinate.getY() * cellHeight,
                (float) (startCoordinate.getX() + 1) * cellWidth, (float) (startCoordinate.getY() + 1) * cellHeight,
                startCoordinatePaint);
    }

然而,它们不是为初始位置获得蓝色而对于当前位置获得红色,而是两个都是黑色,请参阅: Screenshot of app

关于drawRect(...)方法的文档我使用的只是声明如下:

  

使用指定的绘制绘制指定的Rect。矩形将根据绘画中的样式填充或加框。

所以..我真的没有看到代码错在哪里以及为什么我得到了结果。也许有人知道为什么?

2 个答案:

答案 0 :(得分:1)

Paint constructor you are using期望将int标志作为参数,而不是填充颜色。

尝试:

currentCoordinatePaint = new Paint();
currentCoordinatePaint.setStyle(Paint.Style.FILL);
currentCoordinatePaint.setColor(Color.RED);

答案 1 :(得分:0)

就像josef.adamcik statet一样,我错误地认为我用于绘制对象的构造函数。将代码更改为

upload_max_filesize = 15M
post_max_size = 15M
max_input_time = 0
LimitRequestBody 0

做了这个伎俩。