版本1:根据比例缩放
lv_obj_t * lv_img_zoom(lv_obj_t * parent,const char * imgPath,double factor)
{
lv_img_header_t header;
if (lv_img_decoder_get_info(imgPath, &header) != LV_RES_OK)
{
return nullptr;
}
uint32_t objW = factor * header.w;
uint32_t objH = factor * header.h;
lv_obj_t * obj = lv_obj_create(parent);
lv_obj_remove_style_all(obj);
lv_obj_set_size(obj,objW,objH);
lv_obj_set_scroll_dir(obj,LV_DIR_NONE);//禁止滚动
lv_obj_t * img = lv_img_create(obj);
lv_img_set_src(img, imgPath);
lv_img_set_zoom(img,256 * factor);
lv_obj_center(img);
return obj;
}
void lv_example1(void)
{
auto obj = lv_img_zoom(lv_scr_act(),"M:mario.png",0.7);
lv_obj_set_pos(obj,0,0);
auto obj2 = lv_img_zoom(lv_scr_act(),"M:mario.png",1.0);
lv_obj_set_pos(obj2,200,0);
auto obj3 = lv_img_zoom(lv_scr_act(),"M:mario.png",1.5);
lv_obj_set_pos(obj3,450,0);
}
版本2:设置缩放后控件区域,计算缩放比例
void lv_img_zoom(lv_obj_t * parent,const char * imgPath,lv_area_t area)
{
lv_img_header_t header;
if (lv_img_decoder_get_info(imgPath, &header) != LV_RES_OK)
{
return ;
}
uint32_t imgW = header.w;
uint32_t imgH = header.h;
auto zreaW = std::abs(area.x2 - area.x1);
auto zreaH = std::abs(area.y2 - area.y1);
double radioX = static_cast<double>(zreaW) / imgW;
double radioY = static_cast<double>(zreaH) / imgH;
double factor = std::min(radioX,radioY);
lv_obj_t * obj = lv_obj_create(parent);
lv_obj_remove_style_all(obj);
lv_obj_set_size(obj,zreaW,zreaH);
lv_obj_set_pos(obj,area.x1,area.y1);
lv_obj_set_scroll_dir(obj,LV_DIR_NONE);//禁止滚动
lv_obj_t * img = lv_img_create(obj);
lv_img_set_src(img, imgPath);
lv_img_set_zoom(img,256 * factor);
lv_obj_center(img);
}
void lv_example2()
{
lv_img_zoom(lv_scr_act(),"M:mario.png",lv_area_t{0,0,100,200});
lv_img_zoom(lv_scr_act(),"M:mario.png",lv_area_t{210,0,360,380});
lv_img_zoom(lv_scr_act(),"M:mario.png",lv_area_t{370,0,800,480});
}