lvgl V7绘制虚线的示例diam
时间: 2023-07-23 22:15:21 浏览: 193
css仿word首字下沉效果示例
以下是使用 LVGL V7 绘制虚线的示例代码:
```c
static void draw_dashed_line(lv_obj_t * obj, const lv_area_t * clip_area, const lv_style_t * style, const lv_draw_line_dsc_t * line_dsc)
{
int16_t w = lv_area_get_width(clip_area);
int16_t h = lv_area_get_height(clip_area);
int16_t dash_len = 5;
int16_t gap_len = 5;
int16_t x1, y1, x2, y2;
lv_draw_get_line_end_points(line_dsc, clip_area, &x1, &y1, &x2, &y2);
int16_t dx = x2 - x1;
int16_t dy = y2 - y1;
if (dx == 0 && dy == 0) {
return;
}
int16_t len = lv_sqrt(dx * dx + dy * dy);
int16_t x = x1;
int16_t y = y1;
int16_t dash_count = 0;
int16_t dash_gap_count = 0;
while (len--) {
if (dash_count >= dash_len) {
dash_gap_count = 0;
dash_count = 0;
}
if (dash_gap_count >= gap_len) {
dash_gap_count = 0;
dash_count = 0;
}
if (dash_count == 0) {
lv_draw_px(obj, x, y, clip_area, style);
}
if (dx != 0) {
x += (dx > 0) ? 1 : -1;
}
if (dy != 0) {
y += (dy > 0) ? 1 : -1;
}
dash_count++;
dash_gap_count++;
}
lv_draw_px(obj, x2, y2, clip_area, style);
}
static void lv_obj_draw_dashed_line(lv_obj_t * obj, const lv_area_t * clip_area, const lv_style_t * style, const lv_draw_line_dsc_t * line_dsc)
{
draw_dashed_line(obj, clip_area, style, line_dsc);
}
void lv_demo_widgets(void)
{
/* Create a screen */
lv_obj_t * scr = lv_disp_get_scr_act(NULL);
/* Create a button with a line on it */
lv_obj_t * btn = lv_btn_create(scr, NULL);
lv_obj_set_size(btn, 150, 50);
lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
lv_style_t style;
lv_style_copy(&style, &lv_style_plain);
style.line.width = 2;
style.line.color = LV_COLOR_RED;
lv_draw_line_dsc_t line_dsc;
lv_draw_line_dsc_init(&line_dsc);
line_dsc.color = LV_COLOR_RED;
lv_obj_set_style_local_line_width(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 2);
lv_obj_set_style_local_line_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_obj_set_style_local_line_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_obj_set_style_local_line_rounded(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, true);
lv_obj_set_style_local_border_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_obj_set_style_local_border_width(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 2);
lv_obj_set_style_local_border_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_shadow_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_outline_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_pad_all(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_draw_cb(btn, lv_obj_draw_dashed_line);
lv_obj_t * label = lv_label_create(btn, NULL);
lv_label_set_text(label, "Button");
}
```
这个示例代码中,我们定义了一个 `draw_dashed_line` 函数,该函数使用 Bresenham 算法绘制虚线。然后,我们创建了一个按钮,并为其设置了样式。最后,我们使用 `lv_obj_set_draw_cb` 函数将 `draw_dashed_line` 函数作为按钮的绘制回调函数,从而实现了在按钮上绘制虚线。
阅读全文