lvgl v8之Translate on scroll

Translate on scroll事件处理


static void scroll_event_cb(lv_event_t* e)
{
    lv_obj_t* cont = lv_event_get_target(e); //获取产生事件的对象
    lv_area_t cont_a;   
    lv_obj_get_coords(cont, &cont_a);  // 获取坐标信息
    lv_coord_t cont_y_center = cont_a.y1 + lv_area_get_height(&cont_a) / 2; //计算中点
    lv_coord_t r = lv_obj_get_height(cont) * 7 / 10; // 计算半径
    uint32_t i;
    uint32_t child_cnt = lv_obj_get_child_cnt(cont); // 获取子对象数量
    for (i = 0; i < child_cnt; i++) {
        lv_obj_t* child = lv_obj_get_child(cont, i); // 根据序号获取子对象
        lv_area_t child_a;
        lv_obj_get_coords(child, &child_a); //获取子对象的坐标信息
        lv_coord_t child_y_center = child_a.y1 + lv_area_get_height(&child_a) / 2;      // 计算子对象的中点
        lv_coord_t diff_y = child_y_center - cont_y_center; //计算子对象child_a的中点与父对象cont的中点之差
        diff_y = LV_ABS(diff_y); // 取中点差绝对值
        /*Get the x of diff_y on a circle.*/
        lv_coord_t x;
        /*If diff_y is out of the circle use the last point of the circle (the␣
        ,→radius)*/
        if (diff_y >= r) { //中点差值大于等于半径
            x = r;
        }
        else { //中点差值小于半径
            /*Use Pythagoras theorem to get x from radius and y*/
            lv_coord_t x_sqr = r * r - diff_y * diff_y; //计算方差值
            lv_sqrt_res_t res;
            lv_sqrt(x_sqr, &res, 0x8000); /*Use lvgl's built in sqrt root function*/
            x = r - res.i;
        }
        /*Translate the item by the calculated X coordinate*/
        lv_obj_set_style_translate_x(child, x, 0);  
        /*Use some opacity with larger translations*/
        lv_opa_t opa = lv_map(x, 0, r, LV_OPA_TRANSP, LV_OPA_COVER);
        lv_obj_set_style_opa(child, LV_OPA_COVER - opa, 0); 
    }
}

Translate the object as they scroll

/**
* Translate the object as they scroll
*/
static void lv_example_scroll_6(void)
{
    lv_obj_t* cont = lv_obj_create(lv_scr_act()); //创建obj对象
    lv_obj_set_size(cont, 200, 200); // 设置大小
    lv_obj_center(cont);   // 居中显示
    lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN); //设置LV_FLEX_FLOW_COLUMN布局
    lv_obj_add_event_cb(cont, scroll_event_cb, LV_EVENT_SCROLL, NULL);//设置滚动事件
    lv_obj_set_style_radius(cont, LV_RADIUS_CIRCLE, 0); /设置圆形风格
    lv_obj_set_style_clip_corner(cont, true, 0); // 设置clip cornner
    lv_obj_set_scroll_dir(cont, LV_DIR_VER);  //设置滚动方向LV_DIR_VER
    lv_obj_set_scroll_snap_y(cont, LV_SCROLL_SNAP_CENTER); //设置y轴snap
    lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);//关闭滚动条显示
    uint32_t i;
    for (i = 0; i < 20; i++) {  //创建20个按键对象
        lv_obj_t* btn = lv_btn_create(cont);  
        lv_obj_set_width(btn, lv_pct(100));
        lv_obj_t* label = lv_label_create(btn);
        lv_label_set_text_fmt(label, "Button %d", i);
    }
    /*Update the buttons position manually for first*/
    lv_event_send(cont, LV_EVENT_SCROLL, NULL);
    /*Be sure the fist button is in the middle*/
    lv_obj_scroll_to_view(lv_obj_get_child(cont, 0), LV_ANIM_OFF); //第一个按键滚动到中间显示
}

显示效果图

在这里插入图片描述

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值