侧边栏壁纸
博主头像
潦草地博主等级

行动起来,活在当下

  • 累计撰写 8 篇文章
  • 累计创建 2 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

LVGL 堆叠滑动2

Storen
2025-03-10 / 0 评论 / 0 点赞 / 5 阅读 / 3422 字
static void page_slide_event_cb_2(lv_event_t *e);

void lv_example_stack_5(void)
{
    lv_obj_t *root = lv_obj_create(lv_scr_act());
    lv_obj_set_size(root, 466, 466);
    clear_pad_border_all(root);
    lv_obj_t *content = lv_obj_create(root);
    lv_obj_set_size(content, 466, 466);
    clear_pad_border_all(content);
    lv_obj_set_style_bg_color(content,get_random_hex_color(), 0);
    // lv_obj_set_style_bg_opa(content, LV_OPA_0, 0);
    for (int i = 0; i < PAGE_COUNT; i++)
    {
        lv_obj_t *page = lv_obj_create(content);
        lv_obj_set_size(page, 466, 466 * 2);
        lv_obj_set_pos(page, 0, i * 466);
        lv_obj_set_style_bg_color(page, get_random_hex_color(), 0);
        lv_obj_set_style_bg_opa(page, LV_OPA_0, 0);
        clear_pad_border_all(page);
        // Page Content
        lv_obj_t *page_content = lv_obj_create(page);
        clear_pad_border_all(page_content);
        lv_obj_set_size(page_content, 466, 466);
        lv_obj_align(page_content, LV_ALIGN_TOP_MID, 0, 233);
        page_contents[i] = page_content;
        // Set Image
        lv_obj_t *img_bg = lv_img_create(page_content);
        lv_obj_set_size(img_bg, 466, 466);
        lv_obj_align(img_bg, LV_ALIGN_TOP_MID, 0, 0);
        if (i % 3 == 0)
        {
            lv_img_set_src(img_bg, &girl_2);
        }
        else if (i % 3 == 1)
        {
            lv_img_set_src(img_bg, &girl_3);
        }
        else
        {
            lv_img_set_src(img_bg, &girl_466);
        }
        lv_obj_t *btn = lv_btn_create(page_content);
        lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, 300);
    }

    lv_obj_set_scroll_dir(content, LV_DIR_VER);
    lv_obj_set_scroll_snap_y(content, LV_SCROLL_SNAP_CENTER);
    lv_obj_scroll_to(content, 0, 232, LV_ANIM_OFF);
    lv_obj_scroll_to(content, 0, 233, LV_ANIM_ON);

    lv_obj_add_event_cb(content, page_slide_event_cb_2, LV_EVENT_SCROLL, content);
}

static void page_slide_event_cb_2(lv_event_t *e)
{
    lv_obj_t *content = lv_event_get_target_obj(e);
    int st = lv_obj_get_scroll_top(content) - 233;
    float slide_progress = st * 1.0 / 466;
    int page_index = (int)slide_progress;
    int offset_y = st - 466 * page_index;
    if (offset_y > 466)
    {
        offset_y = 466;
    }
    if (offset_y < 0)
    {
        offset_y = 0;
    }
    if (page_index == PAGE_COUNT - 1)
    {
        offset_y = 0;
    }
    float page_progress = limit_float_value(slide_progress - page_index, 0, 1);
    float cur_page_progress = limit_float_value(page_progress / 0.5, 0, 1);
    lv_obj_set_style_translate_y(page_contents[page_index], offset_y, 0);
    lv_obj_set_style_opa(page_contents[page_index], LV_OPA_100 * (1 - cur_page_progress), 0);
    // Update Next Page
    if (page_index + 1 < PAGE_COUNT)
    {
        float next_page_progress = limit_float_value((page_progress - 0.5) / (1 - 0.5), 0, 1);
        lv_obj_set_style_translate_y(page_contents[page_index + 1], -200 * (1 - next_page_progress), 0);
        lv_obj_set_style_opa(page_contents[page_index + 1], LV_OPA_100 * next_page_progress, 0);
        if (page_progress == 0)
        {
            lv_obj_add_flag(page_contents[page_index + 1]->parent, LV_OBJ_FLAG_HIDDEN);
        }
        else
        {
            lv_obj_remove_flag(page_contents[page_index + 1]->parent, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

0

评论区