【03】优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-在lib目录新建自定义库UtilsLibrary,ComponentLibrary,CommonConstLibrary完成设置SettingsView.ets初始公共类书写-优雅草卓伊凡

简介: 【03】优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-在lib目录新建自定义库UtilsLibrary,ComponentLibrary,CommonConstLibrary完成设置SettingsView.ets初始公共类书写-优雅草卓伊凡

【03】优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-在lib目录新建自定义库UtilsLibrary,ComponentLibrary,CommonConstLibrary完成设置SettingsView.ets初始公共类书写-优雅草卓伊凡

项目背景

本项目渊源已久,优雅草2025年发布,PC端已经发布,将在4月完成成品发布,目前由于考虑到鸿蒙端用户使用,毕竟新一代纯血鸿蒙harmonyos next已经不再支持安卓android,因此优雅草团队必须考虑把鸿蒙端也开发上,以下实战过程完整记录了整个开发过程,优雅草卓伊凡审核代码,记录并更新,再次感谢优雅草团队所有同事们的努力,鸿蒙端为了加速鸿蒙生态已经开源地址供查看,关于优雅草星云物联网AI智控系统可以关注优雅草官网,在本文梳理的过程中发现在后面的页面中,只需要直接写出代码并且附上注释,卓伊凡相信大家应该看得懂,针对比较复杂的部分会单独做独立解释

项目开源代码地址

https://gitee.com/youyacao/axharmonyos

鸿蒙端运行环境

deveco 5.0.4版本

实战过程

本次过后为了方便预览我们采用previewer来预览项目,为了方便,因为真机运行目前需要的推荐配置是4G ram 8g rom 因此比较占用空间,虽然确实非常丝滑,但是一般我们留在最终测试的时候可以用。

建立公共文件src/main/ets/common/common.ets

import { FilterParams } from "@kit.ArkUI";
export class CommonConst {
    // log domain
    public static readonly LOG_ID: number = 0x8812;
    // 基础时间,超前
    public static readonly BASE_TIME = Number.MAX_VALUE;
    // 全局大号行高
    public static readonly FONT_LINE_HEIGHT: number = 32;
    public static readonly FONT_SIZE_SMALL: number = 12;
    // 卡片字体大小
    public static readonly FONT_SIZE_MEDIUM: number = 14;
    // 阅读文字大小
    public static readonly FONT_SIZE_LARGE: number = 16;
    // 二级标题
    public static readonly FONT_SIZE_XLARGE: number = 18;
    // 一级标题
    public static readonly FONT_SIZE_XXLARGE: number = 20;
    // 标题
    public static readonly FONT_SIZE_TITLE_MEDIUM: number = 24;
    // 大标题
    public static readonly FONT_SIZE_TITLE_LARGE: number = 28;
    // 全局左右内边距
    public static readonly GLOBAL_PADDING_LEFT_RIGHT: number = 20;
    // 全局内边距 8
    public static readonly GLOBAL_PADDING_8: number = 8;
    // 全局内边距 12
    public static readonly GLOBAL_PADDING_12: number = 12;
    // 全局内边距 16
    public static readonly GLOBAL_PADDING_16: number = 16;
    // 全局边距 超小
    public static readonly GLOBAL_SPACE_EXTRA_SMALL: number = 3;
    // 全局边距 小
    public static readonly GLOBAL_SPACE_SMALL: number = 5;
    // 全局边距 中
    public static readonly GLOBAL_SPACE_MEDIUM: number = 10;
    // 全局边距 大
    public static readonly GLOBAL_SPACE_LARGE: number = 20;
    // 满屏
    public static readonly FULL_SCREEN: string = "100%";
    // 组件间隔
    public static readonly COMPONENT_SPACE_SMALL: number = 5;
    public static readonly COMPONENT_SPACE_MEDIUM: number = 10;
    public static readonly COMPONENT_SPACE_LARGE: number = 20;
    // 全局圆角
    public static readonly GLOBAL_ROUND: number = 8;
    // 全局子圆角
    public static readonly GLOBAL_SUB_ROUND: number = 3;
    // 全局字间距
    public static readonly GLOBAL_LETTER_SPACING: number = 0;
    // 特小图标
    public static readonly ICON_EXTRA_SMALL_SIZE: number = 16;
    // 小 图标
    public static readonly ICON_SMALL_SIZE: number = 24;
    // 中 图标
    public static readonly ICON_MEDIUM_SIZE: number = 32;
    // 大 图标
    public static readonly ICON_LARGE_SIZE: number = 40;
    // 超短动画时长
    public static readonly ANIMATION_EXTRA_SMALL_DURATION: number = 80;
    // 短动画时长
    public static readonly ANIMATION_SHORT_DURATION: number = 300;
    // 中动画时长
    public static readonly ANIMATION_MEDIUM_DURATION: number = 500;
    // 长动画时长
    public static readonly ANIMATION_LONG_DURATION: number = 700;
    // 小插画
    public static readonly IMAGE_SMALL_SIZE: number = 200;
    // 中插画
    public static readonly IMAGE_MEDIUM_SIZE: number = 280;
    // 大插画
    public static readonly IMAGE_LARGE_SIZE: number = 360;
    // 全局可见区间
    public static readonly GLOBAL_VISIBLE_AREA: number[] = [0, 0.95];
    // 全屏
    public static readonly GLOBAL_FULL_SCREEN: string = "100%";
    public static readonly SETTING_ITEM: string[] = [
        "系统配置",
        "消息服务配置",
        "文件服务配置",
        "SSH启动配置",
        "通知媒介",
        "通知策略",
        "通知模板"
    ];
}

