图像 (Img)

图像控件是用于显示图像的基本控件,支持移动、缩放、旋转等功能。

用法

创建控件

开发者可以使用 gui_img_create_from_mem() 从内存中创建一个图像控件,或者使用 gui_img_create_from_fs() 从文件系统中创建一个图像控件。同样,也可以使用 gui_img_create_from_ftl() 从闪存中创建一个图像控件。 如果图像控件的宽度或高度设置为 0,那么控件的大小将根据图像源的大小自动设置。

设置焦点

图片控件的默认焦点位置位于图片的左上角 (0, 0),开发者可以使用 gui_img_set_focus() 设置新的图片控件的焦点,设置完成之后,图片控件的变换、旋转、平移操作都会基于新的焦点进行。

设定位置

开发者可以通过 gui_img_set_pos() 来设置图像控件的新坐标。

设定图片源

通过 gui_img_set_src() 来设置图像控件的新图像源。

获取高度/宽度

如果想要获取图像控件的高度/宽度,开发者可以使用 gui_img_get_height()gui_img_get_width()

刷新大小

开发者可以调用 gui_img_refresh_size() 来刷新图像控件大小。

混合模式

开发者可以使用 gui_img_set_mode() 来设定图像控件的混合模式。

移动

通过 gui_img_translate() 来移动图像控件。 开发者可以将图像控件移动到新坐标,而不改变控件属性中的原始坐标。

旋转

开发者可以通过 gui_img_rotation() 来围绕圆心旋转图像控件。

缩放

开发者可以使用 gui_img_scale() 调整图像控件的大小以满足需求。

不透明度

图像控件的不透明度值是可调整的,开发者可以调用 gui_img_set_opacity() 来调整。

质量

开发者可以调用 gui_img_set_quality() 来设定图像控件的显示质量。

截屏

开发者可以使用 gui_img_tree_convert_to_img() 来保存全屏截图。保存的图像会是 RGB 格式。

示例

void img_kb_cb(void *obj, gui_event_t *e)
{
    gui_obj_t *this = (gui_obj_t *)obj;
    const char *dev_name = e->indev_name ? (const char *)e->indev_name : "unknown";
    gui_log("Event test obj name = %s, e = 0x%x, indev = %s !\n", this->name, e->code, dev_name);
}

void img_tp_clicked_cb(void *obj, gui_event_t *e)
{
    GUI_UNUSED(obj);
    GUI_UNUSED(e);
    gui_log("Single clicked!\n");
}

void img_tp_double_clicked_cb(void *obj, gui_event_t *e)
{
    GUI_UNUSED(obj);
    GUI_UNUSED(e);
    gui_log("Double clicked!\n");
}

void img_tp_triple_clicked_cb(void *obj, gui_event_t *e)
{
    GUI_UNUSED(obj);
    GUI_UNUSED(e);
    gui_log("Triple clicked!\n");
}

void img_tp_long_pressed_cb(void *obj, gui_event_t *e)
{
    GUI_UNUSED(obj);
    GUI_UNUSED(e);
    gui_log("Touch long!\n");
}

void img_timer_cb(void *param)
{
    static float angle = 0;
    gui_img_t *img = (gui_img_t *)param;

    gui_img_rotation(img, angle++);

    gui_log("Handler[img] timer cb!\n");
}


static int app_init(void)
{
    void *addr = (void *)_actiger_blue;
    gui_dispdev_t *dc = gui_get_dc();

    gui_dirty_border_enable(true);

    // gui_img_t *img = gui_img_create_from_fs(gui_obj_get_root(),  "img_1_test", "/pc/example/application/screen_410_502/root_image/root/UI/cellular_menu_card.bin", 0, 0, 0, 0);
    gui_img_t *img = gui_img_create_from_mem(gui_obj_get_root(),  "img_test", addr, 0, 0, 0, 0);

    gui_img_set_focus(img, gui_img_get_width(img) / 2, gui_img_get_height(img) / 2);
    gui_img_rotation(img, 0.0f);
    gui_img_translate(img, dc->screen_width / 2, dc->screen_height / 2);

    gui_obj_add_event_cb(img, (gui_event_cb_t)img_tp_clicked_cb, GUI_EVENT_TOUCH_CLICKED, NULL);
    gui_obj_add_event_cb(img, (gui_event_cb_t)img_tp_double_clicked_cb, GUI_EVENT_TOUCH_DOUBLE_CLICKED,
                         NULL);
    gui_obj_add_event_cb(img, (gui_event_cb_t)img_tp_triple_clicked_cb, GUI_EVENT_TOUCH_TRIPLE_CLICKED,
                         NULL);
    gui_obj_add_event_cb(img, (gui_event_cb_t)img_tp_long_pressed_cb, GUI_EVENT_TOUCH_LONG, NULL);

    gui_obj_add_event_cb(img, (gui_event_cb_t)img_kb_cb, GUI_EVENT_KB_SHORT_PRESSED, NULL);
    gui_obj_focus_set(img);


    gui_obj_create_timer(&(img->base), 100, true, img_timer_cb);
    gui_obj_start_timer(&(img->base));


    return 0;
}

