I've divide my screen up into rects, but I'm using a a for loop so I'm not storing the rects there getting remade each time. How would I store them in like an array?
public void drawGrid() {
//android.os.Debug.waitForDebugger();
int height,width;
int column,row;
int maxColumn,maxRow;
maxColumn = 4;
maxRow = 4;
column = 0;
row = 0;
height = c.getHeight()/maxRow;
width = c.getWidth()/maxColumn;
Paint pg = new Paint();
Rect[] test[];
for(int i = 0;i < 5; i++) {
int srcX = column * width;
int srcY = row * height;
Rect src =new Rect(srcX,srcY,srcX + width, srcY +height);
pg.setColor(Color.WHITE);
pg.setStyle(Paint.Style.STROKE);
pg.setStrokeWidth(5);
c.drawRect(src, pg);
if (column == maxColumn && row == maxRow){
i = 5;
} else {i=0;}
if (column == maxColumn){
row = row + 1;
column = 0;
} else {column = column + 1;}
}
}
Allocate them in advance, so that you're not instantiating objects during a drawing operation. So whenever you determine the number of rects that you'll need (i.e. if it's always the same, initialize them in the constructor). Something like this:
Rect[] rects = new Rect[rectCount];
for(int i = 0; i < rectCount; i++) rects[i] = new Rect();
Then, in your drawing loop, use:
rects[i].set(srcX, srcY, srcX + width, srcY + height);
You should avoid allocating objects during a draw operation whenever possible.
EDIT: For a 2D array:
Rect[][] rects = new Rect[rowCount][colCount];
for(int i = 0; i < rowCount; i++) {
for(int j = 0; j < colCount; j++) {
rects[i][j] = new Rect();
}
}
Then in the loop, it's the same thing:
rects[row][col].set(srcX, srcY, srcX + width, srcY + height);