1 #include "../lv_examples.h"
2 #if LV_BUILD_EXAMPLES && LV_USE_FLEX
3 
scroll_event_cb(lv_event_t * e)4 static void scroll_event_cb(lv_event_t * e)
5 {
6     lv_obj_t * cont = lv_event_get_target(e);
7 
8     lv_area_t cont_a;
9     lv_obj_get_coords(cont, &cont_a);
10     lv_coord_t cont_y_center = cont_a.y1 + lv_area_get_height(&cont_a) / 2;
11 
12     lv_coord_t r = lv_obj_get_height(cont) * 7 / 10;
13     uint32_t i;
14     uint32_t child_cnt = lv_obj_get_child_cnt(cont);
15     for(i = 0; i < child_cnt; i++) {
16         lv_obj_t * child = lv_obj_get_child(cont, i);
17         lv_area_t child_a;
18         lv_obj_get_coords(child, &child_a);
19 
20         lv_coord_t child_y_center = child_a.y1 + lv_area_get_height(&child_a) / 2;
21 
22         lv_coord_t diff_y = child_y_center - cont_y_center;
23         diff_y = LV_ABS(diff_y);
24 
25         /*Get the x of diff_y on a circle.*/
26         lv_coord_t x;
27         /*If diff_y is out of the circle use the last point of the circle (the radius)*/
28         if(diff_y >= r) {
29             x = r;
30         }
31         else {
32             /*Use Pythagoras theorem to get x from radius and y*/
33             uint32_t x_sqr = r * r - diff_y * diff_y;
34             lv_sqrt_res_t res;
35             lv_sqrt(x_sqr, &res, 0x8000);   /*Use lvgl's built in sqrt root function*/
36             x = r - res.i;
37         }
38 
39         /*Translate the item by the calculated X coordinate*/
40         lv_obj_set_style_translate_x(child, x, 0);
41 
42         /*Use some opacity with larger translations*/
43         lv_opa_t opa = lv_map(x, 0, r, LV_OPA_TRANSP, LV_OPA_COVER);
44         lv_obj_set_style_opa(child, LV_OPA_COVER - opa, 0);
45     }
46 }
47 
48 /**
49  * Translate the object as they scroll
50  */
lv_example_scroll_6(void)51 void lv_example_scroll_6(void)
52 {
53     lv_obj_t * cont = lv_obj_create(lv_scr_act());
54     lv_obj_set_size(cont, 200, 200);
55     lv_obj_center(cont);
56     lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
57     lv_obj_add_event_cb(cont, scroll_event_cb, LV_EVENT_SCROLL, NULL);
58     lv_obj_set_style_radius(cont, LV_RADIUS_CIRCLE, 0);
59     lv_obj_set_style_clip_corner(cont, true, 0);
60     lv_obj_set_scroll_dir(cont, LV_DIR_VER);
61     lv_obj_set_scroll_snap_y(cont, LV_SCROLL_SNAP_CENTER);
62     lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);
63 
64     uint32_t i;
65     for(i = 0; i < 20; i++) {
66         lv_obj_t * btn = lv_btn_create(cont);
67         lv_obj_set_width(btn, lv_pct(100));
68 
69         lv_obj_t * label = lv_label_create(btn);
70         lv_label_set_text_fmt(label, "Button %"LV_PRIu32, i);
71     }
72 
73     /*Update the buttons position manually for first*/
74     lv_event_send(cont, LV_EVENT_SCROLL, NULL);
75 
76     /*Be sure the fist button is in the middle*/
77     lv_obj_scroll_to_view(lv_obj_get_child(cont, 0), LV_ANIM_OFF);
78 }
79 
80 #endif
81