GUI_INIT_APP_EXPORT(app_init);


API

Functions

uint16_t gui_img_get_width(gui_img_t *_this)

Load the image to read its width.

参数:

_this -- Image widget pointer.

返回:

Image's width.

uint16_t gui_img_get_height(gui_img_t *_this)

Load the image to read its height.

参数:

_this -- Image widget pointer.

返回:

Image's height.

void gui_img_refresh_size(gui_img_t *_this)

Refresh the image size from source.

参数:

_this -- Image widget pointer.

void gui_img_refresh_draw_data(gui_img_t *_this)

Refresh the image draw data.

参数:

_this -- Image widget pointer.

void gui_img_set_mode(gui_img_t *_this, BLEND_MODE_TYPE mode)

Set the image's blend mode.

参数:
  • _this -- Image widget pointer.

  • mode -- Enumeration value of the mode (BLEND_MODE_TYPE).

void gui_img_set_attribute(gui_img_t *_this, const char *name, void *addr, int16_t x, int16_t y)

Deprecated! Use 'void gui_img_set_src(gui_img_t *_this, const uint8_t *file_pointer, uint32_t storage_type)' instead. Use 'void gui_img_set_pos(gui_img_t *_this, int16_t x, int16_t y)' instead.

void gui_img_set_pos(gui_img_t *_this, int16_t x, int16_t y)

Set image position.

参数:
  • this -- Image widget pointer.

  • x -- X-axis coordinate.

  • y -- Y-axis coordinate.

void gui_img_rotation(gui_img_t *_this, float degrees)

Rotate the image around its center.

参数:
  • _this -- Image widget pointer.

  • degrees -- Clockwise rotation absolute angle.

void gui_img_scale(gui_img_t *_this, float scale_x, float scale_y)

Scale the image, taking (0,0) as the zoom center.

参数:
  • _this -- Image widget pointer.

  • scale_x -- Scale factor in X direction.

  • scale_y -- Scale factor in Y direction.

void gui_img_translate(gui_img_t *_this, float t_x, float t_y)

Translate (move) the image.

参数:
  • _this -- Image widget pointer.

  • t_x -- New X-axis coordinate.

  • t_y -- New Y-axis coordinate.

void gui_img_skew_x(gui_img_t *_this, float degrees)

Skew the image on X-axis.

参数:
  • _this -- Image widget pointer.

  • degrees -- Skew angle.

void gui_img_skew_y(gui_img_t *_this, float degrees)

Skew the image on Y-axis.

参数:
  • _this -- Image widget pointer.

  • degrees -- Skew angle.

void gui_img_set_opacity(gui_img_t *_this, unsigned char opacity_value)

Set the opacity of the image.

参数:
  • _this -- Image widget pointer.

  • opacity_value -- Opacity value (0-255, default 255).

void gui_img_set_focus(gui_img_t *_this, float c_x, float c_y)

Set the focus point for image transformations.

参数:
  • _this -- Image widget pointer.

  • c_x -- Center X coordinate.

  • c_y -- Center Y coordinate.

gui_img_t *gui_img_create_from_mem(void *parent, const char *name, void *addr, int16_t x, int16_t y, int16_t w, int16_t h)

Create an image widget from memory address.

备注

Create an image widget and set attribute.

参数:
  • parent -- Father widget it nested in.

  • name -- Widget name.

  • addr -- Bin file address.

  • x -- X-axis coordinate of the widget.

  • y -- Y-axis coordinate of the widget.

  • w -- Width of the widget.

  • h -- Height of the widget.

返回:

Widget object pointer.

gui_img_t *gui_img_create_from_ftl(void *parent, const char *name, void *ftl, int16_t x, int16_t y, int16_t w, int16_t h)

Create an image widget from memory address.

备注

Create an image widget and set attribute.

