Issue
I'm building an app which needs to draw a grid, and add images to various cells. There are about 10 different images, all in .png format. There's an array which allows the app to loop over each grid square and check whether that square should have an image, and if so, which one. If there's supposed to be an image in the square, it's drawn in as follows:
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);
}
}
}
}
The problem is that nothing is appearing on the grid. I can see from the debugger that the drawable has been found ok; the values calculated for posX and posY look reasonable (they are definitely within the grid, so even if they're not completely accurate, the drawable should be visible somewhere).
The grid has had a background colour added earlier, using:
mPaint.setColor(bgColour.returnHexCode());
canvas.drawRect(minX, minY, maxX, maxY, mPaint);
I've tried taking that step out (in case the background was hiding the drawable or something, but it didn't make any difference; I'm just including it here in case it might be relevant.
Any ideas where I'm going wrong? I don't really know where to start with drawables.
Edit
CellImage
is defined as follows:
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; }
}
The name of the CellImage
is the same as the drawable file name. Eg if the CellImage
name is "example", then the drawable would be "example.png".
(most of the images are a bit more complicated than this one, so I can't just, eg, draw circles directly. They are all saved as 64px x 64px pngs)
Solution
Turned out that the image was drawing in white on a white background... I'll leave the question here in case it saves anyone else a few wasted hours.
Answered By - Sharon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.