62,615
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) throws Throwable {
int imageCount = 100;
int[] result = splitImage(1024, 768, imageCount);
System.out.printf("rows=%d, columns=%d\n", result[0], result[1]);
int width = 1024 / result[1];
int height = 768 / result[0];
System.out.printf("width=%d, height=%d\n", width, height);
}
public static int[] splitImage(int screenWidth, int screenHeight, int imageCount) {
int[] result = {1, 0}; //保存分割的行,列
double min = imageCount;
double w = 0, h = 0, m = 0;
for (int i=0; i<imageCount; i++) {
if (imageCount % (i+1) == 0) { //优先选择能分成m行n列的方案
h = (double)screenHeight / (i+1);
w = (double)screenWidth / (imageCount / (i+1));
m = Math.abs(h / w - 1); //优先选择分割后的图片的宽高比列最接近
if (m < min) {
result[0] = i+1;
min = m;
}
}
}
result[1] = imageCount / result[0];
return result;
}
}