1

给定动态数量的项目,我必须(尽可能)平均分割屏幕。

所以我需要根据屏幕大小/比例找到列数和行数。

每个项目的大小并不重要,因为我将根据每列和每行的项目以百分比计算它们。

如果我有 5 个项目,那么(取决于屏幕比例)我可能在第一行有 3 列,在第二行有 2 列。没关系。

4

1 回答 1

2

首先,您必须确定“平均划分屏幕”的含义。这可能意味着每个项目都有一个首选的 x 与 y 比率。

您可以使用以下算法,该算法有利于接近所需的 x 与 y 比率,而不是减少空白空间的数量。

// Set this to the desired x-to-y ratio.
const preferred_ratio = 1.2; 

function calc_cols_rows(In: screen, In: num_items, Out: num_rows, Out: num_cols) {
  desired_aspect = (screen.width / screen.height) / preferred_ratio;

  // Calculate the number of rows that is closest to the desired aspect:
  num_rows = round(sqrt(num_items / desired_aspect));

  // Calculate the required number of columns:
  num_cols = ceil(num_items / num_rows);
}
于 2012-05-18T00:42:13.537 回答