【平头哥RVB2601创意应用开发】lvgl使用1——标签
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">在</font>RVB2601上尝试屏幕也差不多有几个月了,勉强算是有些心得,之前零零散散写了东西,这里把它整理了一下,这是第一篇——字体使用。</span></span></span></span></p><p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">在</font>RVB2601的源代码中使用little vgl的标签来显示文字,根据调用不同的字体库实现文字大小的变化,我们知道RVB2601的屏是一个128*64的单色屏,因此其上使用的文字不会超过这个范围。</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">打开</font>lv_font源文件夹,可以看到已经定义了一些高度</span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">8~</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线">48像素的文字的字体</span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">,具体怎么使用,例程很简单,大家一看就明白,也有不少小伙伴发了心得,我在这里只简单介绍一下如何更换字体,主要是修改</font><font face="等线">lv_conf.h文件,这里以显示中文为例。</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">在</font><font face="等线">lvgl中,缺省是不支持中文的,如果在label中尝试使用中文文字,那么什么都不会显示。因此要让其显示中文,需要修改使用的字体,尝试了自带的各种字体之后,发现只有lv_font_simsun_16_cjk字体可以支持中文。修改方法也很简单,在lv_conf.h文件中,可以查看文字的定义,如下:</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_FONT_SIMSUN_16_CJK 1 /*1000 most common CJK radicals*/</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">说明</font><font face="等线">v_font_simsun_16_cjk字体是可用的,另外查看所用的缺省字体如下:</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_SMALL &lv_font_montserrat_14</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_NORMAL &lv_font_montserrat_14</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_SUBTITLE &lv_font_montserrat_14</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_TITLE &lv_font_montserrat_14</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">如果希望中文为主显示,可以修改缺省字体为</font><font face="等线">lv_font_simsun_16_cjk,如下:</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_NORMAL &lv_font_simsun_16_cjk</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">再到</font><font face="等线">label中使用,就能“非常有限”的使用中文了,字体库中的中文相对较少,不一定能满足需要。比如我们尝试显示如下文字:“这是一个简单的测试例。”会发现显示如下:</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">文字缺失得太多,基本不可直接使用。</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">于是考虑自己做一个字库来用,事实上,网上可以找到不少很好(免费)的字模软件,但我还没找到一款可以直接在</font>lvgl中使用,自己尝试做一款。</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">字体源文件是个文本文件,多看几遍也就猜出是什么意思了,这里不展开</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">介绍,有兴趣讨论加我好友单聊</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">。做了一个小工具:</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">网上随便找几张图片,比如米老鼠、喜羊羊、简笔画的恐龙等等,载入图片</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">:</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">选择文字高度,尝试预览一下:</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">发现淡了点,</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">可以加大颜色阈值</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">符合心意之后,用一个符号(可以是标点、英文、数字、中文</font><font face="等线">……)来记录,在以后的使用中,label框中调用这个符号显示信息。</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">确定之后,不再支持修改文字高度。类似的我们再添加一点其它文字。比如某个恐龙。</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">完成以后试试效果,使用</font><font face="等线">CDK的例程ch</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线">2601-gui-demo,将lv_label_set_text中的字符串改为自定义字库时所用的符号</span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">,修改</font><font face="等线">lvgl\v7.4.3\src\lv_font\font.h文件,添加</font></span></span></span></span></p>
<p style="margin-left:24px; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">LV_FONT_DECLARE(lv_user_test)</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">在</font><font face="等线">lvgl\v7.4.3\src\lv_conf_kconfig.h中添加</font></span></span></span></span></p>
<p style="margin-left:24px; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">#define CONFIG_LV_THEME_DEFAULT_FONT_NORMAL &lv_user_test</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">在</font><font face="等线">lvgl\v7.4.3\lv_conf.h中修改</font></span></span></span></span></p>
<p style="text-indent:18.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线">#define LV_THEME_DEFAULT_FONT_NORMAL &lv_user_test</span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">修改完成以后,我们尝试显示一个</font><font face="等线">“恐</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">@M\n4</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">”的标签:</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">lv_label_set_text(p, "恐@</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">M</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">\n</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">4</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">");</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">其中</font><font face="等线">“恐”</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">指的是恐龙,</font><font face="等线">“@”是蛇,“M”是米老鼠,“\n”是回车,“4”是“天道酬勤”四个字。效果如下:</font></span></span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">哈哈,字体文件还是挺有效的</font></span></span><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">,字母汉字数字标点都能用,各种宽度都支持。</font></span></span></span></span></p>
<p style="text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线"><span style="font-size:10.5000pt"><span style="font-family:等线"><font face="等线">PS:发之前给一个朋友看了,他说“你这是画图,不是文字”,我回答,这就是文字,lvgl使用的文字不像windows可以使用矢量字体,每个字体内的文字数量和大小都是固定的,说是多少像素就是多少像素。</font></span></span></span></span></p>
<p>好奇,字体源文件是个文本文件,多看几遍也就猜出是什么意思了,不好猜</p>
Jacktang 发表于 2022-5-22 15:46
好奇,字体源文件是个文本文件,多看几遍也就猜出是什么意思了,不好猜
<p>好猜的,比如“!”是“0xff, 0xcc”,转换一下“11111111 11001100”屏幕上看到是两列,竖着5个像素,空一格,又是一个像素。结合box_w = 2, .box_h = 7, .ofs_x = 3, .ofs_y = 1,基本能看出来是什么意思了。再随便找几个验证一下,就知道猜得没错。</p>
页:
[1]