为了实现通用接口模块化画矩形,可以更改横纵参数来改变框的个数,实际效果如下:
左半边的实现代码,通过移动坐标系:
if (1 != left)
{
/******************左耳横线***********************/
p->translate(12, 1 * y / 5 - 2);
p->drawLine(0, 0, line, 0);
/******************左耳横线***********************/
p->translate(0, 2);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
p->drawRoundedRect(rect, 1, 1);
p->translate(single_x + 3, 0);
}
p->translate(-(single_x + 3) * m, single_y + 2);
}
p->translate(-12, -(single_y + 2) * n - 1.0 * y / 5);//坐标还原回去,到大框的起始点
/*************整体加减*************/
{
p->translate(12 + (single_x + 3) * m, 1 * y / 5);
p->drawRoundedRect(0, 0, 18, (single_y + 2)* n - 2, 0, 0);
for (int i = 1; i <= n; i++)
{
if (i == n)
{
//p->drawLine(0, (single_y + 2) * i, 18, (single_y + 2) * i - 2);//不用画了这个分界线
}
else
{
p->drawLine(0, (single_y + 2) * i, 18, (single_y + 2) * i); //画框的分界线
}
p->drawPixmap(1, (single_y + 2) *(i - 1) + (single_y + 2) / 2 - 8, 16, 16, updown);//画整体加减的标志
}
p->translate(-(12 + (single_x + 3) * m), -1 * y / 5);
}
}
右半边的实现代码,通过更改画矩形框坐标位置:
if (1 != right)
{
/******************左耳横线***********************/
p->translate(x - 12 - line, 1 * y / 5 - 2);
p->drawLine(0, 0, line, 0);
/******************左耳横线***********************/
/*************整体加减*************/
{
p->translate(0, 2);
p->drawRoundedRect(0, 0, 18, (single_y + 2)* n - 2, 0, 0);
for (int i = 1; i <= n; i++)
{
if (i == n)
{
//p->drawLine(0, (single_y + 2) * i, 18, (single_y + 2) * i - 2);//不用画了这个分界线
}
else
{
p->drawLine(0, (single_y + 2) * i, 18, (single_y + 2) * i); //画框的分界线
}
p->drawPixmap(1, (single_y + 2) *(i - 1) + (single_y + 2) / 2 - 8, 16, 16, updown);//画整体加减的标志
}
p->translate(21, 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
p->drawRoundedRect((single_x + 3)*(j - 1), (single_y + 2) * (i - 1), single_x, single_y, 0, 0);
}
}
}
/*************整体加减*************/
p->translate(-(x - 12 - line) - 21, -1 * y / 5);
/* p->translate(21, 2);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
p->drawRoundedRect(rect, 1, 1);
p->translate(single_x + 3, 0);
}
p->translate(-(single_x + 3) * m, single_y + 2);
}*/
}
分别循环执行100次所花费的时间:
结论:通过测试发现直接画矩形框的效率要比通过移动坐标系的效率高,这里100次循环的情况下是2倍左右,随着测试量增加,直接画图矩形框的效率会比通过移动坐标系画矩形框的效率会越来越高。