win_horizontal_slide.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --[[
  2. @module win_horizontal_slide
  3. @summary 横向滑动页面演示模块
  4. @version 1.0.0
  5. @date 2025.12.9
  6. @author 江访
  7. @usage
  8. 本文件为横向滑动页面演示模块,核心业务逻辑为:
  9. 1、创建窗口容器并设置白色背景;
  10. 2、启用横向滑动功能;
  11. 3、创建两页内容并水平排列;
  12. 4、实现横向滑动切换页面效果;
  13. 5、启动UI渲染循环持续刷新显示;
  14. 本文件没有对外接口;
  15. ]]
  16. local function ui_main()
  17. -- 显示触摸初始化
  18. hw_font_drv.init()
  19. -- 设置主题
  20. ui.sw_init({ theme = "light" })
  21. -- 创建窗口容器
  22. local win = ui.window({ background_color = ui.COLOR_WHITE })
  23. -- 启用横向滚动,将两页内容并排布置
  24. local page_w, page_h = lcd.getSize()
  25. local totalW = math.floor(page_w * 2)
  26. -- 创建横向滑动窗口
  27. win:enable_scroll({
  28. direction = "horizontal",
  29. content_width = totalW,
  30. threshold = 8,
  31. page_width = page_w
  32. })
  33. -- 创建网格按钮函数
  34. local function makeGrid(offset_x, label_prefix)
  35. local cols, rows = 3, 4
  36. local bw, bh = 200, 80
  37. local mx, my = math.floor((page_w - cols * bw - (cols - 1) * 20) / 2 + offset_x), 80
  38. local gapx, gapy = 20, 20
  39. local n = 1
  40. -- 添加页面标题
  41. local page_title = ui.label({
  42. x = mx,
  43. y = my - 50,
  44. text = "页面 " .. label_prefix,
  45. color = ui.COLOR_BLACK,
  46. size = 22
  47. })
  48. win:add(page_title)
  49. for r = 0, rows - 1 do
  50. for c = 0, cols - 1 do
  51. local x = mx + c * (bw + gapx)
  52. local y = my + r * (bh + gapy)
  53. local btn = ui.button({
  54. x = x,
  55. y = y,
  56. w = bw,
  57. h = bh,
  58. text = string.format("%s-按钮%d", label_prefix, n),
  59. size = 16
  60. })
  61. win:add(btn)
  62. n = n + 1
  63. end
  64. end
  65. end
  66. -- 创建总标题
  67. local title = ui.label({
  68. x = 250,
  69. y = 30,
  70. text = "横向滑动页面演示",
  71. color = ui.COLOR_BLACK,
  72. size = 24
  73. })
  74. win:add(title)
  75. -- 创建左页和右页内容
  76. makeGrid(0, "一") -- 第一页
  77. makeGrid(page_w, "二") -- 第二页
  78. -- 添加提示标签
  79. local hint = ui.label({
  80. x = 600,
  81. y = 50,
  82. text = "左右滑动切换页面",
  83. color = ui.COLOR_GRAY,
  84. size = 16
  85. })
  86. win:add(hint)
  87. -- 注册窗口到UI系统
  88. ui.add(win)
  89. end
  90. sys.taskInit(ui_main)