我正在尝试使用Android的画布生成正方形网格,这里是代码:
public static void drawSquares(Canvas canvas) {
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
int squareWidth = canvasWidth / NUMBER_OF_HORIZONTAL_SQUARES;
int squareHeight = canvasHeight / NUMBER_OF_VERTICAL_SQUARES;
Rect destinationRect = new Rect();
int xOffset;
int yOffset;
// Init paint
Paint background = new Paint();
// Set the destination rectangle size
destinationRect.set(0, 0, squareWidth, squareHeight);
for (int horizontalPosition = 0; horizontalPosition < NUMBER_OF_HORIZONTAL_SQUARES; horizontalPosition++){
xOffset = horizontalPosition * squareWidth;
for (int verticalPosition = 0; verticalPosition < NUMBER_OF_VERTICAL_SQUARES; verticalPosition++){
yOffset = verticalPosition * squareHeight;
// Set the destination rectangle offset for the canvas origin
destinationRect.offsetTo(xOffset, yOffset);
// color
Random r = new Random();
background.setColor(COLORS[r.nextInt(3)]);
// draw
canvas.drawRect(destinationRect, background );
}
}
}
以下代码需要200ms才能生成网格,有没有办法获得66ms以下的意思?在这种情况下,颜色是随机提供的,但通常我会通过int数组从外部传递值。
谢谢,任何帮助!