层的运用也许是解决界面刷新,复杂界面的堆叠,代码维护最好的方法
这两天一直在研究层的概念和怎么运用它。MTK相关的资料很少,首先感谢那些肯把自己手里资料奉献出来给大家共享的人。
这里举一个简单的例子说明层的创建和运用
在这里我们创建两个层,加上基层一共三个,基层作为背景层,
MTK最多支持四个层合成,层的创建最好放在窗口里或者窗口控件里,
在退出窗口时释放~!要不会出问题的。,当然在别的地方创建也是可以的,
但要注意不能是窗口需要频繁调用的函数,因为每次创建窗口都要分配内存,
频繁调用会导致内存耗尽,退出时切记释放。该代码省略了一些与层无关的内容,比如按按键处理,触屏处理,请知悉
PU8 buf_ptr;
gdi_handle image_buffer_handle;
gdi_handle image_buffer_handle1;
gdi_handle base_handle;
#define LAYER_OPACITY 255
S32 num = 0;
extern void mmi_image_timer();
//退出释放内存函数和停止定时函数
void my_layer_exit(void)
{
gdi_layer_multi_layer_disable();
if (buf_ptr != NULL)
{
med_free_ext_mem((void **)&buf_ptr);
}
if (image_buffer_handle != NULL)
{
gdi_layer_free(image_buffer_handle);
}
if (image_buffer_handle1 != NULL)
{
gdi_layer_free(image_buffer_handle1);
}
gui_cancel_timer(mmi_image_timer);
}
//重新绘制屏幕
void redraw_my_screen()
{
gui_lock_double_buffer();
gdi_layer_set_active(image_buffer_handle);
gdi_layer_set_opacity(TRUE, 255 - num);
gdi_image_draw_id(40, 50, MAIN_MENU_VDOPLY_ICON);
gdi_layer_set_active(image_buffer_handle1);
gdi_layer_set_opacity(TRUE,num);
gdi_image_draw_id(50, 100, MAIN_MENU_AUDPLY_ICON);
gdi_layer_get_base_handle(&base_handle);
gdi_layer_set_active(base_handle);
gdi_layer_clear_background(GDI_COLOR_BLACK);
gdi_layer_set_source_key(FALSE, GDI_COLOR_BLACK);
gdi_layer_set_active(image_buffer_handle);
gdi_layer_set_source_key(TRUE, GDI_COLOR_RED);
gui_unlock_double_buffer();
gdi_layer_set_blt_layer(base_handle, image_buffer_handle, image_buffer_handle1, 0);
gdi_layer_blt_previous(0, 0, UI_device_width - 1, UI_device_height - 1);
}
//定时重复显示屏幕函数
//随便写的定时函数,有错误
//如果想得到自己的效果改下就可以了
void mmi_image_timer()
{
if(num >= LAYER_OPACITY)
{
//做NUM减小
}
redraw_my_screen();
num += 10;
gui_start_timer(100,mmi_image_timer);
}
void my_layer_entry(void)
{
U8 *gui_buffer;
BOOL isEnable;
gdi_color base_color;
//进入新的屏幕
EntryNewScreen (SCR_ID_FNG_AREA,my_layer_exit,my_layer_entry, NULL);
gui_buffer = GetCurrGuiBuffer(SCR_ID_FNG_AREA);
entry_full_screen();//全屏
gdi_layer_multi_layer_enable();//开启多层绘制
//分配内存空间,记得分配了要释放
buf_ptr = med_alloc_ext_mem(GDI_LCD_WIDTH*GDI_LCD_HEIGHT*3);
if (buf_ptr == NULL)
{
return;
}
//创建层,gdi_layer_create和gdi_layer_create_using_outside_memory是有区别的
//不做详解,望文生义可略知一二
gdi_layer_create_using_outside_memory(
0,
0,
UI_device_width,
UI_device_height,
&image_buffer_handle,
(PU8) buf_ptr,
(S32) GDI_LCD_WIDTH*GDI_LCD_HEIGHT*3);
//激活层
gdi_layer_set_active(image_buffer_handle);
//gdi_layer_clear_background(GDI_COLOR_RED);
//gdi_layer_set_source_key(TRUE, GDI_COLOR_RED);
gdi_layer_set_opacity(TRUE, 255);//设置透明度
gdi_image_draw_id(40, 50, MAIN_MENU_VDOPLY_ICON);
//同上
gdi_layer_create(0, 0, UI_device_width, UI_device_height, &image_buffer_handle1);
gdi_layer_set_active(image_buffer_handle1);
gdi_layer_clear_background(GDI_COLOR_TRANSPARENT);//刷色
gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);//设置通透
gdi_layer_set_opacity(TRUE,0);
gdi_image_draw_id(50, 100, MAIN_MENU_AUDPLY_ICON);
//这里要激活下背景层
gdi_layer_get_base_handle(&base_handle);
gdi_layer_set_active(base_handle);
gdi_layer_clear_background(GDI_COLOR_BLACK);
gdi_layer_set_source_key(FALSE, GDI_COLOR_BLACK);
//设置最后那个为激活层,以后的绘制都在这个层上
gdi_layer_set_active(image_buffer_handle);
//得到当前活动层的通透属性
//gdi_layer_get_source_key(&isEnable, &base_color);
//设置通透
gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);
//合并图层,注意先后顺序
gdi_layer_set_blt_layer(base_handle, image_buffer_handle, image_buffer_handle1, 0);
//刷屏
gdi_layer_blt_previous(0, 0, UI_device_width - 1, UI_device_height - 1);
//开始计时进入
gui_start_timer( 500, mmi_image_timer);
SetKeyHandler( GoBackHistory, KEY_RSK, KEY_EVENT_UP);
}