立即注册 找回密码

微雪课堂

搜索

树莓派littlevGL系列教程:基本对象(lv_obj)的基本属性

2019-11-11 23:15| 发布者: imliubo| 查看: 8687| 评论: 0|原作者: IAMLIUBO

摘要: Hi,大家好,欢迎来到树莓派之littlevGL课堂,我们在上节课中学习了如何搭建开发环境,相信聪明的你一定搭建好了,肯定急于学习新的知识,不过不要着急,本节教程教大家去了解一下基本的知识---对象的基本属性,理解 ...
Hi,大家好,欢迎来到树莓派之littlevGL课堂,我们在上节课中学习了如何搭建开发环境,相信聪明的你一定搭建好了,肯定急于学习新的知识,不过不要着急,本节教程教大家去了解一下基本的知识---对象的基本属性,理解好了本节课程,后面的课程才能理解的更加深入,废话不多说!
一 对象的基本属性
littlevGL 6.0 目前共有三十多种对象类型,本节教程我们来学习littlevGL最基本的一种对象类型--基本对象(lv_obj),这是三十多种对象类型中最基本的对象类型,其他的对象类型都是基于基本对象派生出来的,那么我们就来先来学习一下基本对象吧。
想知道基本对象有哪些属性,我们可以通过它的结构体去了解一下:
typedef struct _lv_obj_t
{
    struct _lv_obj_t * par; /**< Pointer to the parent object*/
    lv_ll_t child_ll;       /**< Linked list to store the children objects*/

    lv_area_t coords; /**< Coordinates of the object (x1, y1, x2, y2)*/

    lv_event_cb_t event_cb; /**< Event callback function */
    lv_signal_cb_t signal_cb; /**< Object type specific signal function*/
    lv_design_cb_t design_cb; /**< Object type specific design function*/

    void * ext_attr;            /**< Object type specific extended data*/
    const lv_style_t * style_p; /**< Pointer to the object's style*/

#if LV_USE_GROUP != 0
    void * group_p; /**< Pointer to the group of the object*/
#endif

#if LV_USE_EXT_CLICK_AREA == LV_EXT_CLICK_AREA_TINY
    uint8_t ext_click_pad_hor; /**< Extra click padding in horizontal direction */
    uint8_t ext_click_pad_ver; /**< Extra click padding in vertical direction */
#endif

#if LV_USE_EXT_CLICK_AREA == LV_EXT_CLICK_AREA_FULL
    lv_area_t ext_click_pad;   /**< Extra click padding area. */
#endif

    /*Attributes and states*/
    uint8_t click : 1;          /**< 1: Can be pressed by an input device*/
    uint8_t drag : 1;           /**< 1: Enable the dragging*/
    uint8_t drag_throw : 1;     /**< 1: Enable throwing with drag*/
    uint8_t drag_parent : 1;    /**< 1: Parent will be dragged instead*/
    uint8_t hidden : 1;         /**< 1: Object is hidden*/
    uint8_t top : 1;            /**< 1: If the object or its children is clicked it goes to the foreground*/
    uint8_t opa_scale_en : 1;   /**< 1: opa_scale is set*/
    uint8_t parent_event : 1;   /**< 1: Send the object's events to the parent too. */
    lv_drag_dir_t drag_dir : 2; /**<  Which directions the object can be dragged in */
    uint8_t reserved : 6;       /**<  Reserved for future use*/
    uint8_t protect;            /**< Automatically happening actions can be prevented. 'OR'ed values from
                                   `lv_protect_t`*/
    lv_opa_t opa_scale;         /**< Scale down the opacity by this factor. Effects all children as well*/

    lv_coord_t ext_draw_pad; /**< EXTtend the size in every direction for drawing. */

#if LV_USE_OBJ_REALIGN
    lv_reailgn_t realign;       /**< Information about the last call to ::lv_obj_align. */
#endif

#if LV_USE_USER_DATA
    lv_obj_user_data_t user_data; /**< Custom user data for object. */
#endif

} lv_obj_t;
我们可以看到这个结构体代码还是有一点多的,不过不要紧,本节教程我们只需要了解一个大概就可以,刚开始学习没必要研究的太深入,如果太关注底层的实现有时候反而会适得其反,如果你后面熟练的掌握了littlevGL的所有控件后,可以反过头来详细的去看一下具体的实现方式,以及作者的编程思维,当然这些都是后话。
二 对象的基本属性介绍
我们通过代码来看一下,其实注释也已经非常清晰的指出有哪些基本的属性了:
    struct _lv_obj_t * par; /**< Pointer to the parent object*/
    lv_ll_t child_ll;       /**< Linked list to store the children objects*/

    lv_area_t coords; /**< Coordinates of the object (x1, y1, x2, y2)*/

    lv_event_cb_t event_cb; /**< Event callback function */
    lv_signal_cb_t signal_cb; /**< Object type specific signal function*/
    lv_design_cb_t design_cb; /**< Object type specific design function*/

    void * ext_attr;            /**< Object type specific extended data*/
    const lv_style_t * style_p; /**< Pointer to the object's style*/
