Android 调整位图大小,保持纵横比,无失真和裁剪
有没有办法在不失真的情况下调整位图的大小,使其不超过720*1280?高度和宽度越小越好(宽度或高度越小,空白画布就越好),我试过了,但它会使图像失真。有人能提出更好的解决方案吗 以下是将位图缩小到不超过最大允许分辨率的方法。在您的情况下,Android 调整位图大小,保持纵横比,无失真和裁剪,android,bitmap,android-imageview,android-bitmap,Android,Bitmap,Android Imageview,Android Bitmap,有没有办法在不失真的情况下调整位图的大小,使其不超过720*1280?高度和宽度越小越好(宽度或高度越小,空白画布就越好),我试过了,但它会使图像失真。有人能提出更好的解决方案吗 以下是将位图缩小到不超过最大允许分辨率的方法。在您的情况下,允许的最大分辨率=1280。它将在不失真和质量损失的情况下缩小尺寸: private static Bitmap downscaleToMaxAllowedDimension(String photoPath) { BitmapFactory.Opti
允许的最大分辨率=1280
。它将在不失真和质量损失的情况下缩小尺寸:
private static Bitmap downscaleToMaxAllowedDimension(String photoPath) {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, bitmapOptions);
int srcWidth = bitmapOptions.outWidth;
int srcHeight = bitmapOptions.outHeight;
int dstWidth = srcWidth;
float scale = (float) srcWidth / srcHeight;
if (srcWidth > srcHeight && srcWidth > MAX_ALLOWED_RESOLUTION) {
dstWidth = MAX_ALLOWED_RESOLUTION;
} else if (srcHeight > srcWidth && srcHeight > MAX_ALLOWED_RESOLUTION) {
dstWidth = (int) (MAX_ALLOWED_RESOLUTION * scale);
}
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inDensity = bitmapOptions.outWidth;
bitmapOptions.inTargetDensity = dstWidth;
return BitmapFactory.decodeFile(photoPath, bitmapOptions);
}
如果您已经有位图对象而不是路径,请使用以下选项:
private static Bitmap downscaleToMaxAllowedDimension(Bitmap bitmap) {
int MAX_ALLOWED_RESOLUTION = 1024;
int outWidth;
int outHeight;
int inWidth = bitmap.getWidth();
int inHeight = bitmap.getHeight();
if(inWidth > inHeight){
outWidth = MAX_ALLOWED_RESOLUTION;
outHeight = (inHeight * MAX_ALLOWED_RESOLUTION) / inWidth;
} else {
outHeight = MAX_ALLOWED_RESOLUTION;
outWidth = (inWidth * MAX_ALLOWED_RESOLUTION) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
return resizedBitmap;
}
@pskink大小调整Bitmap@pskink给扭曲的图像。。。我想在不失真的情况下缩放位图(保持纵横比或均匀缩放),并将其设置为720大小的画布*1280@pskink您能否回答如何统一缩放任意大小的位图,以便id不超过指定的高度和宽度?