定义了共用的文字类,
建立公共颜色类src/main/ets/common/commonColor.ets

@ObservedV2
export class CommonColor {
    // brand
    @Trace public BRAND_COLOR: ResourceColor = "#008DF0"  //$r('sys.color.brand');
    // background
    public BACKGROUND_COLOR: ResourceStr = $r('sys.color.comp_background_gray');  //$r('app.color.comp_background_gray');
    // Card background
    public CARD_BACKGROUND_COLOR: ResourceStr = $r('sys.color.comp_background_primary');
    // CARD 二级背景
    public CARD_BACKGROUND_SECONDARY_COLOR: ResourceStr = $r('sys.color.comp_background_tertiary');
    // 前景颜色
    public FOREGROUND_COLOR: ResourceStr = $r('sys.color.comp_foreground_primary');
    // 文本
    public FONT_COLOR: ResourceStr = $r('sys.color.font_primary');
    // 辅助文本
    public FONT_SECONDARY_COLOR: ResourceStr = $r('sys.color.font_secondary');
    // 三级
    public FONT_TERTIARY_COLOR: ResourceStr = "#999999";
    // 3333
    public FONT_3333_COLOR: ResourceStr = "#FF333333";
    // #FF666666
    public FONT_6666_COLOR: ResourceStr = "#FF666666";
    // #FF2F3133
    public FONT_2F3133_COLOR: ResourceStr = "#FF2F3133";
    // #FF008DF0
    public FONT_00DFF0_COLOR: ResourceStr = "#FF00DFF0";
    // #FF999999
    public FONT_9999_COLOR: ResourceStr = "#FF999999";
    // 警告颜色
    public WARNING_COLOR: ResourceStr = $r('sys.color.warning');
    // 二级警告
    public SECONDARY_WARNING_COLOR: ResourceStr = $r('sys.color.alert');
    // 确认颜色
    public CONFIRM_COLOR: ResourceStr = $r('sys.color.confirm');
    // 白色
    public WHITE_COLOR: ResourceStr = $r('sys.color.font_on_primary');
    // #FF111111 背景
    public BACKGROUND_111111_COLOR: ResourceStr = "#FF111111";
    // #FFE6EBF0
    public E6EBF0_COLOR: ResourceStr = "#FFE6EBF0";
    // 黑色
    public BLACK_COLOR: ResourceStr = "#FF000000";
    // 将#34FFBB 格式转化为  rgba(52, 255, 187, 0.5)
    public hexToRgba(hex: string, alpha: number): string {
        const rgb = parseInt(hex.slice(1), 16);
        return `rgba(${rgb >> 16}, ${(rgb >> 8) & 0xff}, ${rgb & 0xff}, ${alpha})`;
    }
}
export const commonColor = new CommonColor();

