6

我正在尝试通过保持纵横比来调整图像大小。它应该足够大以填充屏幕而没有空白空间,如果需要,一些图像应该在屏幕外。

下图显示了如何根据黑色屏幕大小调整黄色图像的大小。

在此处输入图像描述

这是我实际使用的代码,有没有更好的方法来做到这一点?

if(bwidth > bheight) {
    if(bwidth > swidth && bheight > sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth > swidth && bheight < sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth < swidth && bheight > sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth >= swidth && bheight >= sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
    }

} else if(bwidth < bheight) {
    if(bwidth > swidth && bheight > sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight > sheight) {        
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth >= swidth && bheight >= sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    }
}
  • swidth = 屏幕宽度
  • 高度 = 屏幕高度
  • bwidth = 图像宽度
  • bheight = 图像高度
4

2 回答 2

10

Compare ratios.

If the width to height ratio of the image is more than the width to height ratio of the screen, then you know you'll be using the screen width and calculating the height. Otherwise you'll be using the screen height and calculating the width. Just make sure none of the heights are zero!

Note that the code here will resize the image so that it will always fill the screen. This effectively crops off any additional part of the image. To make the image as large as possible while being entirely visible, change the < to a > in the first line.

if (bwidth / bheight < swidth / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth
                                 * (double) sheight / (double) bheight);
}

I also made a couple more improvements:

  • Simplified the equations. Dividing a numerator and denominator both by 100 doesn't do anything.
  • Simplified the typecasting. I don't know the type of each variable, but they all need to be doubles.
  • Used Math.floor instead of just a typecast to int to make sure it doesn't round up.
于 2013-01-07T14:27:50.327 回答
-3

好吧,谢谢@Erick Robertson 改变了一点,但现在可以了!

这是更改后的代码:

if (bwidth / swidth <  bheight / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight 
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth 
                                 * (double) sheight / (double) bheight);
}
于 2013-01-07T14:43:10.433 回答