[English]

Canvas(画布)(lv_canvas)

Overview(概述)

显示原文

A Canvas inherits from Image where the user can draw anything. Rectangles, texts, images, lines, arcs can be drawn here using lvgl's drawing engine.


Canvas(画布) 继承自 Image ,用户可以在其中绘制任何内容:矩形、文本、图像、线条、圆弧可以在这里使用lvgl的绘图引擎绘制。

Parts and Styles(部分和样式)

显示原文
  • LV_PART_MAIN Uses the typical rectangle style properties and image

style properties.


  • LV_PART_MAIN 使用典型的矩形样式属性和图像样式属性。

Usage(用法)

Buffer(缓冲区)

显示原文

The Canvas needs a buffer in which it stores the drawn image. To assign a buffer to a Canvas, use lv_canvas_set_buffer(canvas, buffer, width, height, LV_COLOR_FORMAT_...). Where buffer is a static buffer (not just a local variable) to hold the image of the canvas. For example for a 100x50 ARGB8888 buffer: static uint8_t buffer[100 * 50 * 4].

Or you can use static uint8_t buffer[LV_CANVAS_BUF_SIZE(width, height, bit_per_pixel, stride_in_bytes)].

The canvas supports all the color formats like LV_COLOR_FORMAT_ARGB8888 or LV_COLOR_FORMAT_I2. See the full list in the Color formats section.


Canvas 需要一个缓冲区来存储绘制的图像。 要为 Canvas 分配缓冲区,请使用函数 lv_canvas_set_buffer(canvas, buffer, width, height, LV_COLOR_FORMAT_...) , 其中参数 buffer 是一个静态缓冲区(不能是局部变量)来保存画布的图像。例如,对于 100x50 的 ARGB888缓冲区,可以这样分配:

  • static uint8_t buffer[100 * 50 * 4]

  • static uint8_t buffer[LV_CANVAS_BUF_SIZE(width, height, bit_per_pixel, stride_in_bytes)]

画布支持所有LVGL内置支持的颜色格式,如 LV_COLOR_FORMAT_ARGB8888LV_COLOR_FORMAT_I2 ,请阅读 Color formats 了解完整的格式列表。

Indexed colors(颜色索引)

显示原文

For LV_COLOR_FORMAT_I1/2/4/8 color formats a palette needs to be initialized with lv_canvas_set_palette(canvas, 3, lv_color_hex(0xff0000)). It sets pixels with index=3 to red.


对于 LV_COLOR_FORMAT_I1/2/4/8 颜色格式,调色板需要用 lv_canvas_set_palette(canvas, 3, lv_color_hex(0xff0000)) 初始化。 它将 index=3 的像素设置为红色

Drawing(画画)

显示原文

To set a pixel's color on the canvas, use lv_canvas_set_px_color(canvas, x, y, color, opa). With LV_COLOR_FORMAT_I1/2/4/8 the index of the color needs to be passed as color like this lv_color_from_int(13);. It passes index 13 as a color.

lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_50) fills the whole canvas to blue with 50% opacity. Note that if the current color format doesn't support colors (e.g. LV_COLOR_FORMAT_A8) the color will be ignored. Similarly, if opacity is not supported (e.g. LV_COLOR_FORMAT_RGB565) it will be ignored.

An array of pixels can be copied to the canvas with lv_canvas_copy_buf(canvas, buffer_to_copy, x, y, width, height). The color format of the buffer and the canvas need to match.

To draw something to the canvas use LVGL's draw functions directly. See the examples for more details.

The draw function can draw to any color format to which LVGL can render. Typically it means LV_COLOR_FORMAT_RGB565, LV_COLOR_FORMAT_RGB888, LV_COLOR_FORMAT_XRGB888, and LV_COLOR_FORMAT_ARGB8888.


要在画布上设置像素,请使用 lv_canvas_set_px_color(canvas, x, y, color, opa) 。 对于 LV_COLOR_FORMAT_I1/2/4/8 颜色索引需要像这样 lv_color_from_int(13); 传递,它将颜色索引 13 作为颜色传递。

lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_50) 将整个画布填充为蓝色,不透明度为 50%。 请注意,如果当前颜色格式不支持(例如 LV_COLOR_FORMAT_A8),则该颜色将被忽略。同样,如果不支持不透明度(例如 LV_COLOR_FORMAT_RGB565),不透明度将被忽略。

可以使用 lv_canvas_copy_buf(canvas, buffer_to_copy, x, y, width, height) 将像素数组复制到画布。缓冲区和画布的颜色格式需要一致。

要在画布上绘制内容,请直接使用 LVGL 的绘制函数。有关更多详细信息,请参阅后面的示例。

draw 函数可以绘制为 LVGL 可以渲染的任何颜色格式,通常是: LV_COLOR_FORMAT_RGB565LV_COLOR_FORMAT_RGB888LV_COLOR_FORMAT_XRGB888LV_COLOR_FORMAT_ARGB8888

Events(事件)

显示原文

No special events are sent by canvas objects. The same events are sent as for the

See the events of the Image (图象)(lv_image) too.

Learn more about Events(事件).


画布对象不会发送特殊事件。

也可以阅读 Image (图象)(lv_image) 的事件。

详细了解更多阅读 Events(事件)

Keys(按键)

显示原文

No Keys are processed by the object type.

Learn more about Keys(按键).


对象类型不处理 Keys

阅读了解有关 Keys(按键) 的更多信息

Example

API

lv_types.h

lv_canvas.h