如果我有一个我知道高度和宽度的图像,如何将它放在具有最大可能尺寸的矩形中而不会拉伸图像。使图像适合长方形
伪代码已经足够了(但我打算在Java中使用它)。
谢谢。
所以,根据答案,我写了这个:但它不起作用。我做错了什么?
double imageRatio = bi.getHeight()/bi.getWidth();
double rectRatio = getHeight()/getWidth();
if (imageRatio < rectRatio)
{
// based on the widths
double scale = getWidth()/bi.getWidth();
g.drawImage(bi, 0, 0, (int) (bi.getWidth() * scale), (int) (bi.getHeight() * scale), this);
}
if (rectRatio < imageRatio)
{
// based on the height
double scale = getHeight()/bi.getHeight();
g.drawImage(bi, 0, 0 , (int) (bi.getWidth() * scale), (int) (bi.getHeight() * scale), this);
}
你的意思是保持拟合方面宽高比? – 2010-04-29 19:11:30
@SB:我认为是这样(我不明白你的意思是什么......)所以,源图像和缩放图像的宽度和高度的比例必须相同。 – 2010-04-29 19:13:20