使用
Canvas方法
public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paintpaint)。设置
dst为要缩放整个图像的矩形的大小。
编辑:
这是在画布上以正方形绘制位图的可能实现。假设位图是二维数组(例如
BitmapbitmapArray[][];),并且画布是正方形的,因此正方形位图的宽高比不会失真。
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); } }


