gpt4 book ai didi

java - 调整位图大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:01 25 4
gpt4 key购买 nike

下面的代码调整位图的大小并保持宽高比。我想知道是否有更有效的调整大小的方法,因为我知道我正在编写 android API 中已经可用的代码。

private Bitmap resizeImage(Bitmap bitmap, int newSize){
int width = bitmap.getWidth();
int height = bitmap.getHeight();

int newWidth = 0;
int newHeight = 0;

if(width > height){
newWidth = newSize;
newHeight = (newSize * height)/width;
} else if(width < height){
newHeight = newSize;
newWidth = (newSize * width)/height;
} else if (width == height){
newHeight = newSize;
newWidth = newSize;
}

float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);

return resizedBitmap;
}

最佳答案

关于java - 调整位图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9583757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com