我正在尝试通过保持纵横比来调整图像大小。它应该足够大以填充屏幕而没有空白空间,如果需要,一些图像应该在屏幕外。
下图显示了如何根据黑色屏幕大小调整黄色图像的大小。
这是我实际使用的代码,有没有更好的方法来做到这一点?
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 = 图像高度