Labs / LVGL / 04 Quick Start Case - Layout and Space Management (Based on Web Simulator and LVGL v9.0

4.3 Translation

Author: Li Zisheng · Source: FreakStudio open documentation, v1.0 · License: CC BY 4.0

4.3 Translation

It uses the "relative offset" based on the object's current position to solve the problem that "objects with different initial positions will offset by the same distance during interaction"(for example, a button moves upward when pressed). Here is an mpy example:


# Initialize

import display_driver
import lvgl as lv

# Create a button with a label

lv.init()
scr = lv.screen_active()

btn = lv.button(scr)
btn.align(lv.ALIGN.CENTER, 0, 0)

# 1. Create the default state style
style_normal = lv.style_t()
style_normal.init()
style_normal.set_width(100)  # Default width 100 pixels

# 2. Create the pressed state style
style_pressed = lv.style_t()
style_pressed.init()
style_pressed.set_width(120)  # Width 120 pixels when pressed
# Create the pressed state style, set y-axis translation to -20 pixels (move up 20)
style_pressed.set_translate_y(-20)

# 3. Add styles to the button
btn.add_style(style_normal, lv.STATE.DEFAULT)
btn.add_style(style_pressed, lv.STATE.PRESSED)

lv.screen_load(scr)
v1.20.0 1053 c349ae32bf 2024 09 11
4.3 Translation

In addition, there are two key features:

Version Record

Version Number

Modifier

Time

Content

v1.0

Li Zisheng

2026/03/24

Initialize the document.