android customview覆盖onDraw():画布绘制所有矩形黑色

问题描述:

我有一个自定义视图我使用的问题。它绘制了一个网格,我用它来表示一个平面布局图,其上有一个开始和当前位置(彩色矩形)。 (代码在这里:https://pastebin.com/8SExmtAp)。android customview覆盖onDraw():画布绘制所有矩形黑色

总之,我初始化不同的涂料是这样的:

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

然而,除了得到一个蓝色一个用于指定startPosition和红色的当前位置,它们都是黑色的,请参阅: Screenshot of app

关于drawRect(...)方法的文档我只使用s给出以下内容:

使用指定的绘图绘制指定的Rect。该矩形将根据油漆中的样式进行填充或框定。

因此,我真的不知道代码错在哪里,为什么我得到我得到的结果。也许你们中有人知道为什么?

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

尝试:

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

哦......我明白了。我一回到家就会尝试。谢谢 – DerDingens

+0

差点忘了回报。你是完全正确的,改变油漆初始化做到了。 – DerDingens

像josef.adamcik statet,我错我用油漆对象的构造函数。将代码更改为

private void initPaints() 
    { 
     waypointPaint = new Paint(); 
     waypointPaint.setColor(Color.GREEN); 
     waypointPaint.setStyle(Paint.Style.FILL); 
     currentCoordinatePaint = new Paint(); 
     currentCoordinatePaint.setColor(Color.RED); 
     currentCoordinatePaint.setStyle(Paint.Style.FILL); 
     linePaint = new Paint(); 
     linePaint.setColor(Color.BLACK); 
     linePaint.setStyle(Paint.Style.STROKE); 
     startCoordinatePaint = new Paint(); 
     startCoordinatePaint.setColor(Color.BLUE); 
     startCoordinatePaint.setStyle(Paint.Style.FILL); 
    } 

诀窍。