【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
前面说到了scroll bar,下面还是首先看一下效果图。
说完了滑动条。下面就来谈一谈image的相关流程,特别是demo代码的编写。正好ftk源代码中也包含了image的相关内容,我们不妨首先了解一下相关的流程是如何编写的。
#include "ftk.h" static Ret timeout(void* ctx) { ftk_quit(); ftk_logd("%s: timeout and quit.\n", __func__); return RET_REMOVE; } int FTK_MAIN(int argc, char* argv[]) { FtkSource* timer = NULL; FtkWidget* image = NULL; FtkWidget* win = NULL; char filename[FTK_MAX_PATH+1] = {0}; ftk_init(argc, argv); timer = ftk_source_timer_create(5000, timeout, NULL); win = ftk_window_create(0, 0, 320, 480); ftk_snprintf(filename, FTK_MAX_PATH, "%s/earth.png", TESTDATA_DIR); image = ftk_image_create(win, 0, 0, ftk_widget_width(win)/2, ftk_widget_height(win)/2); ftk_image_set_image(image, ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename)); ftk_snprintf(filename, FTK_MAX_PATH, "%s/png_RGB_tRNS.png", TESTDATA_DIR); image = ftk_image_create(win, 0, 0, ftk_widget_width(win)/2, ftk_widget_height(win)/2); ftk_image_set_image(image, ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename)); ftk_widget_set_attr(image, FTK_ATTR_TRANSPARENT); ftk_snprintf(filename, FTK_MAX_PATH, "%s/Calculator.png", TESTDATA_DIR); image = ftk_image_create(win, ftk_widget_width(win)/2, 0, ftk_widget_width(win)/2, ftk_widget_height(win)/2); ftk_image_set_image(image, ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename)); ftk_widget_set_attr(image, FTK_ATTR_BG_TILE); ftk_snprintf(filename, FTK_MAX_PATH, "%s/t8.bmp", TESTDATA_DIR); image = ftk_image_create(win, 0, ftk_widget_height(win)/2, ftk_widget_width(win)/2, ftk_widget_height(win)/2); ftk_image_set_image(image, ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename)); ftk_widget_set_attr(image, FTK_ATTR_BG_CENTER); ftk_snprintf(filename, FTK_MAX_PATH, "%s/jpeg1.jpg", TESTDATA_DIR); image = ftk_image_create(win, ftk_widget_width(win)/2, ftk_widget_height(win)/2, ftk_widget_width(win)/2, ftk_widget_height(win)/2); ftk_image_set_image(image, ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename)); ftk_widget_set_attr(image, FTK_ATTR_BG_TILE); ftk_widget_set_text(win, "image demo"); ftk_widget_show_all(win, 1); ftk_widget_set_attr(win, FTK_ATTR_QUIT_WHEN_CLOSE); ftk_main_loop_add_source(ftk_default_main_loop(), timer); ftk_run(); return 0; }
首先请大家迅速浏览一下整个代码流程。流程其实不复杂,主要的内容其实都在FTK_MAIN里面。从行数上看,看似复杂的代码,其实很简单。关于image的内容主要集中在几个函数之间,分别是ftk_image_create、ftk_image_set_image和ftk_widget_set_attr。三个函数的功能依次为创建图像、设置图像和设置属性。细心的朋友可能发现了整份代码中有五份图像显示,但是在效果图上只有四幅图像可以显示。原因是什么呢?关键就在于FTK_ATTR_TRANSPARENT这个属性的设置了。具体内容还请大家多看看代码吧。
效果图,下次见了。