|
|
基于XXXXX\tools\DebugKits\graphic_tools\image_converter_tool中的脚本运行如下代码:
python images_tool.py -p dir -d cmp -c config.xml -i D:\tjd_image\image\in -o D:\tjd_image\image\out -t D:\tjd_image\image\out
在D:\tjd_image\image\in下放置需要生成bin文件的图片文件夹,即可在D:\tjd_image\image\out路径下生成D:\tjd_image\image\out\img_menu_list\xxx.bin文件和ui_resource_image.h文件
以下为代码示例演示生成一个三行三列的列表
#include "mainView.h"
#include "events.h"
// D:\tjd_image\image\out\img_menu_list
namespace OHOS {
void MainView::Init()
{
if (container_ == nullptr) {
container_ = new UIScrollView();
container_->SetPosition(0, 0, 466, 466); // 466 width and height
container_->SetStyle(STYLE_BACKGROUND_OPA, 0);
}
// auto img = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(static_cast<uint32_t>(UIResourceImage::IMG_MENU_LIST_MENU_AI), "D:/tjd_image/image/out/img_menu_list/img_menu_list.bin");
// auto img= OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes("D:/tjd_image/image/out/img_menu_list/img_menu_list.bin");
// imagView.SetPosition(233, 233);
// imagView.SetSrc(img);
// container_->Add(&imagView);
// (57, 66) 30 - 20
// 假设你有一个容器来添加 UI 元素,比如 container_
// 以及一个图片资源 img,它应该是一个有效的图片路径或对象
// 设定每个 UIImageView 的尺寸
int imageViewWidth = 98;
int imageViewHeight = 98;
// 设定网格的起始位置(左上角)
int startX = 57; // 第一个元素的 x 坐标
int startY = 66; // 第一行第一个元素的 y 坐标
// 设定每个 UIImageView 在水平方向上的间距
int xSpacing = 30+98;
// 设定行与行之间的总间距(图片高度 + 额外间距)
int rowSpacing = imageViewHeight + 20;
int k=0;
// 遍历三行三列
for (int i = 0; i < 3; ++i) { // 行
for (int j = 0; j < 3; ++j) { // 列
auto img = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(0x100001+k, "D:/tjd_image/image/out/img_menu_list/img_menu_list.bin");
++k;
// 创建一个新的 UIImageView 实例(注意:这里的创建方式取决于你的UI框架)
auto *newImage = new UIImageView(); // 假设这是你的UI框架中创建图片视图的方式
// 计算当前元素的 x 和 y 坐标
int x = startX + j * xSpacing;
int y = startY + i * rowSpacing;
// 设置 UIImageView 的位置和大小(注意:这里的SetPosition方法取决于你的UI框架)
// 假设SetPosition接受四个参数:x, y, width, height
newImage->SetPosition(x, y, imageViewWidth, imageViewHeight);
// printf("(%d, %d)\r\n", x, y);
// 设置 UIImageView 的图片源(注意:这里的SetSrc方法取决于你的UI框架)
// 假设SetSrc接受一个图片资源作为参数
newImage->SetSrc(img); // 确保img是一个有效的图片资源
newImage->Scale({98.0/110,98.0/110}, {0,0});
// 将 UIImageView 添加到容器中(注意:这里的Add方法取决于你的UI框架)
container_->Add(newImage); // 假设container_是一个可以添加UI元素的容器
}
}
}
void MainView::DeInit()
{
if (container_ != nullptr) {
container_->RemoveAll();
delete container_ ;
container_ = nullptr;
}
if (container1 != nullptr) {
container1->RemoveAll();
delete container1 ;
container1 = nullptr;
}
}
UIView* MainView::GetView()
{
Init();
return container_;
}
}
|
|