staticvoidscroll_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
*/staticvoidlv_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 cornnerlv_obj_set_scroll_dir(cont, LV_DIR_VER);//设置滚动方向LV_DIR_VERlv_obj_set_scroll_snap_y(cont, LV_SCROLL_SNAP_CENTER);//设置y轴snaplv_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);//第一个按键滚动到中间显示}