Welcome to K230 Vision Module repository
06.BaseApp Base Class Analysis
Section 6: Complete Analysis of BaseApp Base Class
1. Course Objectives
Before developing an App, we must understand the "father" of all Apps—BaseApp. It helps us handle most of the repetitive work. In this lesson, we will study the implementation of base_app.py in detail.
2. Core Source Code Analysis
File Path: src/canmv/port/builtin_py/ybMain/base_app.py
2.1 Initialization (__init__)
class BaseApp: def __init__(self, app_manager, name, icon=None): self.app_manager = app_manager # Save appmanager reference for convenient return to desktop later self.name = name # App name self.icon = icon # App icon object self.screen = None # App's main screen (created later) # Get screen resolution self.disp = lv.disp_get_default() self.disp_w = self.disp.get_hor_res() self.disp_h = self.disp.get_ver_res()Note: In the __init__ phase, we usually don't create UI. Because the system initializes all Apps at once during startup, if each App creates hundreds of controls here, memory might instantly explode (possibly).
2.2 Creating Desktop Icon (create_icon)
This method is called by AppManager.
xxxxxxxxxx def create_icon(self, parent): self.icon_btn = lv.btn(parent) # Create a button # Set icon text icon_label = lv.label(self.icon_btn) icon_label.set_text(self.name) # Bind click event self.icon_btn.add_event(self.on_icon_clicked, lv.EVENT.CLICKED, None) return self.icon_btn2.3 Launch and Animation (launch_with_animation)
This is the most exciting part of BaseApp. When you click the icon, it doesn't jump abruptly, but has an animation that zooms from the icon position to full screen.
xxxxxxxxxx def launch_with_animation(self): # 1. Create new screen (this is when the App's UI container is actually created) self.screen = lv.obj() # 2. Get current position of icon icon_coords = lv.area_t() self.icon_btn.get_coords(icon_coords) # 3. Set initial state: size equals icon, position equals icon self.screen.set_size(icon_w, icon_h) self.screen.set_pos(icon_x, icon_y) self.screen.set_style_radius(start_radius, 0) # Circular # 4. Create animation anim = lv.anim_t() # ... Set animation to zoom from icon_size to display_size ... # 5. After animation ends, call initialize() # This means subclasses must implement the initialize() method3. What to Pay Attention to When Developing Apps?
After inheriting BaseApp, you only need to care about two things:
- Prepare Data: Prepare the name and icon in
__init__. - Build Interface: Override the
initialize(self)method and draw your application interface onself.screen.