LVGL 7.11 tileview界面循环切换

定义变量


static const lv_point_t valid_pos2[] = { {0, 0}, {0, 1} ,{0,2},{0,3},{0,4}, {1,2} ,{2,2},{3,2} };
static int delay_cnt;
static int  last_hor_pos;
#define VALID_POS_LEN  sizeof(valid_pos2)/sizeof(valid_pos2[0])
typedef lv_obj_t* (*tileview_elem)(lv_obj_t* scr);



lv_obj_t* create_tileview_elem_00_03(lv_obj_t* scr)
{
	
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 0_3");

	return btn;
}

lv_obj_t* create_tileview_elem_0_1(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 0_1");
	return btn;
}

lv_obj_t* create_tileview_elem_0_2(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 0_2");
	return btn;
}

lv_obj_t* create_tileview_elem_0_3(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 0_3");
	return btn;
}

lv_obj_t* create_tileview_elem_04_01(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 0_1");
	return btn;
}

lv_obj_t* create_tileview_elem_1_2(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 1_2");
	return btn;
}

lv_obj_t* create_tileview_elem_2_2(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 2_2");
	return btn;
}

lv_obj_t* create_tileview_elem_3_2(lv_obj_t* scr)
{
	lv_obj_t* btn = lv_btn_create(scr, NULL);
	lv_obj_align(btn, scr, LV_ALIGN_CENTER, 0, 0);
	lv_obj_t* label = lv_label_create(btn, NULL);
	lv_label_set_text_fmt(label, "Button 3_2");
	return btn;
}

const tileview_elem s_tileview[VALID_POS_LEN] = {
		create_tileview_elem_00_03,
		create_tileview_elem_0_1,
		create_tileview_elem_0_2,
		create_tileview_elem_0_3,
		create_tileview_elem_04_01,
		create_tileview_elem_1_2,
		create_tileview_elem_2_2,
		create_tileview_elem_3_2,
};

定时器回调函数

static void page_task(struct _lv_task_t* task)
{

	if (lv_anim_count_running())
		return;
	lv_obj_t *obj = task->user_data;

	if (delay_cnt)
	{
		if (--delay_cnt == 0) {
			lv_coord_t x, y;
			lv_tileview_get_tile_act(obj, &x, &y);
			if (x == 0 && y == 0) {
				lv_tileview_set_tile_act(obj, 0, 3, LV_ANIM_OFF); 
			}
			else
				if (x == 0 && y == last_hor_pos) {
				lv_tileview_set_tile_act(obj, 0, 1, LV_ANIM_OFF);
			}
	  }
	}
	
}

事件处理回调函数

void btn_handler(struct _lv_obj_t* obj, lv_event_t event)
{
	LV_LOG_USER("--event %d", event);
	if (event == LV_EVENT_PRESSING)
	{
		delay_cnt = 10;
	}
}

创建tileview



lv_obj_t * init_main_tileview(void)
{
	

	lv_obj_t* tv = lv_tileview_create(lv_scr_act(), NULL);
	lv_obj_set_style_local_bg_opa(tv, LV_TILEVIEW_PART_SCROLLBAR, LV_STATE_DEFAULT, LV_OPA_TRANSP);
	lv_obj_set_style_local_bg_opa(tv, LV_TILEVIEW_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
	lv_tileview_set_valid_positions(tv, valid_pos2, VALID_POS_LEN);
	lv_tileview_set_edge_flash(tv, true);
	lv_obj_set_size(tv, LV_HOR_RES, LV_VER_RES);
	lv_obj_set_pos(tv, 0, 0);
	last_hor_pos = 0;
	for (int i = 0; i < VALID_POS_LEN; i++)
	{
		if (valid_pos2[i].x == 0)
		{
			if (valid_pos2[i].y > last_hor_pos)
				last_hor_pos = valid_pos2[i].y;
			
		}
	}

	LV_LOG_USER("--last_hor_pos %d", last_hor_pos);
	
	for (int i = 0; i < VALID_POS_LEN; i++) {
		
		lv_obj_t* cont = s_tileview[i](tv);
		lv_obj_set_size(cont, LV_HOR_RES, LV_HOR_RES);
		lv_obj_set_pos(cont, valid_pos2[i].x* LV_HOR_RES, valid_pos2[i].y* LV_HOR_RES);
		lv_tileview_add_element(tv, cont);
		if (i == 1 || i == last_hor_pos-1)
		{
			lv_obj_set_event_cb(cont, btn_handler);
			lv_obj_set_user_data(cont, tv);
		}	
	}
	
	lv_task_create(page_task, 10, LV_TASK_PRIO_LOW, tv);
	lv_tileview_set_tile_act(tv, 0, 2, LV_ANIM_OFF); 
	return tv;
}

运行效果

你可能感兴趣的:(little,VGL,C/C++,LVGL,7.11,tileview)