我正在考虑如何在手机的主屏幕上创建一个巨大按钮的错觉。
我的计划是修改主屏幕背景艺术(墙纸),使其看起来像一个巨大的按钮。然后,我将使2 x 2或3 x 3的主屏幕快捷方式不可见。所有不可见的快捷方式都会启动同一应用。
主屏幕图标网格通常是5个图标乘5个图标,但是如果改变了怎么办?例如,如果用户视力受损并且使用大图标设置该怎么办。
这变成了以下问题:即使没有太多像素可以使用,我们如何绘制一个完全位于画布中央左右的框?例如,手机图标网格通常只有5个图标乘5个图标。
我们希望在像素网格上绘制一个“按钮”,以使该按钮在画布上居中。如果像素网格非常大,那么我们希望按钮大约是像素网格宽度的60%。但是,像素网格通常很小。
我们有以下限制条件:
我们的目标如下:
在典型的编程语言中,我们如何编写函数来计算最佳按钮宽度?该功能接受像素网格宽度作为输入,并且功能是输出按钮宽度。
答案 0 :(得分:0)
在进行任何编程之前,我们先做一些数学运算:
让screen_width
为表示像素网格宽度的变量。
假设button_width
是表示按钮宽度的变量。
如果要使居中居中,则screen_width
为奇数,则button_width
也必须为奇数。另外,如果screen_width
是偶数,则button_width
是偶数。最初,该按钮的像素数至少为1像素。但是,如果像素网格的像素数为偶数,则可以将其扩展到最少2个像素。
以下是某些示例画布(screen_width
)尺寸的结果:
+--------+---------+
| Canvas | Central |
| width | Button |
| | width |
+--------+---------+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
| 5 | 3 |
+--------+---------+
让n = max {整数k,以使k小于或等于屏幕宽度的60%,并且k与屏幕宽度具有相同的奇偶校验(偶数或奇数)。
让m是一个变量,如果(屏幕宽度为奇数),则m = 1;如果屏幕宽度为偶数,则为(m = 2)。
让button_width
最小{n,m}
然后button_width
具有所有所需的属性。
现在,我们终于写了一些代码:
int CALC_BUTTON_WIDTH(int screen_width, float fraction) {
int bw_lrg = CALC_BUTTON_WIDTH_FOR_LRG_CANVAS(screen_width, fraction);
int bw_sml = CALC_BUTTON_WIDTH_FOR_SML_CANVAS(screen_width, fraction);
button width = min(bw_sml, bw_lrg);
return button_width;
}
int CALC_BUTTON_WIDTH_FOR_SML_CANVAS(int screen_width, float fraction) {
// sw_pairity --- parity of screen width
// sw_pairity is 0 if screen width is even
// sw_pairity is 1 if screen width is odd
sw_pairity = screen width % 2;
// sw_pairity_inv --- inverse of parity of screen width
// sw_pairity_inv is 1 if screen width is even
// sw_pairity_inv is 0 if screen width is odd
sw_pairity_inv = 1 – sw_pairity;
// bw_sml is the button with minimum.
// Either 1 or 2 pixels
// if screen is even number of pixels wide, bw_sml is 1
// if screen is odd number of pixels wide, bw_sml is 0
// Example: screen 3 --> button 1
// Example: screen 4 --> button 2
//
// --X- button not centered!
// -XX- okay
//
// --X-- okay
// -XX-- button not centered!
bw_sml = 1 + sw_pairity_inv;
return bw_sml;
}
int CALC_BUTTON_WIDTH_FOR_LRG_CANVAS(int screen_width, float fraction) {
int sw_pairity;
int bw_lrg;
sw_pairity = screen width % 2;
bw_lrg = floor(fraction * screen_width);
bw_lrg_parity = bw_lrg % 2;
correction = (sw_pairity + bw_lrg_parity) % 2;
bw_lrg = bw_lrg + correction;
return bw_lrg;
}