I am looking at the Snake example that comes with the SDK, specifically the onDraw method inside the TileView class:
This works great for the snake application but I am thinking of doing something a bit more intensive so I want my draw method to be as quick as possible. I was thinking of something a bit more like this:
The idea being that I store a separate image (canvasBuffer) that I only draw the tiles that need to be changed, then I draw the buffer to the canvas. Also this way I do not have to calculate the offset for every tile, I can set the offset at anytime by setting the bounds of the canvasBuffer.
My question here is how to draw one drawable onto another, I can only find methods that allow you to draw to a canvas object. Any suggestions?
Code:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int x = 0; x < mXTileCount; x += 1) {
for (int y = 0; y < mYTileCount; y += 1) {
if (mTileGrid[x][y] > 0) {
canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
mXOffset + x * mTileSize,
mYOffset + y * mTileSize,
mPaint);
}
}
}
}
This works great for the snake application but I am thinking of doing something a bit more intensive so I want my draw method to be as quick as possible. I was thinking of something a bit more like this:
Code:
public Drawable canvasBuffer;
public Coordinate[] dirtyTiles; //A list of tiles that need to be re-drawn
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < dirtyTiles.length; i += 1) {
//I need a way to do this - drawOtherImage is not a real function
canvasBuffer.drawOtherImage(mTileGrid[dirtyTiles.x][dirtyTiles.y],
dirtyTiles.x*mTileSize, dirtyTiles.y*mTileSize)
}
canvasBuffer.draw(canvas);
}
The idea being that I store a separate image (canvasBuffer) that I only draw the tiles that need to be changed, then I draw the buffer to the canvas. Also this way I do not have to calculate the offset for every tile, I can set the offset at anytime by setting the bounds of the canvasBuffer.
My question here is how to draw one drawable onto another, I can only find methods that allow you to draw to a canvas object. Any suggestions?