1import gc
2import lvgl as lv
3from imagetools import get_png_info, open_png
4
5# Register PNG image decoder
6decoder = lv.img.decoder_create()
7decoder.info_cb = get_png_info
8decoder.open_cb = open_png
9
10# Measure memory usage
11gc.enable()
12gc.collect()
13mem_free = gc.mem_free()
14
15label = lv.label(lv.scr_act())
16label.align(lv.ALIGN.BOTTOM_MID, 0, -10)
17label.set_text(" memory free:" + str(mem_free/1024) + " kB")
18
19# Create an image from the png file
20try:
21    with open('../../assets/img_star.png','rb') as f:
22        png_data = f.read()
23except:
24    print("Could not find star.png")
25    sys.exit()
26
27img_star = lv.img_dsc_t({
28  'data_size': len(png_data),
29  'data': png_data
30})
31
32def event_cb(e, snapshot_obj):
33    img = e.get_target()
34
35    if snapshot_obj:
36        # no need to free the old source for snapshot_obj, gc will free it for us.
37
38        # take a new snapshot, overwrite the old one
39        dsc = lv.snapshot_take(img.get_parent(), lv.img.CF.TRUE_COLOR_ALPHA)
40        snapshot_obj.set_src(dsc)
41
42    gc.collect()
43    mem_used = mem_free - gc.mem_free()
44    label.set_text("memory used:" + str(mem_used/1024) + " kB")
45
46root = lv.scr_act()
47root.set_style_bg_color(lv.palette_main(lv.PALETTE.LIGHT_BLUE), 0)
48
49# Create an image object to show snapshot
50snapshot_obj = lv.img(root)
51snapshot_obj.set_style_bg_color(lv.palette_main(lv.PALETTE.PURPLE), 0)
52snapshot_obj.set_style_bg_opa(lv.OPA.COVER, 0)
53snapshot_obj.set_zoom(128)
54
55# Create the container and its children
56container = lv.obj(root)
57container.align(lv.ALIGN.CENTER, 0, 0)
58container.set_size(180, 180)
59container.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
60container.set_flex_align(lv.FLEX_ALIGN.SPACE_EVENLY, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
61container.set_style_radius(50, 0)
62
63for i in range(4):
64    img = lv.img(container)
65    img.set_src(img_star)
66    img.set_style_bg_color(lv.palette_main(lv.PALETTE.GREY), 0)
67    img.set_style_bg_opa(lv.OPA.COVER, 0)
68    img.set_style_transform_zoom(400, lv.STATE.PRESSED)
69    img.add_flag(img.FLAG.CLICKABLE)
70    img.add_event_cb(lambda e: event_cb(e, snapshot_obj), lv.EVENT.PRESSED, None)
71    img.add_event_cb(lambda e: event_cb(e, snapshot_obj), lv.EVENT.RELEASED, None)
72