image_page.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. --[[
  2. @module image_page
  3. @summary 图片组件演示页面
  4. @version 1.0
  5. @date 2026.01.30
  6. @author 江访
  7. @usage 本文件是图片组件的演示页面,展示图片的各种用法。
  8. 注意:需要确保图片文件存在于设备中。
  9. ]]
  10. local image_page = {}
  11. ----------------------------------------------------------------
  12. -- 页面UI元素
  13. ----------------------------------------------------------------
  14. local main_container = nil
  15. local scroll_container = nil
  16. ----------------------------------------------------------------
  17. -- 辅助函数:创建带标题的容器
  18. ----------------------------------------------------------------
  19. local function create_demo_container(parent, title, x, y, width, height)
  20. local container = airui.container({
  21. parent = parent,
  22. x = x,
  23. y = y,
  24. w = width,
  25. h = height,
  26. color = 0xFFFFFF,
  27. radius = 8,
  28. })
  29. airui.label({
  30. parent = container,
  31. text = title,
  32. x = 10,
  33. y = 5,
  34. w = width - 20,
  35. h = 25,
  36. color = 0x333333,
  37. font_size = 14,
  38. })
  39. return container
  40. end
  41. ----------------------------------------------------------------
  42. -- 辅助函数:创建控制面板
  43. ----------------------------------------------------------------
  44. local function create_control_panel(parent, title, x, y, width, height)
  45. local panel = airui.container({
  46. parent = parent,
  47. x = x,
  48. y = y,
  49. w = width,
  50. h = height,
  51. color = 0xF8F9FA,
  52. radius = 6,
  53. })
  54. airui.label({
  55. parent = panel,
  56. text = title,
  57. x = 8,
  58. y = 5,
  59. w = width - 16,
  60. h = 20,
  61. color = 0x666666,
  62. font_size = 12,
  63. })
  64. return panel
  65. end
  66. ----------------------------------------------------------------
  67. -- 创建UI
  68. ----------------------------------------------------------------
  69. function image_page.create_ui()
  70. main_container = airui.container({
  71. parent = airui.screen,
  72. x = 0,
  73. y = 0,
  74. w = 320,
  75. h = 480,
  76. color = 0xF5F5F5,
  77. })
  78. -- 标题栏
  79. local title_bar = airui.container({
  80. parent = main_container,
  81. x = 0,
  82. y = 0,
  83. w = 320,
  84. h = 50,
  85. color = 0x8BC34A,
  86. })
  87. airui.label({
  88. parent = title_bar,
  89. text = "图片组件演示",
  90. x = 10,
  91. y = 15,
  92. w = 200,
  93. h = 20,
  94. font_size = 16,
  95. color = 0xFFFFFF,
  96. })
  97. -- 返回按钮
  98. local back_btn = airui.button({
  99. parent = title_bar,
  100. x = 250,
  101. y = 10,
  102. w = 60,
  103. h = 30,
  104. text = "返回",
  105. on_click = function(self)
  106. go_back()
  107. end
  108. })
  109. -- 滚动容器
  110. scroll_container = airui.container({
  111. parent = main_container,
  112. x = 0,
  113. y = 50,
  114. w = 320,
  115. h = 430,
  116. color = 0xF5F5F5,
  117. })
  118. local current_y = 10
  119. --------------------------------------------------------------------
  120. -- 示例1: 基本图片显示
  121. --------------------------------------------------------------------
  122. local demo1_container = create_demo_container(scroll_container, "示例1: 基本图片显示", 10, current_y, 300, 180)
  123. current_y = current_y + 180 + 10
  124. airui.label({
  125. parent = demo1_container,
  126. text = "显示静态图片,支持JPG和PNG格式",
  127. x = 10,
  128. y = 35,
  129. w = 280,
  130. h = 20,
  131. color = 0x666666,
  132. font_size = 12,
  133. })
  134. local basic_image = airui.image({
  135. parent = demo1_container,
  136. x = 20,
  137. y = 60,
  138. w = 120,
  139. h = 100,
  140. src = "/luadb/test1.png",
  141. on_click = function(self)
  142. log.info("image", "基本图片被点击")
  143. local msg = airui.msgbox({
  144. text = "基本图片被点击",
  145. buttons = { "确定" },
  146. timeout = 1500,
  147. on_action = function(self, label)
  148. self:hide()
  149. end
  150. })
  151. msg:show()
  152. end
  153. })
  154. airui.label({
  155. parent = demo1_container,
  156. text = "尺寸: 120x100",
  157. x = 150,
  158. y = 70,
  159. w = 140,
  160. h = 20,
  161. color = 0x333333,
  162. font_size = 12,
  163. })
  164. airui.label({
  165. parent = demo1_container,
  166. text = "格式: PNG",
  167. x = 150,
  168. y = 95,
  169. w = 140,
  170. h = 20,
  171. color = 0x333333,
  172. font_size = 12,
  173. })
  174. airui.label({
  175. parent = demo1_container,
  176. text = "支持点击事件",
  177. x = 150,
  178. y = 120,
  179. w = 140,
  180. h = 20,
  181. color = 0x333333,
  182. font_size = 12,
  183. })
  184. --------------------------------------------------------------------
  185. -- 示例2: 图片透明度控制
  186. --------------------------------------------------------------------
  187. local demo2_container = create_demo_container(scroll_container, "示例2: 图片透明度控制", 10, current_y, 300, 200)
  188. current_y = current_y + 200 + 10
  189. local png_image = airui.image({
  190. parent = demo2_container,
  191. x = 20,
  192. y = 40,
  193. w = 100,
  194. h = 100,
  195. src = "/luadb/test2.png",
  196. zoom = 256,
  197. opacity = 255,
  198. })
  199. local opacity_panel = create_control_panel(demo2_container, "透明度控制", 130, 40, 160, 150)
  200. local opacity_label = airui.label({
  201. parent = opacity_panel,
  202. text = "当前: 255",
  203. x = 10,
  204. y = 25,
  205. w = 140,
  206. h = 20,
  207. color = 0x333333,
  208. font_size = 12,
  209. })
  210. local opacity_buttons = {
  211. { text = "100%", value = 255 },
  212. { text = "75%", value = 191 },
  213. { text = "50%", value = 127 },
  214. { text = "25%", value = 64 },
  215. }
  216. for i, btn_info in ipairs(opacity_buttons) do
  217. airui.button({
  218. parent = opacity_panel,
  219. x = 10 + ((i - 1) % 2) * 70,
  220. y = 50 + math.floor((i - 1) / 2) * 30,
  221. w = 60,
  222. h = 25,
  223. text = btn_info.text,
  224. on_click = function(self)
  225. local opacity_value = btn_info.value
  226. png_image:set_opacity(opacity_value)
  227. opacity_label:set_text("当前: " .. opacity_value)
  228. local percent = math.floor((opacity_value / 255) * 100)
  229. log.info("image", "透明度设置为: " .. percent .. "%")
  230. end
  231. })
  232. end
  233. --------------------------------------------------------------------
  234. -- 示例3: 图片缩放控制
  235. --------------------------------------------------------------------
  236. local demo3_container = create_demo_container(scroll_container, "示例3: 图片缩放控制", 10, current_y, 300, 200)
  237. current_y = current_y + 200 + 10
  238. local zoom_image = airui.image({
  239. parent = demo3_container,
  240. x = 20,
  241. y = 40,
  242. w = 120,
  243. h = 120,
  244. src = "/luadb/test1.png",
  245. zoom = 256,
  246. })
  247. local zoom_panel = create_control_panel(demo3_container, "缩放控制", 150, 40, 140, 150)
  248. local zoom_label = airui.label({
  249. parent = zoom_panel,
  250. text = "当前: 100%",
  251. x = 10,
  252. y = 25,
  253. w = 120,
  254. h = 20,
  255. color = 0x333333,
  256. font_size = 12,
  257. })
  258. local zoom_buttons = {
  259. { text = "50%", value = 128 },
  260. { text = "100%", value = 256 },
  261. { text = "150%", value = 384 },
  262. { text = "200%", value = 512 },
  263. }
  264. for i, btn_info in ipairs(zoom_buttons) do
  265. airui.button({
  266. parent = zoom_panel,
  267. x = 10 + ((i - 1) % 2) * 55,
  268. y = 50 + math.floor((i - 1) / 2) * 30,
  269. w = 50,
  270. h = 25,
  271. text = btn_info.text,
  272. on_click = function(self)
  273. local zoom_value = btn_info.value
  274. zoom_image:set_zoom(zoom_value)
  275. local percent = math.floor((zoom_value / 256) * 100)
  276. zoom_label:set_text("当前: " .. percent .. "%")
  277. log.info("image", "缩放设置为: " .. percent .. "%")
  278. end
  279. })
  280. end
  281. --------------------------------------------------------------------
  282. -- 示例4: 图片切换演示
  283. --------------------------------------------------------------------
  284. local demo4_container = create_demo_container(scroll_container, "示例4: 图片切换演示", 10, current_y, 300, 180)
  285. current_y = current_y + 180 + 10
  286. local image_index = 1
  287. local image_paths = { "/luadb/test1.png", "/luadb/test2.png" }
  288. local image_names = { "测试图片1", "测试图片2" }
  289. local switch_image = airui.image({
  290. parent = demo4_container,
  291. x = 20,
  292. y = 40,
  293. w = 120,
  294. h = 100,
  295. src = image_paths[1],
  296. })
  297. local image_info_label = airui.label({
  298. parent = demo4_container,
  299. text = "当前: " .. image_names[1],
  300. x = 150,
  301. y = 50,
  302. w = 140,
  303. h = 20,
  304. color = 0x333333,
  305. font_size = 12,
  306. })
  307. local switch_btn = airui.button({
  308. parent = demo4_container,
  309. x = 150,
  310. y = 80,
  311. w = 100,
  312. h = 35,
  313. text = "切换图片",
  314. on_click = function(self)
  315. image_index = image_index % #image_paths + 1
  316. switch_image:set_src(image_paths[image_index])
  317. image_info_label:set_text("当前: " .. image_names[image_index])
  318. log.info("image", "切换到图片: " .. image_names[image_index])
  319. local msg = airui.msgbox({
  320. text = "已切换到: " .. image_names[image_index],
  321. buttons = { "确定" },
  322. timeout = 1000,
  323. on_action = function(self, label)
  324. self:hide()
  325. end
  326. })
  327. msg:show()
  328. end
  329. })
  330. --------------------------------------------------------------------
  331. -- 示例5: 动态创建图片
  332. --------------------------------------------------------------------
  333. local demo5_container = create_demo_container(scroll_container, "示例5: 动态创建图片", 10, current_y, 300, 200)
  334. airui.label({
  335. parent = demo5_container,
  336. text = "动态创建和销毁图片",
  337. x = 10,
  338. y = 30,
  339. w = 280,
  340. h = 20,
  341. color = 0x666666,
  342. font_size = 12,
  343. })
  344. local dynamic_images = {}
  345. local img_positions = { {x = 20, y = 60}, {x = 90, y = 60}, {x = 160, y = 60} }
  346. airui.button({
  347. parent = demo5_container,
  348. x = 20,
  349. y = 130,
  350. w = 100,
  351. h = 35,
  352. text = "创建图片",
  353. on_click = function(self)
  354. local count = #dynamic_images + 1
  355. if count <= 3 then
  356. local pos = img_positions[count]
  357. local img = airui.image({
  358. parent = demo5_container,
  359. x = pos.x,
  360. y = pos.y,
  361. w = 50,
  362. h = 50,
  363. src = "/luadb/test1.png",
  364. })
  365. table.insert(dynamic_images, img)
  366. log.info("image", "创建第 " .. count .. " 张图片")
  367. end
  368. end
  369. })
  370. airui.button({
  371. parent = demo5_container,
  372. x = 170,
  373. y = 130,
  374. w = 100,
  375. h = 35,
  376. text = "清除图片",
  377. on_click = function(self)
  378. for _, img in ipairs(dynamic_images) do
  379. img:destroy()
  380. end
  381. dynamic_images = {}
  382. log.info("image", "清除所有动态图片")
  383. end
  384. })
  385. -- 底部提示
  386. airui.label({
  387. parent = main_container,
  388. text = "提示: 支持PNG透明度、图片缩放、点击事件等功能",
  389. x = 10,
  390. y = 450,
  391. w = 300,
  392. h = 20,
  393. color = 0x666666,
  394. font_size = 12,
  395. })
  396. end
  397. ----------------------------------------------------------------
  398. -- 初始化页面
  399. ----------------------------------------------------------------
  400. function image_page.init(params)
  401. image_page.create_ui()
  402. end
  403. ----------------------------------------------------------------
  404. -- 清理页面
  405. ----------------------------------------------------------------
  406. function image_page.cleanup()
  407. if main_container then
  408. main_container:destroy()
  409. main_container = nil
  410. scroll_container = nil
  411. end
  412. end
  413. return image_page