bool pressed;
static void event_cb000(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);

    // if(code == LV_EVENT_VALUE_CHANGED) {
    //     lv_obj_invalidate(obj);
    // }
    if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
        lv_coord_t * s = (lv_coord_t *)lv_event_get_param(e);
        *s = LV_MAX(*s, 20);
    }
    else if(code == LV_EVENT_DRAW_POST_END)
    {
        lv_draw_rect_dsc_t rect_dsc;
        lv_draw_rect_dsc_init(&rect_dsc);
        lv_obj_init_draw_rect_dsc(obj, LV_PART_MAIN, &rect_dsc);
        rect_dsc.bg_color = lv_color_hex(0xe0e0e0);
        rect_dsc.bg_grad_color = lv_color_hex(0x1c1c1c);
        rect_dsc.bg_grad_dir = LV_GRAD_DIR_HOR;

        const lv_area_t * clip_area = (const lv_area_t *)lv_event_get_param(e);

        lv_area_t area = obj->coords ;

        lv_obj_draw_part_dsc_t  part_draw_dsc;
        lv_obj_draw_dsc_init(&part_draw_dsc, clip_area);
        part_draw_dsc.class_p =  &lv_obj_class;
        part_draw_dsc.type = LV_LED_DRAW_PART_RECTANGLE;
        part_draw_dsc.rect_dsc = &rect_dsc;
        part_draw_dsc.part = LV_PART_MAIN;

        lv_draw_rect(&area, clip_area, &rect_dsc);

        area.x1 += 20;
        area.x2 -= 20;
        area.y1 += 20;
        area.y2 -= 20;
        rect_dsc.bg_color = lv_color_hex(0x1c1c1c);
        rect_dsc.bg_grad_color = lv_color_hex(0xe0e0e0);
        lv_draw_rect(&area, clip_area, &rect_dsc);

        area.x1 += 10;
        area.x2 -= 10;
        area.y1 += 10;
        area.y2 -= 10;

        rect_dsc.bg_grad_dir = LV_GRAD_DIR_NONE;

        rect_dsc.bg_color = lv_color_hex(0x128bf1);

        if(pressed)
        {
            rect_dsc.bg_color = lv_color_lighten(lv_color_hex(0x128bf1),LV_OPA_20);
        }
        lv_draw_rect(&area, clip_area, &rect_dsc);
    }
    else if(code == LV_EVENT_PRESSED)
    {
        pressed = true;
        lv_obj_invalidate(obj);
    }
    else if(code == LV_EVENT_RELEASED)
    {
        pressed = false;
        lv_obj_invalidate(obj);
    }
}

int main(int argc, char **argv)
{
    lv_init();
    hal_init();

    lv_log_register_print_cb(esp32_log_cb);

    {
        lv_obj_t * obj = lv_obj_create(lv_scr_act());
        lv_obj_set_size(obj, 200, 200);
        lv_obj_center(obj);

        auto style = new lv_style_t;
        lv_style_init(style);
        lv_style_set_bg_color(style,lv_color_hex(0xcc2222));
        lv_style_set_radius(style, LV_RADIUS_CIRCLE);
        lv_style_set_border_color(style,lv_color_hex(0x00dd66));
        lv_style_set_border_width(style,0);
        lv_obj_add_style(obj, style, LV_PART_MAIN);

        lv_obj_add_event_cb(obj, event_cb000,LV_EVENT_ALL,0);
    }

    while (1)
    {
        lv_timer_handler();
        usleep(5 * 1000);
    }

    return 0;
}

LVGL编写自定义控件:一个指示灯样式-LMLPHP

比较粗糙但主要代码思路很明确,可自行封装成一个新类型。

04-03 16:40