gpt4 book ai didi

Android调整位图大小保持纵横比

转载 作者:IT老高 更新时间:2023-10-28 23:37:19 24 4
gpt4 key购买 nike

我有一个自定义 View (1066 x 738),我正在传递一个位图图像 (720x343)。我想缩放位图以适应自定义 View 而不超出父级的范围。

enter image description here

我想实现这样的目标:

enter image description here

我应该如何计算位图大小?

我如何计算新的宽度/高度:

    public static Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight)
{
int bWidth = b.getWidth();
int bHeight = b.getHeight();

int nWidth = reqWidth;
int nHeight = reqHeight;

float parentRatio = (float) reqHeight / reqWidth;

nHeight = bHeight;
nWidth = (int) (reqWidth * parentRatio);

return Bitmap.createScaledBitmap(b, nWidth, nHeight, true);
}

但我所做的只是:

enter image description here

最佳答案

您应该尝试使用为 ScaleToFit.CENTER 构建的转换矩阵。例如:

Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER);
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);

关于Android调整位图大小保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961797/

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