lv_example_scroll_6.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. def scroll_event_cb(e):
  2. cont = e.get_target()
  3. cont_a = lv.area_t()
  4. cont.get_coords(cont_a)
  5. cont_y_center = cont_a.y1 + cont_a.get_height() // 2
  6. r = cont.get_height() * 7 // 10
  7. child_cnt = cont.get_child_cnt()
  8. for i in range(child_cnt):
  9. child = cont.get_child(i)
  10. child_a = lv.area_t()
  11. child.get_coords(child_a)
  12. child_y_center = child_a.y1 + child_a.get_height() // 2
  13. diff_y = child_y_center - cont_y_center
  14. diff_y = abs(diff_y)
  15. # Get the x of diff_y on a circle.
  16. # If diff_y is out of the circle use the last point of the circle (the radius)
  17. if diff_y >= r:
  18. x = r
  19. else:
  20. # Use Pythagoras theorem to get x from radius and y
  21. x_sqr = r * r - diff_y * diff_y
  22. res = lv.sqrt_res_t()
  23. lv.sqrt(x_sqr, res, 0x8000) # Use lvgl's built in sqrt root function
  24. x = r - res.i
  25. # Translate the item by the calculated X coordinate
  26. child.set_style_translate_x(x, 0)
  27. # Use some opacity with larger translations
  28. opa = lv.map(x, 0, r, lv.OPA.TRANSP, lv.OPA.COVER)
  29. child.set_style_opa(lv.OPA.COVER - opa, 0)
  30. #
  31. # Translate the object as they scroll
  32. #
  33. cont = lv.obj(lv.scr_act())
  34. cont.set_size(200, 200)
  35. cont.center()
  36. cont.set_flex_flow(lv.FLEX_FLOW.COLUMN)
  37. cont.add_event_cb(scroll_event_cb, lv.EVENT.SCROLL, None)
  38. cont.set_style_radius(lv.RADIUS.CIRCLE, 0)
  39. cont.set_style_clip_corner(True, 0)
  40. cont.set_scroll_dir(lv.DIR.VER)
  41. cont.set_scroll_snap_y(lv.SCROLL_SNAP.CENTER)
  42. cont.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF)
  43. for i in range(20):
  44. btn = lv.btn(cont)
  45. btn.set_width(lv.pct(100))
  46. label = lv.label(btn)
  47. label.set_text("Button " + str(i))
  48. # Update the buttons position manually for first*
  49. lv.event_send(cont, lv.EVENT.SCROLL, None)
  50. # Be sure the fist button is in the middle
  51. #lv.obj.scroll_to_view(cont.get_child(0), lv.ANIM.OFF)
  52. cont.get_child(0).scroll_to_view(lv.ANIM.OFF)