我们本节教程先只看上面的这些属性。
1.父对象(struct _lv_obj_t * par):
首先它有一个父对象,那么父对象是什么呢?我们可以简单理解为当前对象是在哪一个窗口之内生成的,或者是基于哪一个对象派生的。
2.子对象(lv_ll_t child_ll):
然后它还有一个指向子对象的链表,其他对象可以是此对象的子对象,依次类推,当当前对象销毁时,会通过此链表依次销毁子对象、子对象的子对象等等。
3.坐标(lv_area_t coords):
然后就是坐标属性,包括在屏幕中的位置以及用来约束对象大小的高度和宽度。
对象的大小可以通过 lv_obj_set_width(obj, new_width) 和lv_obj_set_height(obj, new_height) 或单一函数 lv_obj_set_size(obj, new_width, new_height)来修改。
对象的坐标可以通过lv_obj_set_x(obj, new_x) 和 lv_obj_set_y(obj, new_y) 或单一函数lv_obj_set_pos(obj, new_x, new_y)来设置。
对象的位置我们还可以通过去其他对象的对齐方式去设定,这里给出一张图可以帮助大家更好的理解各种对齐方式:

4.事件(lv_event_cb_t event_cb):
我们可以给对象设置一个事件回调,比如点击事件,拖拽事件,然后触发我们想要的结果。
我们可以通过此方式设置对象的事件回调:
lv_obj_set_event_cb(obj, event_cb)
我们只需要在回调函数中去完成我们的逻辑处理即可。具体都有哪些事件我们会在用到的时候再做详细说明,或者可以去查看一下lv_event_t都有哪些枚举类型。
5.信号(lv_signal_cb_t signal_cb):
其实信号相对来说与用户是不相关的,因为这一属性仅在库内部使用,所以这里我们也不再详细去讲解了。
6.设计(lv_design_cb_t design_cb):
这个属性目前基本用不到,此属性主要用于绘制对象或者遮罩图像。
7.扩展属性(void * ext_attr):
我们可以通过此属性来给对象扩展一些属性,当我们觉得库中某些对象的功能不足,或者我们需要新增某些特殊的属性,我们都是可以设置的,就比如按钮对象相比于基本对象的不同,就是扩展的一些属性,可以看出littlevGL的对象属性是非常灵活。

cpp代码:

/*Allocate the extended data*/
lv_btn_ext_t * ext = lv_obj_allocate_ext_attr(new_btn, sizeof(lv_btn_ext_t));
8.样式(const lv_style_t * style_p):
样式属性决定了我们创建的对象的外观形象,但是样式又相对来说是比较麻烦的,因为我们描绘一个对象的外观的维度是非常多的,比如我们可以设置对象的背景,文本样式,填充样式,线条样式等等,不过我们刚开始学习自带的控件样式就足够我们使用了,所以不需要担心这一点。

这就是基本对象的一些基本属性,当然还一部分没有讲到,当前我们只需要了解这么多就足够了,了解了这些基本属性可以帮助我们更好的去理解某些控件的属性,本节就到这里,下一节我们学习如何创建一个基本对象。


243

顶一下

刚表态过的朋友 (243 人)

相关阅读

最新评论

Arduino
基础入门
OpenCV
littleGL

微雪官网|产品资料|手机版|小黑屋|微雪课堂. ( 粤ICP备05067009号 )

GMT+8, 2024-3-29 14:07 , Processed in 0.044231 second(s), 18 queries .

返回顶部