static lv_obj_t* bg_img = vd_null;
static lv_obj_t* container = vd_null;
static lv_obj_t* btn_recipe = vd_null;
static lv_obj_t* img_recipe = vd_null;
static vd_uint8_t recipeNum = 4;
static vd_char_t* recipe_img[] = {
APP_IMG_PATH "coffee1" APP_IMG_TYPE,
APP_IMG_PATH "coffee2" APP_IMG_TYPE,
APP_IMG_PATH "coffee3" APP_IMG_TYPE,
APP_IMG_PATH "coffee4" APP_IMG_TYPE,
};
/* ======================================================================================
* private implementation
*/
/**
* @brief 在滑动中实现滑动对象的子对象的子对象(图片)的缩放
* @param e:滑动事件
* @param interval:滑动对象的子对象的宽+子对象之间的间隙值
* @param zoom_max:缩放的最大值
* @param zoom_min:缩放的最小值
*/
static void scroll_zoom_event_cb(lv_event_t* e, vd_float_t interval, vd_float_t zoom_max, vd_float_t zoom_min) {
// 获取事件的初始对象
lv_obj_t* cont = lv_event_get_target(e);
// 获取子界面的数量
lv_coord_t child_cnt = lv_obj_get_child_cnt(cont);
// 获取 cont 对象y轴的中心坐标值
lv_area_t cont_pos;
lv_obj_get_coords(cont, &cont_pos);
lv_coord_t cont_x_center = cont_pos.x1 + lv_area_get_width(&cont_pos) / 2;
// 滑动事件处理
if (lv_event_get_code(e) == LV_EVENT_SCROLL) {
for (lv_coord_t i = 0; i < child_cnt; i++) {
// 通过索引获取子对象
lv_obj_t* child = lv_obj_get_child(cont, i);
// 获取子对象y轴的中心坐标值
lv_area_t child_a;
lv_obj_get_coords(child, &child_a);
lv_coord_t child_x_center = child_a.x1 + lv_area_get_width(&child_a) / 2;
// 根据子对象与cont对象中心坐标值之间的距离判断是否缩放,及缩放的大小
if (abs(cont_x_center - child_x_center) < interval) {
vd_int_t zoom_size =
(vd_int_t)((interval - abs(cont_x_center - child_x_center)) * (zoom_max - zoom_min) / interval + zoom_min);
// 将子对象的子对象进行缩放
lv_img_set_zoom(child, zoom_size);
}
}
}
}
// 滑动回调事件
static void container_event_cb(lv_event_t* e) {
// 滑动中缩放
scroll_zoom_event_cb(e, IMG_VALUE + INTERVAL_VALUE, 256, 190);
}
// 滑动菜谱设置
static void menu_display() {
container = lv_obj_create(bg_img);
lv_obj_set_size(container, 480, 360);
lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_scroll_dir(container, LV_DIR_HOR);
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
lv_obj_set_scroll_snap_x(container, LV_SCROLL_SNAP_CENTER);
lv_obj_set_style_bg_opa(container, LV_OPA_0, LV_PART_MAIN);
lv_obj_set_style_border_width(container, 0, LV_PART_MAIN);
lv_obj_set_style_pad_column(container, INTERVAL_VALUE, LV_PART_MAIN); // 图标之间的间隙
lv_obj_set_pos(container, 0, 162);
lv_obj_add_event_cb(container, container_event_cb, 0, NULL);
// set recipe img
for (int i = 0; i < recipeNum; i++) {
img_recipe = lv_img_create(container);
lv_img_set_src(img_recipe, recipe_img[i]);
lv_obj_align(img_recipe, LV_ALIGN_CENTER, 0, 0);
if (i == 0)
lv_img_set_zoom(img_recipe, 256);
else
lv_img_set_zoom(img_recipe, 190);
}
lv_obj_scroll_to_view(lv_obj_get_child(container, 0), LV_ANIM_OFF);
}
滑动缩放效果
于 2024-09-11 10:39:39 首次发布