代码之家  ›  专栏  ›  技术社区  ›  Sharon

在画布上绘制时无法显示

  •  0
  • Sharon  · 技术社区  · 6 年前

    我正在构建一个应用程序,它需要绘制一个网格,并将图像添加到不同的单元格中。大约有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; }
    
    }
    

    的名称 与可绘制文件名相同。例如如果 细胞图像

    可拉伸的一个例子是: circle.png drawable

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sharon    6 年前

    原来这张照片是在白色背景上画的。。。我把问题留在这里,以防别人浪费时间。