Android的 - 内画帆布位图 [英] Android - Draw Bitmap within Canvas

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

问题描述

我现在有一个迷宫游戏绘制一个5×5平方(采用屏幕的宽度并分割其均匀)。然后为每个使用x和y cordinates我用户的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(几个例子)在code:

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的黑线,目前的code在一个灰色的背景画。

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的图像,将扩大和缩小,以填补relvent广场空间的最佳途径,因为它分裂的整体(这个数字的变动对所有不同的屏幕尺寸屏幕宽度均匀)。

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).

推荐答案

使用画布方法公共无效drawBitmap(位图位图,矩形SRC, RectF DST,涂料粉刷)。设置 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.

编辑:

下面是在画布上绘制在正方形的点阵图可能实现。假设位图是一个二维阵列(例如,位图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的 - 内画帆布位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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