这段代码定义了一个名为 CommonColor 的类,用于管理各种颜色常量和提供一个将十六进制颜色转换为 RGBA 颜色的方法。最后,导出了这个类的一个实例 commonColor,方便在其他地方使用这些颜色常量和方法。

继续我们在ComponentLibrary库建立定义弹窗框的类src/main/ets/common/UIWindows.ets

import { window, display } from '@kit.ArkUI';
import { KeyboardAvoidMode } from '@kit.ArkUI';
/**
 * UIWindows 类负责管理窗口的初始化及其属性
 */
class UIWindows {
    public window?: window.Window;
    private windowHeight: number = 0;
    private windowWidth: number = 0;
    private barHeight: number = 0;
    private navigationHeight: number = 0;
    private TAG = 'UIWindows';
    /**
     * 初始化窗口,包括设置键盘避免模式、全屏布局、系统栏属性以及获取窗口和导航栏的高度
     * @param window 窗口对象
     */
    initWindow(window: window.Window): void {
        this.window = window;
        this.window.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.OFFSET);
        this.window.setWindowLayoutFullScreen(true);
        this.window.setWindowSystemBarProperties({
            statusBarContentColor: "#FFFFFF"
        });
        this.barHeight = this.initWindowBarHeight();
        this.navigationHeight = this.initWindowNavigationHeight();
        this.windowHeight = this.initWindowHeight();
        this.windowWidth = this.initWindowWidth();
    }
    /**
     * 获取窗口高度
     * @returns 窗口高度
     */
    public getWindowHeight(): number {
        return this.windowHeight;
    }
    /**
     * 获取窗口宽度
     * @returns 窗口宽度
     */
    public getWindowWidth(): number {
        return this.windowWidth;
    }
    /**
     * 获取状态栏高度
     * @returns 状态栏高度
     */
    public getBarHeight(): number {
        return this.barHeight;
    }
    /**
     * 获取导航栏高度
     * @returns 导航栏高度
     */
    public getNavigationHeight(): number {
        return this.navigationHeight;
    }
    /**
     * 初始化窗口高度
     * @returns 窗口高度
     */
    private initWindowHeight(): number {
        return Math.floor(px2vp(display.getDefaultDisplaySync().height));
    }
    /**
     * 初始化窗口宽度
     * @returns 窗口宽度
     */
    private initWindowWidth(): number {
        return Math.floor(px2vp(display.getDefaultDisplaySync().width));
    }
    /**
     * 初始化状态栏高度
     * @returns 状态栏高度
     */
    private initWindowBarHeight(): number {
        if (!this.window) {
            return 0;
        }
        const avoidArea = this.window.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
        return Math.floor(px2vp(avoidArea.topRect.height));
    }
    /**
     * 初始化导航栏高度
     * @returns 导航栏高度
     */
    private initWindowNavigationHeight(): number {
        if (!this.window) {
            return 0;
        }
        const avoidArea = this.window.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
        return Math.floor(px2vp(avoidArea.bottomRect.height));
    }
}
// 创建 UIWindows 实例
export const uiWindows = new UIWindows();

此代码定义了一个名为 UIWindows 的类,用于管理窗口相关的属性和操作。该类提供了初始化窗口、获取窗口尺寸和状态栏、导航栏高度等功能。最后,导出了这个类的一个实例 uiWindows,以便在其他地方使用这些功能。注意代码里的 px2vp 函数未给出定义,它可能是一个用于将像素值转换为虚拟像素值的自定义函数。

接下来我们要写更多关于设置的页面:

当然了这是我们的最终效果,显得非常的丝滑:

