LVGL自定义滑动

devtools/2024/10/18 18:21:14/

请添加图片描述

触摸和编码器都可以操作


typedef struct
{lv_obj_t* obj;int16_t x;int16_t y;int16_t width;int16_t height;
}pos_and_size_t;typedef struct
{lv_obj_t* obj;lv_coord_t height;lv_coord_t width;lv_coord_t width_pad;lv_coord_t height_pad;lv_coord_t child_widget;lv_coord_t child_height;
}widget_attr_t;#define NUMBER_OF_MENUS    10static pos_and_size_t ps[NUMBER_OF_MENUS+4];
static widget_attr_t widget =
{.height = 260,.height_pad = 10,.width = 360,.width_pad = 10,
};void widget_refresh(void)
{for (int i = 0; i < sizeof(ps) / sizeof(ps[0]); i++){lv_obj_set_size(ps[i].obj, ps[i].width, ps[i].height);lv_obj_set_pos(ps[i].obj, ps[i].x, ps[i].y);}
}/*** @brief 根据x坐标,计算y坐标和宽度
*/
void widget_get_y_w(int x, int* y, int* w)
{float k1 = (2 * widget.height_pad + 3 * widget.child_height) * 1.0f / (2 * widget.width_pad + 3 * widget.child_widget);float b1 = widget.height_pad - k1 * widget.width_pad;float k2 = (3*widget.child_widget) * 1.0f / (2 * widget.width_pad + 3 * widget.child_widget);float b2 = widget.child_widget - k2 * widget.width_pad;float k3 = (2 * widget.height_pad + 3*widget.child_height) * -1.0f / (2 * widget.width_pad + 6 * widget.child_widget);float b3 = widget.height_pad - k3 * (5*widget.width_pad+9*widget.child_widget);float k4 = (3 * widget.child_widget) * -1.0f / (2 * widget.width_pad + 6 * widget.child_widget);float b4 = widget.child_widget - k4 * (5*widget.width_pad + 9*widget.child_widget);if (x < widget.width_pad){*y = widget.height_pad;*w = widget.child_widget;}else if (x < 3 * widget.width_pad + 3 * widget.child_widget){*y = k1 * x + b1;*w = k2 * x + b2;}else if (x < 5 * widget.width_pad + 9 * widget.child_widget){*y = k3 * x + b3;*w = k4 * x + b4;}else{*y = widget.height_pad;*w = widget.child_widget;}
}void widget_update(widget_attr_t* widget)
{int start_x = lv_obj_get_scroll_x(widget->obj);for (int i = 1; i < sizeof(ps) / sizeof(ps[0]); i++){int x = ps[i - 1].x + ps[i - 1].width + widget->width_pad;int diff = (x - start_x);ps[i].x = x ;widget_get_y_w(diff, &ps[i].y, &ps[i].width);ps[i].height = ps[i].width;}int index = 1;for (int i = 1; i < sizeof(ps) / sizeof(ps[0]); i++){if (ps[i].width > ps[index].width){index = i;}lv_obj_clear_state(ps[i].obj, LV_STATE_FOCUS_KEY);}lv_obj_add_state(ps[index].obj, LV_STATE_FOCUS_KEY);widget_refresh();
}/*** @brief 
*/
void widget_end(widget_attr_t* widget)
{static int start_x_old = 0;int start_x = lv_obj_get_scroll_x(widget->obj);if (abs(start_x_old - start_x) < 5){return;}start_x_old = start_x;int index = 1;for (int i = 1; i < sizeof(ps) / sizeof(ps[0]); i++){if (ps[i].width > ps[index].width){index = i;}lv_obj_clear_state(ps[i].obj, LV_STATE_FOCUS_KEY);}lv_obj_scroll_to_x(widget->obj, (index-2)*(widget->width_pad + widget->child_widget), LV_ANIM_OFF);lv_group_focus_obj(ps[index].obj);lv_obj_add_state(ps[index].obj, LV_STATE_FOCUS_KEY);
}lv_timer_t* s_timer = NULL;
static void timer_cb(lv_timer_t* t)
{lv_timer_pause(s_timer);widget_end(&widget);
}static widget_cb(lv_event_t* e)
{lv_event_code_t code = lv_event_get_code(e);if (LV_EVENT_SCROLL == code){lv_timer_pause(s_timer);widget_update(&widget);}else if(code == LV_EVENT_SCROLL_END){//结束滑动时,如果没有控件在最中间位置,将最近的控件滑动到最中间lv_timer_reset(s_timer);lv_timer_resume(s_timer);}}static btn_cb(lv_event_t* e)
{lv_event_code_t code = lv_event_get_code(e);lv_obj_t* obj = lv_event_get_target(e);if (LV_EVENT_FOCUSED == code){//焦点移动时,滑动控件,始终保持最中间控件聚焦int index = lv_event_get_user_data(e);lv_obj_scroll_to_x(lv_obj_get_parent(obj), (index - 2) * (widget.width_pad + widget.child_widget), LV_ANIM_OFF);}
}
void widget_init(void)
{widget.child_widget = (widget.width - 6 * widget.width_pad) / 10;widget.child_height = (widget.height - 2 * widget.height_pad) / 8;widget.obj = lv_obj_create(lv_scr_act());lv_obj_set_size(widget.obj, widget.width, widget.height);//lv_obj_set_scrollbar_mode(widget.obj, LV_SCROLLBAR_MODE_OFF);lv_obj_set_style_bg_opa(widget.obj, LV_OPA_0, LV_STATE_DEFAULT);lv_obj_set_style_pad_all(widget.obj, 0, LV_STATE_DEFAULT);//lv_obj_set_scroll_snap_x(obj, LV_SCROLL_SNAP_CENTER);lv_obj_clear_flag(widget.obj, LV_OBJ_FLAG_SCROLL_ELASTIC);s_timer = lv_timer_create(timer_cb, 100, 0);lv_timer_pause(s_timer);//创建初始屏幕,显示5个控件,宽度 1, 2, 4, 2, 1,for (int i = 0; i < sizeof(ps) / sizeof(ps[0]); i++){ps[i].obj = lv_btn_create(widget.obj);lv_obj_t* lab = lv_label_create(ps[i].obj);lv_label_set_text_fmt(lab, "%d", i);lv_obj_align(lab, LV_ALIGN_CENTER, 0, 0);lv_obj_set_scrollbar_mode(ps[i].obj, LV_SCROLLBAR_MODE_OFF);lv_obj_add_event_cb(ps[i].obj, btn_cb, LV_EVENT_ALL, i);ps[i].width = widget.child_widget;ps[i].height = widget.child_height;}ps[1].width = widget.child_widget*2;ps[1].height = widget.child_height*2;ps[2].width = widget.child_widget*4;ps[2].height = widget.child_height*4;ps[3].width = widget.child_widget*2;ps[3].height = widget.child_height*2;ps[0].x = widget.width_pad;ps[0].y = widget.height_pad;for (int i = 1; i < sizeof(ps) / sizeof(ps[0]); i++){ps[i].y = widget.height_pad;ps[i].x = widget.width_pad + ps[i - 1].x + ps[i - 1].width;}ps[1].y = widget.height_pad*2 + widget.child_height;ps[2].y = widget.height_pad*3 + widget.child_height*3;ps[3].y = widget.height_pad*2 + widget.child_height;//隐藏开头两个lv_obj_add_flag(ps[0].obj, LV_OBJ_FLAG_HIDDEN);lv_obj_add_flag(ps[1].obj, LV_OBJ_FLAG_HIDDEN);//隐藏最后两个lv_obj_add_flag(lv_obj_get_child(ps[NUMBER_OF_MENUS + 2].obj, -1), LV_OBJ_FLAG_HIDDEN);lv_obj_add_flag(lv_obj_get_child(ps[NUMBER_OF_MENUS + 3].obj, -1), LV_OBJ_FLAG_HIDDEN);lv_obj_remove_style_all(ps[NUMBER_OF_MENUS + 2].obj);lv_obj_remove_style_all(ps[NUMBER_OF_MENUS + 3].obj);lv_obj_set_style_border_width(ps[NUMBER_OF_MENUS + 2].obj, 0, LV_STATE_DEFAULT);lv_obj_set_style_border_width(ps[NUMBER_OF_MENUS + 3].obj, 0, LV_STATE_DEFAULT);lv_obj_set_style_bg_opa(ps[NUMBER_OF_MENUS + 2].obj, LV_OPA_0, LV_STATE_DEFAULT);lv_obj_set_style_bg_opa(ps[NUMBER_OF_MENUS + 3].obj, LV_OPA_0, LV_STATE_DEFAULT);lv_obj_add_event_cb(widget.obj, widget_cb, LV_EVENT_ALL, NULL);widget_refresh();
}

http://www.ppmy.cn/devtools/25462.html

相关文章

windows下安装onlyoffice

文章目录 1、 安装ErLang2、 安装rabbitmq3、 安装postgresql4、 安装onlyoffice(社区版) 1、 安装ErLang 下载地址&#xff1a;https://erlang.org/download/otp_win64_24.2.exe opt_wind64_24.2.exe 直接运行&#xff0c;一步一步安装 2、 安装rabbitmq 下载地址&#xf…

Linux--MyMiniTry--Vim

首先下载好vim,我们可以按以下的方式进行光标的移动&#xff08;也可以回车进行换行&#xff09; &#xff08;--> 进入教程&#xff09; &#xff08;初始的时候没有文本&#xff0c;你怎么按都没有用&#xff09; &#xff08;我们要先按 i &#xff0c;进行插入文本才…

Mybatis.net + Mysql

项目文件结构 NuGet下载Mybatis.net相关包&#xff1a;IBatisNet 安装完成后&#xff0c;会显示在&#xff0c;在已安装页面。同时&#xff0c;在管理器中的引用列表中&#xff0c;会多出来两个引用文件 IBatisNet.CommonIBatisNet.DataMapper 安装 Mysql.data。 注意&#xff…

Aiseesoft Data Recovery for Mac:专业数据恢复软件

Aiseesoft Data Recovery for Mac是一款高效且专业的数据恢复软件&#xff0c;专为Mac用户量身打造。 Aiseesoft Data Recovery for Mac v1.8.22激活版下载 无论是由于误删、格式化还是系统崩溃等原因导致的数据丢失&#xff0c;Aiseesoft都能帮助您快速找回。 它采用先进的扫描…

c# winform 控件皮肤

控件皮肤下载&#xff1a; https://download.csdn.net/download/m0_46973223/89225992 步骤&#xff1a; 第一步 将IrisSkin4.dll文件放在debug文件下&#xff0c;选一个或者多个后缀名为.ssk文件&#xff08;各个皮肤文件&#xff09;放在debug文件下。 第二步 解决方案资…

自动驾驶 | 仿真测试-HiL测试全解析

1.HiL 的定义 HiL&#xff08;Hardware-in-the-Loop&#xff09;硬件在环是计算机专业术语&#xff0c;也即是硬件在回路。通过使用 “硬件在环”(HiL) &#xff0c;可以显著降低开发时间和成本。在过去&#xff0c;开发电气机械元件或系统时,使用计算机仿真和实际的实验就已经…

PHP利用JWT refresh_token获取新access_token

PHP利用JWT refresh_token获取新token 在PHP中使用JWT&#xff08;JSON Web Tokens&#xff09;来刷新refresh_token并获取新的access_token&#xff0c;你需要实现以下步骤&#xff1a; 当用户登录时&#xff0c;生成一个access_token和一个refresh_token。 设置refresh_tok…

java中http调用组件深入详解

目录 一、前言 二、http调用概述 2.1 什么是http调用 2.1.1 http调用步骤 2.2 HTTP调用特点 2.3 HTTP调用应用场景 三、微服务场景下http调用概述 3.1 微服务开发中http调用场景 3.2 微服务组件中http的应用 四、常用的http调用组件 4.1 java中常用的http组件介绍 4…