mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-22 15:24:16 +08:00
feat(image): support symbol images with inner alignment (#8182)
This commit is contained in:
@@ -884,7 +884,26 @@ static void draw_image(lv_event_t * e)
|
||||
label_dsc.base.layer = layer;
|
||||
lv_obj_init_draw_label_dsc(obj, LV_PART_MAIN, &label_dsc);
|
||||
label_dsc.text = img->src;
|
||||
lv_draw_label(layer, &label_dsc, &obj->coords);
|
||||
lv_area_t * coords;
|
||||
lv_area_t aligned_coords;
|
||||
const bool image_area_is_same_as_coords = img->w == lv_area_get_width(&obj->coords)
|
||||
&& img->h == lv_area_get_height(&obj->coords);
|
||||
const bool needs_inner_alignment = img->align > LV_IMAGE_ALIGN_TOP_LEFT
|
||||
&& !image_area_is_same_as_coords;
|
||||
const bool has_offset = img->offset.x || img->offset.y;
|
||||
const bool inner_alignment_is_transforming = img->align >= LV_IMAGE_ALIGN_AUTO_TRANSFORM;
|
||||
if((needs_inner_alignment || has_offset) && !inner_alignment_is_transforming) {
|
||||
lv_point_t text_size;
|
||||
lv_text_get_size(&text_size, label_dsc.text, label_dsc.font, label_dsc.letter_space,
|
||||
label_dsc.line_space, LV_COORD_MAX, LV_TEXT_FLAG_NONE);
|
||||
lv_area_set(&aligned_coords, 0, 0, text_size.x, text_size.y);
|
||||
lv_area_align(&obj->coords, &aligned_coords, img->align, img->offset.x, img->offset.y);
|
||||
coords = &aligned_coords;
|
||||
}
|
||||
else {
|
||||
coords = &obj->coords;
|
||||
}
|
||||
lv_draw_label(layer, &label_dsc, coords);
|
||||
}
|
||||
else if(img->src == NULL) {
|
||||
/*Do not need to draw image when src is NULL*/
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.6 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
@@ -661,4 +661,48 @@ void test_image_properties(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_image_symbol_normal_align(void)
|
||||
{
|
||||
lv_obj_t * img;
|
||||
uint32_t i;
|
||||
lv_image_align_t aligns[] = {
|
||||
LV_IMAGE_ALIGN_TOP_LEFT, LV_IMAGE_ALIGN_TOP_MID, LV_IMAGE_ALIGN_TOP_RIGHT,
|
||||
LV_IMAGE_ALIGN_LEFT_MID, LV_IMAGE_ALIGN_CENTER, LV_IMAGE_ALIGN_RIGHT_MID,
|
||||
LV_IMAGE_ALIGN_BOTTOM_LEFT, LV_IMAGE_ALIGN_BOTTOM_MID, LV_IMAGE_ALIGN_BOTTOM_RIGHT,
|
||||
};
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
img = img_create();
|
||||
lv_image_set_src(img, LV_SYMBOL_IMAGE);
|
||||
lv_obj_set_size(img, 200, 120);
|
||||
lv_obj_set_pos(img, 30 + (i % 3) * 260, 40 + (i / 3) * 150);
|
||||
lv_image_set_inner_align(img, aligns[i]);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_SCREENSHOT("widgets/image_symbol_normal_align.png");
|
||||
}
|
||||
|
||||
void test_image_symbol_normal_align_offset(void)
|
||||
{
|
||||
lv_obj_t * img;
|
||||
uint32_t i;
|
||||
lv_image_align_t aligns[] = {
|
||||
LV_IMAGE_ALIGN_TOP_LEFT, LV_IMAGE_ALIGN_TOP_MID, LV_IMAGE_ALIGN_TOP_RIGHT,
|
||||
LV_IMAGE_ALIGN_LEFT_MID, LV_IMAGE_ALIGN_CENTER, LV_IMAGE_ALIGN_RIGHT_MID,
|
||||
LV_IMAGE_ALIGN_BOTTOM_LEFT, LV_IMAGE_ALIGN_BOTTOM_MID, LV_IMAGE_ALIGN_BOTTOM_RIGHT,
|
||||
};
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
img = img_create();
|
||||
lv_image_set_src(img, LV_SYMBOL_IMAGE);
|
||||
lv_obj_set_size(img, 200, 120);
|
||||
lv_obj_set_pos(img, 30 + (i % 3) * 260, 40 + (i / 3) * 150);
|
||||
lv_image_set_inner_align(img, aligns[i]);
|
||||
lv_image_set_offset_x(img, 10);
|
||||
lv_image_set_offset_y(img, 15);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_SCREENSHOT("widgets/image_symbol_normal_align_offset.png");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user