目录
相关文章
|
人工智能 监控 安全
提效40%?揭秘AI驱动的支付方式“一键接入”系统
本项目构建AI驱动的研发提效系统,通过Qwen Coder与MCP工具链协同,实现跨境支付渠道接入的自动化闭环。采用多智能体协作模式,结合结构化Prompt、任务拆解、流程管控与安全约束,显著提升研发效率与交付质量,探索大模型在复杂业务场景下的高采纳率编码实践。
1024 26
提效40%?揭秘AI驱动的支付方式“一键接入”系统
|
人工智能 自然语言处理 前端开发
最佳实践2:用通义灵码以自然语言交互实现 AI 高考志愿填报系统
本项目旨在通过自然语言交互,结合通义千问AI模型,构建一个智能高考志愿填报系统。利用Vue3与Python,实现信息采集、AI推荐、专业详情展示及数据存储功能,支持响应式设计与Supabase数据库集成,助力考生精准择校选专业。(239字)
1129 13
|
存储 人工智能 搜索推荐
LangGraph 记忆系统实战:反馈循环 + 动态 Prompt 让 AI 持续学习
本文介绍基于LangGraph构建的双层记忆系统,通过短期与长期记忆协同,实现AI代理的持续学习。短期记忆管理会话内上下文,长期记忆跨会话存储用户偏好与决策,结合人机协作反馈循环,动态更新提示词,使代理具备个性化响应与行为进化能力。
2042 10
LangGraph 记忆系统实战:反馈循环 + 动态 Prompt 让 AI 持续学习
|
机器学习/深度学习 人工智能 JSON
PHP从0到1实现 AI 智能体系统并且训练知识库资料
本文详解如何用PHP从0到1构建AI智能体,涵盖提示词设计、记忆管理、知识库集成与反馈优化四大核心训练维度,结合实战案例与系统架构,助你打造懂业务、会进化的专属AI助手。
1672 6
|
人工智能 JSON 安全
Claude Code插件系统:重塑AI辅助编程的工作流
Anthropic为Claude Code推出插件系统与市场,支持斜杠命令、子代理、MCP服务器等功能模块,实现工作流自动化与团队协作标准化。开发者可封装常用工具或知识为插件,一键共享复用,构建个性化AI编程环境,推动AI助手从工具迈向生态化平台。
2402 1
|
消息中间件 人工智能 安全
云原生进化论:加速构建 AI 应用
本文将和大家分享过去一年在支持企业构建 AI 应用过程的一些实践和思考。
2792 99
|
人工智能 安全 中间件
阿里云 AI 中间件重磅发布,打通 AI 应用落地“最后一公里”
9 月 26 日,2025 云栖大会 AI 中间件:AI 时代的中间件技术演进与创新实践论坛上,阿里云智能集团资深技术专家林清山发表主题演讲《未来已来:下一代 AI 中间件重磅发布,解锁 AI 应用架构新范式》,重磅发布阿里云 AI 中间件,提供面向分布式多 Agent 架构的基座,包括:AgentScope-Java(兼容 Spring AI Alibaba 生态),AI MQ(基于Apache RocketMQ 的 AI 能力升级),AI 网关 Higress,AI 注册与配置中心 Nacos,以及覆盖模型与算力的 AI 可观测体系。
2139 95
|
人工智能 运维 Kubernetes
Serverless 应用引擎 SAE:为传统应用托底,为 AI 创新加速
在容器技术持续演进与 AI 全面爆发的当下,企业既要稳健托管传统业务,又要高效落地 AI 创新,如何在复杂的基础设施与频繁的版本变化中保持敏捷、稳定与低成本,成了所有技术团队的共同挑战。阿里云 Serverless 应用引擎(SAE)正是为应对这一时代挑战而生的破局者,SAE 以“免运维、强稳定、极致降本”为核心,通过一站式的应用级托管能力,同时支撑传统应用与 AI 应用,让企业把更多精力投入到业务创新。
1197 30
|
设计模式 人工智能 自然语言处理
3个月圈粉百万,这个AI应用在海外火了
不知道大家还记不记得,我之前推荐过一个叫 Agnes 的 AI 应用,也是当时在 WAIC 了解到的。
1115 2
|
存储 人工智能 NoSQL
AI大模型应用实践 八:如何通过RAG数据库实现大模型的私有化定制与优化
RAG技术通过融合外部知识库与大模型,实现知识动态更新与私有化定制,解决大模型知识固化、幻觉及数据安全难题。本文详解RAG原理、数据库选型(向量库、图库、知识图谱、混合架构)及应用场景,助力企业高效构建安全、可解释的智能系统。