我有一个已经解码的位图,我想在将它绘制到 Canvas 上之前临时缩放它。因此,解码文件并设置大小之前是不可能的。我想保持现有位图的大小,并在将其绘制到 Canvas 上之前将其缩放为更小。这可能吗?
使用 Matrix postScale(sx, sy, px, py) 可以正确缩放它但不能正确定位。从我所见,canvas.drawBitmap 没有矩阵和 x 和 y 位置的选项。
有什么建议吗?
最佳答案
代码如下:
public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
final int bitmapWidth = bitmap.getWidth();
final int bitmapHeight = bitmap.getHeight();
final float scale = Math.min((float) width / (float) bitmapWidth,
(float) height / (float) bitmapHeight);
final int scaledWidth = (int) (bitmapWidth * scale);
final int scaledHeight = (int) (bitmapHeight * scale);
final Bitmap decoded = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
final Canvas canvas = new Canvas(decoded);
return decoded;
}
请注意:传递位图进行缩放,它是新的高度和宽度。
关于android - 如何缩放android中已经存在的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11011180/