您当前的位置: 首页 > 

lvgl v8之Translate on scroll

发布时间:2021-10-31 18:40:11 ,浏览量:5

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); //第一个按键滚动到中间显示 } 
显示效果图

在这里插入图片描述

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2326s