简体   繁体   English

通过保持不同屏幕分辨率的宽高比来计算图像大小

[英]Image size calculation by keeping aspect ratio with different screen resolutions

I am trying to resize an image by keeping the aspect ratio. 我试图通过保持纵横比来调整图像大小。 It should be just large enough to fill the screen with no blank space and if necessary some of the image should be off-screen. 它应该足够大,可以填充屏幕,没有空白区域,如果需要,一些图像应该在屏幕外。

The image below shows how the yellow image should be sized based on the black screen size. 下图显示了如何根据黑屏尺寸调整黄色图像的大小。

在此输入图像描述

Heres the Code that I am actually using, is there any better way to do this? 这是我实际使用的代码,有没有更好的方法来做到这一点?

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 = screen width swidth =屏幕宽度
  • sheight = screen height 高度=屏幕高度
  • bwidth = image width bwidth =图像宽度
  • bheight = image height bheight =图像高度

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. 将分子和分母两者除以100都不起作用。
  • 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. 使用Math.floor而不是仅仅对int进行类型转换以确保它不会向上舍入。

Well thank you @Erick Robertson Changed a litte bit, but now it works! 谢谢你@Erick Robertson改变了一点点,但现在它有效!

Here is the changed code: 这是更改的代码:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM