Android - 在 Canvas 中绘制位图 [英] Android - Draw Bitmap within Canvas

查看:26
本文介绍了Android - 在 Canvas 中绘制位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个迷宫游戏,它绘制一个 5 x 5 的正方形(占用屏幕的宽度并将其均匀分割).然后对于每个使用 x 和 y 坐标的框,我使用 drawRect 来绘制彩色背景.

I currently have a maze game which draws a 5 x 5 square (takes the width of screen and splits it evenly). Then for each of these boxes using x and y cordinates I user drawRect, to draw a colored background.

我遇到的问题是我现在需要在同一位置绘制图像,因此替换当前的纯背景颜色填充.

The issue I am having is I now need to draw an image within this same location, therefore replacing the current plain background colour fill.

这是我目前用于 drawRect 的代码(一些示例):

Here is the code I am currently using to drawRect (a few example):

// these are all the variation of drawRect that I use
canvas.drawRect(x, y, (x + totalCellWidth), (y + totalCellHeight), green);
canvas.drawRect(x + 1, y, (x + totalCellWidth), (y + totalCellHeight), green);
canvas.drawRect(x, y + 1, (x + totalCellWidth), (y + totalCellHeight), green);

然后我还需要为画布中的所有其他方块实现背景图像.此背景将在其顶部绘制简单的 1px 黑线,当前代码在灰色背景中绘制.

I would then also need to implement a background image for all the other squares within my canvas. This background will have simple 1px black lines drawn over the top of it, current code to draw in a grey background.

background = new Paint();
background.setColor(bgColor);
canvas.drawRect(0, 0, width, height, background);

如果可能的话,请您给点建议.如果是这样,我可以这样做的最佳方法是什么,同时尝试最小化内存使用量并拥有 1 个图像,该图像将扩展和收缩以填充相关的方形空间(这在所有不同的屏幕尺寸上都会有所不同,因为它分割了整体屏幕宽度均匀).

Could you please advice if this is at all possible. If so, what is the best way I can go about doing this, whilst trying to minimise memory usage and having 1 image which will expand and shrink to fill the relvent square space(this varies on all the different screen sizes as it splits the overall screen width evenly).

推荐答案

使用Canvas方法public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint).将 dst 设置为您希望将整个图像缩放到的矩形的大小.

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint). Set dst to the size of the rectangle you want the entire image to be scaled into.

这是在画布上以正方形绘制位图的可能实现.假设位图位于二维数组中(例如,Bitmap bitmapArray[][];)并且画布是方形的,因此方形位图纵横比不会失真.

Here's a possible implementation for drawing the bitmaps in squares across on the canvas. Assumes the bitmaps are in a 2-dimensional array (e.g., Bitmap bitmapArray[][];) and that the canvas is square so the square bitmap aspect ratio is not distorted.

private static final int NUMBER_OF_VERTICAL_SQUARES = 5;
private static final int NUMBER_OF_HORIZONTAL_SQUARES = 5;

...

    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;

    // 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);

            // Draw the bitmap into the destination rectangle on the canvas
            canvas.drawBitmap(bitmapArray[horizontalPosition][verticalPosition], null, destinationRect, null);
        }
    }

这篇关于Android - 在 Canvas 中绘制位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