To keep the aspect ratio, we can use Matrix.ScaleToFit.CENTER .
First, create two RectF obejct, one is for our prepared bitmap, another is for the target screen size.
Then create and initialize the matrix
RectF defaultRect = new RectF(0, 0, 480, 800); RectF screenRect = new RectF(0, 0, 320, 480);
When the prepared bitmap is drawn on the screen canvas, we can apply the matrix and the scale result will keep the aspect ratio.
Matrix defToScreenMatrix = new Matrix(); defToScreenMatrix.setRectToRect(defaultRect, screenRect, Matrix.ScaleToFit.CENTER);
Let us look the example. PictureA indeed look better than PictureB.
canvas.drawBitmap(preparedBitmap, defToScreenMatrix, null);