对象蒙版(lv_objmask)¶
概述¶
绘制其子级时,对象蒙版能够向图形添加一些蒙版。
零件和样式¶
对象蒙版只有一个主要部分称为 LV_OBJMASK_PART_BG
,它使用典型的背景样式属性。
用法¶
添加蒙版¶
在向对象蒙版添加蒙版之前,应先初始化蒙版:
1 lv_draw_mask_<type>_param_t mask_param;
2 lv_draw_mask_<type>_init(&mask_param, ...);
3 lv_objmask_mask_t * mask_p = lv_objmask_add_mask(objmask, &mask_param);
Lvgl支持以下蒙版类型:
line 剪裁线条左上/右下的像素。可以从两个点或一个点和一个角度初始化:
angle 将像素仅保持在给定的开始角度和结束角度之间
radius 将像素仅保留在可以具有半径的矩形内(也可以是一个圆形)。可以反转以将像素保持在矩形之外。
fade 垂直淡入(根据像素的y位置更改像素的不透明度)
map 使用Alpha遮罩(字节数组)描述像素的不透明度。
遮罩中的坐标是相对于对象的。也就是说,如果对象移动,则蒙版也随之移动。
更新蒙版¶
可以使用 lv_objmask_update_mask(objmask, mask_p, new_param)
更新现有的掩码,其中 mask_p
是 lv_objmask_add_mask
的返回值。
移除蒙版¶
可以使用 lv_objmask_remove_mask(objmask, mask_p)
删除蒙版
范例¶
创建几个对象蒙版¶
上述效果的示例代码:
1 #include "../../../lv_examples.h"
2 #if LV_USE_OBJMASK
3
4 void lv_ex_objmask_1(void)
5 {
6
7 /*Set a very visible color for the screen to clearly see what happens*/
8 lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex3(0xf33));
9
10 lv_obj_t * om = lv_objmask_create(lv_scr_act(), NULL);
11 lv_obj_set_size(om, 200, 200);
12 lv_obj_align(om, NULL, LV_ALIGN_CENTER, 0, 0);
13 lv_obj_t * label = lv_label_create(om, NULL);
14 lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
15 lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
16 lv_obj_set_width(label, 180);
17 lv_label_set_text(label, "This label will be masked out. See how it works.");
18 lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
19
20 lv_obj_t * cont = lv_cont_create(om, NULL);
21 lv_obj_set_size(cont, 180, 100);
22 lv_obj_set_drag(cont, true);
23 lv_obj_align(cont, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -10);
24
25 lv_obj_t * btn = lv_btn_create(cont, NULL);
26 lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
27 lv_obj_set_style_local_value_str(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "Button");
28 uint32_t t;
29
30 lv_refr_now(NULL);
31 t = lv_tick_get();
32 while(lv_tick_elaps(t) < 1000);
33
34 lv_area_t a;
35 lv_draw_mask_radius_param_t r1;
36
37 a.x1 = 10;
38 a.y1 = 10;
39 a.x2 = 190;
40 a.y2 = 190;
41 lv_draw_mask_radius_init(&r1, &a, LV_RADIUS_CIRCLE, false);
42 lv_objmask_add_mask(om, &r1);
43
44 lv_refr_now(NULL);
45 t = lv_tick_get();
46 while(lv_tick_elaps(t) < 1000);
47
48 a.x1 = 100;
49 a.y1 = 100;
50 a.x2 = 150;
51 a.y2 = 150;
52 lv_draw_mask_radius_init(&r1, &a, LV_RADIUS_CIRCLE, true);
53 lv_objmask_add_mask(om, &r1);
54
55 lv_refr_now(NULL);
56 t = lv_tick_get();
57 while(lv_tick_elaps(t) < 1000);
58
59 lv_draw_mask_line_param_t l1;
60 lv_draw_mask_line_points_init(&l1, 0, 0, 100, 200, LV_DRAW_MASK_LINE_SIDE_TOP);
61 lv_objmask_add_mask(om, &l1);
62
63 lv_refr_now(NULL);
64 t = lv_tick_get();
65 while(lv_tick_elaps(t) < 1000);
66
67 lv_draw_mask_fade_param_t f1;
68 a.x1 = 100;
69 a.y1 = 0;
70 a.x2 = 200;
71 a.y2 = 200;
72 lv_draw_mask_fade_init(&f1, &a, LV_OPA_TRANSP, 0, LV_OPA_COVER, 150);
73 lv_objmask_add_mask(om, &f1);
74 }
75
76 #endif
文字蒙版¶
上述效果的示例代码:
1 #include "../../../lv_examples.h"
2 #if LV_USE_OBJMASK
3
4 #define MASK_WIDTH 100
5 #define MASK_HEIGHT 50
6
7 void lv_ex_objmask_2(void)
8 {
9
10 /* Create the mask of a text by drawing it to a canvas*/
11 static lv_opa_t mask_map[MASK_WIDTH * MASK_HEIGHT];
12
13 /*Create a "8 bit alpha" canvas and clear it*/
14 lv_obj_t * canvas = lv_canvas_create(lv_scr_act(), NULL);
15 lv_canvas_set_buffer(canvas, mask_map, MASK_WIDTH, MASK_HEIGHT, LV_IMG_CF_ALPHA_8BIT);
16 lv_canvas_fill_bg(canvas, LV_COLOR_BLACK, LV_OPA_TRANSP);
17
18 /*Draw a label to the canvas. The result "image" will be used as mask*/
19 lv_draw_label_dsc_t label_dsc;
20 lv_draw_label_dsc_init(&label_dsc);
21 label_dsc.color = LV_COLOR_WHITE;
22 lv_canvas_draw_text(canvas, 5, 5, MASK_WIDTH, &label_dsc, "Text with gradient", LV_LABEL_ALIGN_CENTER);
23
24 /*The mask is reads the canvas is not required anymore*/
25 lv_obj_del(canvas);
26
27 /*Create an object mask which will use the created mask*/
28 lv_obj_t * om = lv_objmask_create(lv_scr_act(), NULL);
29 lv_obj_set_size(om, MASK_WIDTH, MASK_HEIGHT);
30 lv_obj_align(om, NULL, LV_ALIGN_CENTER, 0, 0);
31
32 /*Add the created mask map to the object mask*/
33 lv_draw_mask_map_param_t m;
34 lv_area_t a;
35 a.x1 = 0;
36 a.y1 = 0;
37 a.x2 = MASK_WIDTH - 1;
38 a.y2 = MASK_HEIGHT - 1;
39 lv_draw_mask_map_init(&m, &a, mask_map);
40 lv_objmask_add_mask(om, &m);
41
42 /*Create a style with gradient*/
43 static lv_style_t style_bg;
44 lv_style_init(&style_bg);
45 lv_style_set_bg_opa(&style_bg, LV_STATE_DEFAULT, LV_OPA_COVER);
46 lv_style_set_bg_color(&style_bg, LV_STATE_DEFAULT, LV_COLOR_RED);
47 lv_style_set_bg_grad_color(&style_bg, LV_STATE_DEFAULT, LV_COLOR_BLUE);
48 lv_style_set_bg_grad_dir(&style_bg, LV_STATE_DEFAULT, LV_GRAD_DIR_HOR);
49
50 /* Create and object with the gradient style on the object mask.
51 * The text will be masked from the gradient*/
52 lv_obj_t * bg = lv_obj_create(om, NULL);
53 lv_obj_reset_style_list(bg, LV_OBJ_PART_MAIN);
54 lv_obj_add_style(bg, LV_OBJ_PART_MAIN, &style_bg);
55 lv_obj_set_size(bg, MASK_WIDTH, MASK_HEIGHT);
56
57 }
58
59 #endif
相关API¶
函数¶
1 lv_obj_t * lv_objmask_create(lv_obj_t * par,constlv_obj_t *copy)
2 创建对象遮罩对象
3 返回:
4 指向创建的对象掩码的指针
5 形参:
6 par:指向对象的指针,它将是新对象蒙版的父对象
7 copy:指向对象掩码对象的指针,如果不为NULL,则将从其复制新对象
8
9
10 lv_objmask_mask_t * lv_objmask_add_mask(lv_obj_t * objmask,void *param)
11 添加面膜
12 返回:
13 指向添加的蒙版的指针
14 形参:
15 objmask:指向对象遮罩对象的指针
16 param:初始化的mask参数
17
18
19 void lv_objmask_update_mask(lv_obj_t * objmask,lv_objmask_mask_t * mask,void * param )
20 更新已创建的蒙版
21 形参:
22 objmask:指向对象遮罩对象的指针
23 mask:指向创建的遮罩的指针(由返回:lv_objmask_add_mask)
24 param:初始化的mask参数(由初始化lv_draw_mask_line/angle/.../_init)
25
26
27 void lv_objmask_remove_mask(lv_obj_t * objmask,lv_objmask_mask_t * mask )
28 取下口罩
29 形参:
30 objmask:指向对象遮罩对象的指针
31 mask:指向创建的遮罩的指针(由返回:lv_objmask_add_mask)如果NULL