参数:
  • parent -- Father widget it nested in.

  • name -- Widget name.

  • ftl -- Not xip address, use ftl address.

  • x -- X-axis coordinate of the widget.

  • y -- Y-axis coordinate of the widget.

  • w -- Width of the widget.

  • h -- Height of the widget.

返回:

Widget object pointer.

gui_img_t *gui_img_create_from_fs(void *parent, const char *name, void *file, int16_t x, int16_t y, int16_t w, int16_t h)

Create an image widget from filesystem.

参数:
  • parent -- Father widget it nested in.

  • name -- Image widget name.

  • file -- Image file path.

  • x -- X-axis coordinate of the widget.

  • y -- Y-axis coordinate of the widget.

  • w -- Width of the widget.

  • h -- Height of the widget.

返回:

Pointer to the created image widget.

void gui_img_set_quality(gui_img_t *_this, bool high_quality)

Set the image's quality.

参数:
  • _this -- Image widget pointer.

  • high_quality -- True for high quality rendering, false otherwise.

void gui_img_tree_convert_to_img(gui_obj_t *obj, gui_matrix_t *matrix, uint8_t *shot_buf)

Convert a tree to an image data.

参数:
  • obj -- Tree root.

  • matrix -- Null if no need to transform.

  • shot_buf -- Buffer for the screenshot.

float gui_img_get_scale_x(gui_img_t *_this)

Get the scale factor in X direction.

参数:

_this -- Image widget pointer.

返回:

Scale factor in X direction.

float gui_img_get_scale_y(gui_img_t *_this)

Get the scale factor in Y direction.

参数:

_this -- Image widget pointer.

返回:

Scale factor in Y direction.

float gui_img_get_degrees(gui_img_t *_this)

Get the rotation angle in degrees.

参数:

_this -- Image widget pointer.

返回:

Rotation angle in degrees.

float gui_img_get_c_x(gui_img_t *_this)

Get the center X coordinate for transformations.

参数:

_this -- Image widget pointer.

返回:

Center X coordinate.

float gui_img_get_c_y(gui_img_t *_this)

Get the center Y coordinate for transformations.

参数:

_this -- Image widget pointer.

返回:

Center Y coordinate.

float gui_img_get_t_x(gui_img_t *_this)

Get the translation in X direction.

参数:

_this -- Image widget pointer.

返回:

Translation in X direction.

float gui_img_get_t_y(gui_img_t *_this)

Get the translation in Y direction.

参数:

_this -- Image widget pointer.

返回:

Translation in Y direction.

void gui_img_set_image_data(gui_img_t *_this, const uint8_t *image_data_pointer)

Deprecated! Use 'void gui_img_set_src(gui_img_t *_this, const uint8_t *file_pointer, uint32_t storage_type)' instead.

void gui_img_set_src(gui_img_t *_this, const uint8_t *file_pointer, uint32_t storage_type)

Sets the image source for a specified image widget.

This function updates the image source and storage type for the widget. For filesystem sources (IMG_SRC_FILESYS), the path string is automatically duplicated. The old source is properly cleaned up if it was previously allocated.

参数:
  • _this -- Pointer to the image widget.

  • file_pointer -- Pointer to the image data or file path string.

  • storage_type -- Storage type: IMG_SRC_MEMADDR, IMG_SRC_FTL, or IMG_SRC_FILESYS.

const uint8_t *gui_img_get_image_data(gui_img_t *_this)

Gets the image data from a specified image widget.

This function returns the current image data that is set in the specified image widget.

参数:

_this -- Pointer to the image widget (gui_img_t) from which the image data should be retrieved.

返回:

Pointer to the image data currently set in the widget. If no image data is set, the result may be NULL.

void gui_img_a8_recolor(gui_img_t *_this, uint32_t fg_color_mix)

Sets the foreground color mixing value for the image widget.

void gui_img_a8_fix_bg(gui_img_t *_this, uint32_t bg_color_mix)

Sets the background color mixing value for the image widget.

void gui_img_a8_mix_alpha(gui_img_t *_this, uint32_t alpha_mix)

Sets the alpha mixing value for the A8 image widget.

struct gui_img_t

Image widget structure.

Public Members

gui_obj_t base
draw_img_t *draw_img
float degrees
float scale_x
float scale_y
float f_x
float f_y
float t_x
float t_y
void *src_data
uint32_t fg_color_set
uint32_t bg_color_fix
uint32_t opacity_value
uint32_t blend_mode
uint32_t storage_type
uint32_t high_quality
uint32_t need_clip
uint32_t free_on_destroy
uint8_t checksum
uint8_t animate_array_length