“””M5Image”””

M5Image 是一个可在用户界面中显示图像的控件。

UiFlow2 示例

显示图像

在 UiFlow2 中打开 cores3_show_image_example.m5f2 项目。

该示例演示如何在屏幕上显示图像。

UiFlow2 代码块:

cores3_show_image_example.png

示例输出:

None

MicroPython 示例

显示图像

该示例演示如何在屏幕上显示图像。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import m5ui
 9import lvgl as lv
10
11
12page0 = None
13image0 = None
14image1 = None
15
16
17def setup():
18    global page0, image0, image1
19
20    M5.begin()
21    Widgets.setRotation(1)
22    m5ui.init()
23    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
24    image0 = m5ui.M5Image("/flash/res/img/uiflow.jpg", x=129, y=54, parent=page0)
25    image1 = m5ui.M5Image("/flash/res/img/uiflow.png", x=129, y=118, parent=page0)
26
27    page0.screen_load()
28    image0.set_image("/flash/res/img/uiflow.jpg")
29    image1.set_image("/flash/res/img/uiflow.png")
30
31
32def loop():
33    global page0, image0, image1
34    M5.update()
35
36
37if __name__ == "__main__":
38    try:
39        setup()
40        while True:
41            loop()
42    except (Exception, KeyboardInterrupt) as e:
43        try:
44            m5ui.deinit()
45            from utility import print_error_msg
46
47            print_error_msg(e)
48        except ImportError:
49            print("please update to latest firmware")

示例输出:

None

API参考

“””M5Image”””

class m5ui.image.M5Image(*args, **kwargs)

基类:image

创建一个图像对象。

参数:
  • path (str) – 图像文件的路径。

  • x (int) – 图像的 x 坐标。

  • y (int) – 图像的 y 坐标。

  • parent (lv.obj) – 要将图像附加到的父对象;若未指定,则附加到默认屏幕。

MicroPython 代码块:

from m5ui import M5Image
import lvgl as lv

m5ui.init()
image_0 = M5Image("/flash/res/img/defalut.jpg", x=10, y=10, parent=page0)
image_1 = M5Image("/flash/res/img/uiflow.jpg", x=50, y=50, parent=page0)
set_flag(flag, value)

在对象上设置标志。若 value 为 True,则添加该标志;为 False 则移除。

参数:
  • flag (int) – 要设置的标志。

  • value (bool) – 若为 True 则添加标志;若为 False 则移除标志。

UiFlow2 代码块:

set_hidden.png

MicroPython 代码块:

image_0.set_flag(lv.obj.FLAG.HIDDEN, True)
set_pos(x, y)

设置图像的位置。

参数:
  • x (int) – 图像的 x 坐标。

  • y (int) – 图像的 y 坐标。

UiFlow2 代码块:

set_pos.png

MicroPython 代码块:

image_0.set_pos(100, 100)
set_x(x)

设置图像的 x 坐标。

参数:

x (int) – 图像的 x 坐标。

UiFlow2 代码块:

set_x.png

MicroPython 代码块:

image_0.set_x(100)
set_y(y)

设置图像的 y 坐标。

参数:

y (int) – 图像的 y 坐标。

UiFlow2 代码块:

set_y.png

MicroPython 代码块:

image_0.set_y(100)
align_to(obj, align, x, y)

将图像对齐到另一个对象。

参数:
  • obj (lv.obj) – 要对齐到的对象。

  • align (int) – 对齐类型。

  • x (int) – 相对于对齐对象的 x 偏移量。

  • y (int) – 相对于对齐对象的 y 偏移量。

UiFlow2 代码块:

align_to.png

MicroPython 代码块:

image_0.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
set_image(path)

设置要显示的图像。

参数:

path (str) – 图像文件的路径。

UiFlow2 代码块:

set_image.png

MicroPython 代码块:

image_0.set_image("/flash/res/img/uiflow.jpg")
image_1.set_image("/sd/uiflow.png")
set_rotation(rotation)

设置图像的旋转角度。

参数:

rotation (int) – 旋转角度(0、90、180、270)。

UiFlow2 代码块:

set_rotation.png

MicroPython 代码块:

image_0.set_rotation(90)
set_scale(scale, scale_y=None)

设置图像的缩放比例。

参数:
  • scale_x (float) – 水平缩放因子。

  • scale_y (float) – 图像的垂直缩放因子。

UiFlow2 代码块:

set_scale.png

MicroPython 代码块:

image_0.set_scale(2.0, 2.0)