在 Canvas 上绘制时未显示可绘制对象

Drawable not showing up when drawn on Canvas

我正在构建一个需要绘制网格并向各个单元格添加图像的应用程序。大约有 10 张不同的图像,均为 .png 格式。有一个数组允许应用程序遍历每个网格正方形并检查该正方形是否应该有图像,如果有,是哪一个。如果正方形中应该有一个图像,则绘制如下:

    for (int rr = 0; rr < numRows; rr++) {

        for (int cc = 0; cc < numCols; cc++) {

            // Find out what should be in this grid square
            CellImage thisCellImage = mChart.returnCellImage(rr,cc);

            if(thisCellImage == null) continue;

            // Find the drawable from the baseName
            String drawableName = thisCellImage.getName();
            Resources resources = mContext.getResources();
            int resourceId = resources.getIdentifier(drawableName,
                            "drawable",
                            mContext.getPackageName());
            Drawable d;

            if(resourceId != 0) {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                 d = resources.getDrawable(resourceId, mContext.getTheme());
               } else {
                 d = resources.getDrawable(resourceId);
               }

               // Calculate the position of the top left grid square
               float posX = minX + ((numCols - cc - 1) * cell_width);
               float posY = minY + ((numRows - rr - 1) * cell_height);

               // Now draw the image into the square
               if (d != null) {
                  d.setBounds(Math.round(posX), Math.round(posY),
                               Math.round(posX + cell_width),
                               Math.round(posY + cell_height));
                  d.draw(canvas);
               }
            }
        }
     }

问题是网格上什么也没有出现。我可以从调试器中看到 drawable 已经找到了;为 posX 和 posY 计算的值看起来合理(它们肯定在网格内,所以即使它们不完全准确,drawable 也应该在某处可见)。

网格已经在前面添加了背景颜色,使用:

mPaint.setColor(bgColour.returnHexCode());
canvas.drawRect(minX, minY, maxX, maxY, mPaint);

我已经尝试采取这一步骤(以防背景隐藏了可绘制对象或其他东西,但这没有任何区别;我只是将它包括在这里以防它可能相关。

有什么地方出错了吗?我真的不知道从哪里开始绘制。

编辑

CellImage定义如下:

public class CellImage {

    private String name, description;

    // CONSTRUCTOR
    public CellImage() {}

    // GETTERS
    public String getName()         { return name; }
    public String getDescription()  { return description; }

    // SETTERS
    public void setName(String thisName)            { this.name = thisName; }
    public void setDescription(String thisDesc)     { this.description = thisDesc; }

}

CellImage 的名称与可绘制文件的名称相同。例如,如果 CellImage 名称是 "example",那么可绘制对象将是 "example.png".

可绘制对象的一个​​示例是:

(大部分图片都比这个复杂一点,所以我不能直接画圆圈之类的,都保存为64px x 64px pngs)

原来图像是在白色背景上以白色绘制的...我会把问题留在这里以防其他人浪费几个小时。