LITTLEVGL--绘制圆--3

画圈

标题一、 目的

   采用littlegl绘制一个圈。

标题二、 流程方案

标题2.1 方案原理

 littlegl中提供了一个画弧度的函数。我们可以采用此函数绘制一个圆。此UI系统中将各个对象外观,属性和行为都独立分开。
 要绘制一个圆,我们至少要做两个操作,即圆绘制(包含了半径大小),另外一个为圆的一些属性(修饰或者风格),即颜色,边框宽度,以及叠影等。

标题 绘制圆弧的函数:

lv_arc_set_angles(arc, start_angle, end_angle)

标题 配置属性的函数:

lv_arc_set_style(arc, LV_ARC_STYLE_MAIN, &style)

标题2.2 流程

  1. 配置圆的风格属性;
  2. 创建圆arc,确定其显示位置以及大小;
  3. 给arc加载1配置的风格属性。

标题三、 代码

标题3.1 新建arc案例的头文件和源文件

 在pc_simulator_sdl_visual_studio-master\visual_studio_2017_sdl\lv_examples\lv_tests中创建cr_test目录,并在其中创建cr_arc.h和cr_arc.c文件。

文件显示
vc下的工程结构

main.c中添加对此头文件引用#include"lv_examples/lv_tests/cr_test/cr_arc.h"**

标题3.2 编写源文件和头文件

标题 源文件

#include <stdlib.h>
#include <Windows.h>
#include <SDL.h>
#include "lvgl/lvgl.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/mouse.h"
#include "lv_drivers/indev/keyboard.h"
#include "lv_examples/lv_apps/demo/demo.h"
#include "lv_examples/lv_apps/benchmark/benchmark.h"
#include "lv_examples/lv_tests/lv_test_theme/lv_test_theme_1.h"
#include "lv_examples/lv_tutorial/10_keyboard/lv_tutorial_keyboard.h"




static void ArcTaskCb(lv_task_t *t)
{
	static int16_t a = 0;

	//1. 每20ms任务调度时,此a+3;
	a += 3;
	if(a > 360)a = 360;

	//2. 给user_data这个传递过来的圆弧对象重绘制0度->a度的弧度
	lv_arc_set_angles(t->user_data, 0 ,a);

	if(a == 360){
		//删除任务
		lv_task_del(t);
		a = 0;
		printf("task cut!!\r\n");//debug
		return ;
	}
}



void DrawArc()
{
  //1. style config
	 static lv_style_t style;
	 lv_style_copy(&style, &lv_style_plain);//复制默认常规属性lv_style_plain,这样属性不用大规模复制
	 style.line.color = LV_COLOR_NAVY;          //深蓝色
	 style.line.width = 8;                       //宽度


	  //2. arc config
	lv_obj_t * arc = lv_arc_create(lv_scr_act(), NULL);//在当前screen对象上创建一个arc,则arc父类为screen
	lv_arc_set_angles(arc, 90, 190);//初始显示的弧度为一个90度到190度的100度半圆
	lv_arc_set_style(arc, LV_ARC_STYLE_MAIN, &style);//配置圆弧风格
	lv_obj_align(arc, NULL, LV_ALIGN_CENTER, 0, 0);	//在其父对象(这里就是screen)上的中间显示
  	lv_task_create(ArcTaskCb, 20, LV_TASK_PRIO_LOWEST, arc);//创建了一个20ms,优先级为最低的ArcTaskCb重绘任务,并向任务的user_data参数传递arc指针
	
}

标题 头文件

仅仅是声明了DrawArc函数。

#ifndef CR_ARC_H_
#define CR_ARC_H_

void DrawArc();

#endif

标题3.3 main.c函数调用

在实现demo中,需要在lv_init()和hal_init()两个函数初始化后调用【具体原因见其他章节】。

int main(int argc, char** argv)
{
    /*Initialize LittlevGL*/
    lv_init();

    /*Initialize the HAL for LittlevGL*/
    hal_init();

    /*
     * Demo, benchmark, tests and tutorial.
     *
     * Uncomment any one (and only one) of the functions below to run that
     * particular demo, test or tutorial.
     */

	DrawArc();//调用

	//ButtonTest(); SliderInit(); ListInit();
   // demo_create();
    //benchmark_create();
    //lv_test_theme_1(lv_theme_night_init(210, NULL));
    //lv_test_theme_1(lv_theme_night_init(100, NULL));
    //lv_test_theme_1(lv_theme_material_init(210, NULL));
    //lv_test_theme_1(lv_theme_alien_init(210, NULL));
    //lv_test_theme_1(lv_theme_zen_init(210, NULL));
    //lv_test_theme_1(lv_theme_nemo_init(210, NULL));
    //lv_test_theme_1(lv_theme_mono_init(210, NULL));
    //lv_test_theme_1(lv_theme_default_init(210, NULL));
    //lv_tutorial_keyboard(kb_indev);

    while (1) {
        /* Periodically call the lv_task handler.
        * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();
        Sleep(8);       /*Just to let the system breathe */
    }

    return 0;
}

四、 结果
在这里插入图片描述

发布了17 篇原创文章 · 获赞 5 · 访问量 5633

猜你喜欢

转载自blog.csdn.net/weixin_43854435/article/details/100938746