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.
RectF defaultRect = new RectF(0, 0, 480, 800);
RectF screenRect = new RectF(0, 0, 320, 480);
Then create and initialize the matrix Matrix defToScreenMatrix = new Matrix();
defToScreenMatrix.setRectToRect(defaultRect, screenRect, Matrix.ScaleToFit.CENTER);
When the prepared bitmap is drawn on the screen canvas, we can apply the matrix and the scale result will keep the aspect ratio.canvas.drawBitmap(preparedBitmap, defToScreenMatrix, null);
Let us look the example. PictureA indeed look better than PictureB.PictureA (Matrix.ScaleToFit.CENTER) |
PictureB (Matrix.postScale) |
No comments:
Post a Comment