Android缩小图像总是以拉伸结束

Android缩小图像总是以拉伸结束,android,imageview,image-resizing,Android,Imageview,Image Resizing,我正在尝试缩小高分辨率图像的大小,如1980x1080左右,我正在Nexus5API21+中进行模拟,我需要将此高分辨率图像加载到我的图像视图支架中 我正在使用下面的位图代码 float imageRatio = (float) newWidth / newHeight; int imageWidth = mybitmapimage.getWidth(); int imageHeight = mybitmapimage.getHeight(); float aspectRatio = (fl

我正在尝试缩小高分辨率图像的大小,如1980x1080左右,我正在Nexus5API21+中进行模拟,我需要将此高分辨率图像加载到我的图像视图支架中

我正在使用下面的位图代码

float imageRatio = (float) newWidth / newHeight;

int imageWidth = mybitmapimage.getWidth();
int imageHeight = mybitmapimage.getHeight();

float aspectRatio = (float) imageWidth / imageHeight; // original iamge

// Initializing to find which needs scale down
float scaleWidth = newWidth;
float scaleHeight = newHeight;

if (imageRatio < aspectRatio) {
    scaleHeight = ((float) newWidth) / aspectRatio;
} else {
    scaleWidth = ((float) newHeight) * aspectRatio;
}

Matrix matrix = new Matrix();
// RESIZE THE BIT MAP

matrix.postScale(scaleWidth, scaleHeight);

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, imageWidth, imageHeight, matrix, false);
return resizedBitmap;
float-imageRatio=(float)newWidth/newHeight;
int imageWidth=mybitmapimage.getWidth();
int-imageHeight=mybitmapimage.getHeight();
float aspectRatio=(float)图像宽度/图像高度;//原音
//初始化以查找哪些需要缩小
浮动刻度宽度=新宽度;
浮动刻度高度=新高度;
if(图像比率
就我的一生而言,这段代码最终要么被拉伸,要么被裁剪。源图像文件的大小约为1920*1080,因此需要缩小比例

如果有人能帮我解决这个棘手的问题,我将不胜感激

ratio = Math.min(newWidth/oldWidth, newHeight/oldHeight);
这就是在保持纵横比的同时获得调整位图大小的缩放比的方法

最终宽度和高度为:

finalWidth = oldWidth * ratio;
finalHeight = oldHeight * ratio;
只需在比例矩阵中使用该比率:

matrix.postScale(ratio, ratio);

显然,scaleWidth和scaleHeight应该是相同的。如果您传递不同的D值,您的图像将沿x轴和y轴进行不同的缩放。是的,我同意这一点,但如何防止拉伸?像它的宽度在尝试适应高度时会被拉伸,如果高度和宽度看起来合适,那么像的图像会定位在中间,周围有黑色背景。。我尝试了fitXY和scaletype中所有其他可用的缩放选项,但似乎没有任何效果。如果您希望图像只适合整个区域(而不保持比例),您可以只计算scaleWidth=newWidth/oldWidth;比例高度=新高度/旧高度;我搞不懂你的计算结果是什么。实际上我做的是scaleHeight=((float)newWidth)/oldwidth;比例宽度=((浮动)新高度)/旧高度;但即使是这样,最终还是被屏幕的宽度拉长了。检查“宽度”和“高度”可能是印刷错误,就像你们的评论一样。