一、资源准备
准备三张png格式图片
二、touchGFX工程搭建
放置三个按钮和一个image控件
image控件不选择如何图片
添加按键interactions
三、图片bin文件生成
生成bin文件用到两个工具,touchGFX designer和正点原子的C2B
1、利用touchGFX designer将图片生成数组
将图片添加进来,然后生成点击代码
2、拷贝出touchGFX designer生成的数组
到TouchGFX\generated\images\src
此文件夹加下拷贝出数组
3、修改touchGFX designer生成的数组
将cpp改为c文件
修改下面标注的部分
为
4、将touchGFX designer生成的数组改为bin文件
按如下设置,然后点击转换
5、将生成的bin文件拷贝到SD卡中
四、工程代码修改
1、修改TouchGFXHAL.cpp
文件
添加图片cache缓存空间
void TouchGFXHAL::initialize()
{
// Calling parent implementation of initialize().
//
// To overwrite the generated implementation, omit call to parent function
// and implemented needed functionality here.
// Please note, HAL::initialize() must be called to initialize the framework.
TouchGFXGeneratedHAL::initialize();
#define SDRAM_START_ADDR 0xc0000000
uint32_t frame_size = DISPLAY_HEIGHT*DISPLAY_WIDTH*3;//一个帧缓冲区的大小
setFrameBufferStartAddresses((void*)SDRAM_START_ADDR, (void*)(SDRAM_START_ADDR+frame_size), (void*)(SDRAM_START_ADDR+frame_size*2));//设置三级缓存地址,最后一级是开启动画功能的
setFrameRateCompensation(true);//开启帧速率补偿,对动画有平滑的作用
//图片缓存功能
#define BITMAP_CACHE_SIZE 0x1400000 //图片缓冲区大小,这里设为20MB
Bitmap::removeCache();
Bitmap::setCache((uint16_t*)(SDRAM_START_ADDR+frame_size*3),BITMAP_CACHE_SIZE,1);//设置资源缓冲区的地址和大小,最后一个默认的0不用传,让其自动计算
Bitmap::cacheAll();//开始缓存所有的图片
}
2、修改Screen1View.hpp
文件
#ifndef SCREEN1VIEW_HPP
#define SCREEN1VIEW_HPP
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
class Screen1View : public Screen1ViewBase
{
public:
Screen1View();
virtual ~Screen1View() {}
virtual void setupScreen();
virtual void tearDownScreen();
virtual void btn_load_image1_handler();
virtual void btn_load_image2_handler();
virtual void btn_load_image3_handler();
protected:
bool load_image(const char* imgPath, uint16_t imgWidth, uint16_t imgHeight);
Bitmap bitmapId;
};
#endif // SCREEN1VIEW_HPP
3、修改Screen1View.cpp
文件
#include <gui/screen1_screen/Screen1View.hpp>
#ifndef SIMULATOR
#include <unistd.h>
#include <fcntl.h>
#include "rtthread.h"
#endif
Screen1View::Screen1View()
{
}
void Screen1View::setupScreen()
{
Screen1ViewBase::setupScreen();
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
void Screen1View::btn_load_image1_handler()
{
if(this->load_image("/sdcard/touchgfx/image1.bin", 770, 180) == false)
rt_kprintf("load failed!\r\n");
}
void Screen1View::btn_load_image2_handler()
{
if(this->load_image("/sdcard/touchgfx/image2.bin", 770, 180) == false)
rt_kprintf("load failed!\r\n");
}
void Screen1View::btn_load_image3_handler()
{
if(this->load_image("/sdcard/touchgfx/image3.bin", 1024, 600) == false)
rt_kprintf("load failed!\r\n");
}
bool Screen1View::load_image(const char* imgPath, uint16_t imgWidth, uint16_t imgHeight)
{
#ifndef SIMULATOR
uint8_t *bitmapBuff = NULL;
uint32_t fileSize = 0;
uint32_t readNum;
if(this->bitmapId != BITMAP_INVALID)
{
Bitmap::dynamicBitmapDelete(bitmapId);
this->bitmapId = BITMAP_INVALID;
}
this->bitmapId = Bitmap::dynamicBitmapCreate(imgWidth, imgHeight, Bitmap::RGB888);
if(this->bitmapId != BITMAP_INVALID)
{
fileSize = imgHeight*imgWidth*3;
bitmapBuff = (uint8_t *)Bitmap::dynamicBitmapGetAddress(bitmapId);
int fd = open(imgPath, O_RDONLY);
if(fd <0)
{
rt_kprintf("open failed!\r\n");
return false;
}
readNum = read(fd, bitmapBuff, fileSize);
if(readNum == fileSize )
{
this->image1.setBitmap(bitmapId);
this->image1.invalidate();
this->box1.invalidate();
return true;
}
else
{
rt_kprintf("read failed!\r\n");
return false;
}
}
else
{
rt_kprintf("dynamicBitmapCreate failed!\r\n");
return false;
}
#else
return true;
#endif //
return false;
}
五、注意点