static void sw1_event_cb(lv_obj_t * obj, lv_event_t event)
{
uint32_t color = 0;
if (event == LV_EVENT_VALUE_CHANGED)
{
printf("value changed\n");
printf("state=%d\n",lv_sw_get_state(obj));
}
}
void demo_create(void)
{
lv_obj_t *scr = lv_disp_get_scr_act(NULL); /* 获取当前屏幕 */
static lv_style_t bg_style;
static lv_style_t indic_style;
static lv_style_t knob_on_style1;
static lv_style_t knob_on_style2;
static lv_style_t knob_on_style3;
static lv_style_t knob_off_style;
/* 设置 style 属性 */
lv_style_copy(&bg_style, &lv_style_pretty);
bg_style.body.radius = LV_RADIUS_CIRCLE; /* 圆形 */
bg_style.body.padding.top = 6; /* 顶部填充 */
bg_style.body.padding.bottom = 6; /* 底部填充 */
lv_style_copy(&indic_style, &lv_style_pretty_color);
indic_style.body.radius = LV_RADIUS_CIRCLE; /* 圆形 */
indic_style.body.main_color = lv_color_hex(0x9fc8ef);/* 主颜色 */
indic_style.body.grad_color = lv_color_hex(0x9fc8ef); /* 阴影颜色 */
indic_style.body.padding.left = 0; /* 上下左右填充 */
indic_style.body.padding.right = 0;
indic_style.body.padding.top = 0;
indic_style.body.padding.bottom = 0;
lv_style_copy(&knob_off_style, &lv_style_pretty);
knob_off_style.body.radius = LV_RADIUS_CIRCLE; /* 圆形 */
knob_off_style.body.shadow.width = 4; /* 阴影宽度 */
knob_off_style.body.shadow.type = LV_SHADOW_BOTTOM; /* 底部阴影 */
lv_style_copy(&knob_on_style1, &lv_style_pretty_color);
knob_on_style1.body.radius = LV_RADIUS_CIRCLE; /* 圆形 */
knob_on_style1.body.shadow.width = 4; /* 阴影宽度 */
knob_on_style1.body.shadow.type = LV_SHADOW_BOTTOM; /* 底部阴影 */
knob_on_style1.body.main_color = LV_COLOR_RED; /* 主色 */
lv_obj_t *sw1 = lv_sw_create(scr, NULL); /* 创建 sw 控件 */
lv_sw_set_style(sw1, LV_SW_STYLE_BG, &bg_style); /* 设置背景样式 */
lv_sw_set_style(sw1, LV_SW_STYLE_INDIC, &indic_style); /* 填充样式 */
lv_sw_set_style(sw1, LV_SW_STYLE_KNOB_OFF, &knob_off_style);/* 开关关闭的样式 */
lv_sw_set_style(sw1, LV_SW_STYLE_KNOB_ON, &knob_on_style1); /* 开关打开的样式 */
lv_obj_set_size(sw1, 120, 50); /* 设置尺寸 */
lv_obj_align(sw1, NULL, LV_ALIGN_CENTER, 0, -100); /* 设置位置 */
lv_obj_t *label_sw1 = lv_label_create(scr, NULL); /* 创建 label 控件 */
lv_label_set_text(label_sw1, "RED"); /* 设置文本 */
lv_obj_align(label_sw1, sw1, LV_ALIGN_IN_LEFT_MID, -50, 0); /* 设置位置 */
lv_obj_set_event_cb(sw1, sw1_event_cb); /* 为 sw 控件设置事件回调函数 */
}