Android - Draw Bitmap within Canvas

别来无恙 提交于 2020-01-09 10:13:43

问题


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.

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

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

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


回答1:


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.

EDIT:

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



回答2:


Try the following code :

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);

canvas.drawBitmap(bitmap, x, y, paint);

==================

You could also just reference this answer.



来源:https://stackoverflow.com/questions/13361231/android-draw-bitmap-within-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!