快速渲染的帆布由方形网格制成

时间:2018-03-04 18:59:40

标签: android canvas

我正在尝试使用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数组从外部传递值。

谢谢,任何帮助!

0 个答案:

没有答案