lv_example_scroll_6.c 2.6 KB

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