假如:已知屏幕的宽度是1024,高度是786。图片数量为100张。在不能要下拉列表的情况下,为了让100张图片能够刚好的显示在屏幕上,求该100张图片的宽度、高度、以及图片间距若图片数量为5、10、70、200张。。求公式。求出来的图片大小可能有很多种可能,求最合理的图片显示宽度、高度。(例如求出来的值为200每张图片大小可能为:1*200,2*100,5*40,10*20,应选中最佳的图片显示宽、高)。求大神给出个算法,纠结快一整天了,在线等
调试欢乐多
在LZ的另一个帖子也做了回答了,在这里也贴出回帖的内容吧这个应该没有什么所谓的公式吧,应该是根据需求来决定要分成多少行多少列,使得分布最合适,可以写个方法来实现,如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;
}
}
那就加修正参数调整一下就可以了
public class Test {
public static void main(String[] args) throws Throwable {
int imageCount = 100;
int hdis = 20;
int wdis = 20;
int[] result = splitImage(1024, 768, imageCount, wdis, hdis);
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 wdis, int hdis) {
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列的方案
int t = imageCount / (i+1);
h = (double)(screenHeight - i*hdis) / (i+1); //修正图片高度距离
w = (double)(screenWidth - (t-1)*wdis) / t; //修正图片宽度距离
m = Math.abs(h / w - 1); //优先选择分割后的图片的宽高比列最接近
if (m < min) {
result[0] = i+1;
min = m;
}
}
} result[1] = imageCount / result[0]; return result;
}
}