STTLine 用点数组绘制直线或折线;同一头文件还提供 STTLineArray,用于在一个父控件内管理多条线。
#include "widgets/STTLine.h"
using namespace stt;
STTLine* line = new STTLine(getRuntime(), parent, "lineTrend");
line->setColor(0x1687d9)
->setWidth(3)
->setOpa(255)
->setRounded(true);
头文件没有 setMainArg。线的尺寸和位置主要由点坐标与父控件决定,也可使用继承自 STTWidget 的通用位置接口。
分离坐标数组:
int xs[] = {0, 40, 90, 150};
int ys[] = {20, 5, 60, 30};
line->setPoints(xs, ys, 4);
交错数组:
int xy[] = {0, 20, 40, 5, 90, 60, 150, 30};
line->setPoints(xy, 4);
字符串:
line->setPoints("0,20,40,5,90,60,150,30");
STTLine 会复制点到内部 new[] 数组,因此输入数组在调用返回后可以离开作用域。再次 setPoints 会删除旧数组。
字符串实现注意:
line->drawLine(0, 0, 100, 50);
line->drawHori(0, 200, 30);
line->drawVert(50, 0, 120);
闭合折线:
int x[] = {0, 100, 50};
int y[] = {100, 100, 0};
line->drawLine(x, y, 3, true);
point_num>2 且 close=true 时,会把第一个点追加到末尾。实现使用变长栈数组,这是 GCC 扩展,不是标准 C++;极大点数可能造成栈溢出。
line->setColor(0xff5500);
line->setOpa(180);
line->setWidth(4);
line->setRounded(true);
line->setYInvert(false);
setYInvert 相当于围绕控件水平中线镜像 Y。
虚线:
line->setDash(10, 6);
LVGL 限制虚线主要用于水平或垂直线,斜线虚线效果不可依赖。opacity 应由调用方限制在 0..255,宽度和 dash 长度应为正值。
onDataChanged 当前为空实现。即使调用 bindData,变量变化也不会改变点或样式。动态曲线需要业务回调中重新构建坐标并调用 setPoints。
STTLineArray lines(parent);
lines.width = 2;
lines.color = 0x999999;
lines.opa = 255;
lines.round = false;
lines.yInvert = false;
公共字段会应用到之后 add 的线,不会自动更新已有线。
int xy[] = {0, 0, 100, 50, 180, 20};
STTLine* custom = lines.add(xy, 3);
STTLine* horizontal = lines.addHori(40);
STTLine* vertical = lines.addVert(80);
STTLine* first = lines.get(0);
addHori 使用 owner->mSettedwidth,addVert 使用 owner->mSettedheight,因此父控件应先设置尺寸。get 对越界返回 nullptr。
lines.remove(0);
lines.removeAll();
remove 不检查索引范围,错误索引会越界。应先确认 0 <= i < lines.size()。
remove/removeAll 调用 STTWidget::del 后从 vector 移除。STTLineArray 析构函数本身为空,不会自动 removeAll;若线没有由父 LVGL 对象统一销毁,应在数组结束使用前显式 removeAll。
STTLine* border = new STTLine(getRuntime(), panel, "border");
border->setColor(0x333333)->setWidth(2)->setRounded(false);
int x[] = {0, 200, 200, 0};
int y[] = {0, 0, 100, 100};
border->drawLine(x, y, 4, true);