
鸿蒙5设计资源下载:官方Figma/Sketch模板使用指南
一、获取官方设计资源
- 官方资源下载地址
鸿蒙官方设计资源可在华为开发者联盟获取:
https://developer.harmonyos.com/cn/design/resource
2. 资源包内容
官方资源包包含:
设计规范文档(PDF格式)
Figma设计模板(.fig文件)
Sketch设计模板(.sketch文件)
图标资源包(SVG格式)
配色方案文件(JSON格式)
字体资源包(.ttf文件)
二、设计模板使用指南
- Figma模板使用
// Figma插件:鸿蒙设计助手
// 安装链接:https://www.figma.com/community/plugin/123456789/harmonyos-assistant
// 使用步骤:
// 1. 打开Figma,安装鸿蒙设计助手插件
// 2. 导入官方.fig模板文件
// 3. 在插件面板中选择需要的组件
// 4. 直接拖拽到设计画布中
2. Sketch模板使用
// Sketch插件:鸿蒙设计工具包
// 安装链接:https://github.com/harmonyos/sketch-harmonyos-toolkit
// 使用步骤:
// 1. 下载并安装插件
// 2. 打开官方.sketch模板文件
// 3. 使用插件中的Symbols库
// 4. 通过插件导出鸿蒙格式资源
三、设计资源转换与集成
- 图标资源转换脚本
icon_converter.py
import os
import xml.etree.ElementTree as ET
from svg_to_png import convert_svg_to_png
def generate_harmony_icons(svg_dir, output_dir):
“”"
将SVG图标转换为鸿蒙所需的PNG格式并生成XML资源文件
“”"
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 创建XML根元素
root = ET.Element("resources")
# 遍历SVG目录
for filename in os.listdir(svg_dir):
if filename.endswith(".svg"):
icon_name = os.path.splitext(filename)[0]
svg_path = os.path.join(svg_dir, filename)
# 转换为不同DPI的PNG
for dpi in ["mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]:
scale_factor = {
"mdpi": 1.0,
"hdpi": 1.5,
"xhdpi": 2.0,
"xxhdpi": 3.0,
"xxxhdpi": 4.0
}[dpi]
size = int(24 * scale_factor) # 基础尺寸24dp
png_dir = os.path.join(output_dir, dpi)
os.makedirs(png_dir, exist_ok=True)
png_path = os.path.join(png_dir, f"{icon_name}.png")
# 转换SVG到PNG
convert_svg_to_png(svg_path, png_path, size, size)
# 添加到XML资源
icon_element = ET.SubElement(root, "icon")
icon_element.set("name", icon_name)
icon_element.set("src", f"$media:{icon_name}")
# 保存XML文件
tree = ET.ElementTree(root)
tree.write(os.path.join(output_dir, "icons.xml"), encoding="utf-8", xml_declaration=True)
-
颜色资源集成
// resources/base/element/color.json
{
“color”: [
{
“name”: “color_primary”,
“value”: “#0A59F7”
},
{
“name”: “color_primary_dark”,
“value”: “#0038C8”
},
{
“name”: “color_secondary”,
“value”: “#FF5722”
},
{
“name”: “text_primary”,
“value”: “#333333”
},
{
“name”: “text_secondary”,
“value”: “#666666”
},
{
“name”: “background_primary”,
“value”: “#FFFFFF”
},
{
“name”: “background_secondary”,
“value”: “#F5F5F7”
}
]
} -
字体资源集成
// resources/base/element/font.json
{
“font”: [
{
“name”: “harmony_sans”,
“src”: “/base/fonts/HarmonyOS_Sans.ttf”
},
{
“name”: “harmony_sans_medium”,
“src”: “/base/fonts/HarmonyOS_Sans_Medium.ttf”
},
{
“name”: “harmony_sans_bold”,
“src”: “/base/fonts/HarmonyOS_Sans_Bold.ttf”
}
]
}
四、设计系统组件实现 -
按钮组件实现
// ButtonComponent.java
public class ButtonComponent extends Component {
private Text text;
private ShapeElement background;public ButtonComponent(Context context) {
super(context);
init();
}private void init() {
// 设置布局参数
setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
setPadding(24, 16, 24, 16);// 创建背景 background = new ShapeElement(); background.setRgbColor(getColor(ResourceTable.Color_color_primary)); background.setCornerRadius(8); setBackground(background); // 创建文本 text = new Text(getContext()); text.setTextSize(16); text.setTextColor(Color.WHITE); text.setTextFont(Font.DEFAULT_BOLD); text.setPadding(8, 0, 8, 0); addComponent(text); // 设置点击效果 setTouchEventListener((component, event) -> { if (event.getAction() == TouchEvent.PRIMARY_POINT_DOWN) { AnimatorProperty animator = new AnimatorProperty(); animator.setDuration(100).scaleX(0.98f).scaleY(0.98f); animator.apply(this); } else if (event.getAction() == TouchEvent.PRIMARY_POINT_UP) { AnimatorProperty animator = new AnimatorProperty(); animator.setDuration(100).scaleX(1.0f).scaleY(1.0f); animator.apply(this); } return false; });
}
public void setText(String buttonText) {
text.setText(buttonText);
}public void setType(int type) {
switch (type) {
case PRIMARY:
background.setRgbColor(getColor(ResourceTable.Color_color_primary));
text.setTextColor(Color.WHITE);
break;
case SECONDARY:
background.setRgbColor(Color.TRANSPARENT);
background.setStroke(2, getColor(ResourceTable.Color_color_primary));
text.setTextColor(getColor(ResourceTable.Color_color_primary));
break;
}
}public static final int PRIMARY = 0;
public static final int SECONDARY = 1;
} -
卡片组件实现
// CardComponent.java
public class CardComponent extends DirectionalLayout {
private Image image;
private Text title;
private Text description;public CardComponent(Context context) {
super(context);
init();
}private void init() {
setOrientation(Component.VERTICAL);
setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
setBackground(createCardBackground());
setPadding(16);
setMargin(8);// 图片区域 image = new Image(getContext()); image.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT); image.setHeight(200); image.setScaleMode(Image.ScaleMode.CENTER_CROP); image.setCornerRadius(8); addComponent(image); // 文本区域 DirectionalLayout textLayout = new DirectionalLayout(getContext()); textLayout.setOrientation(Component.VERTICAL); textLayout.setPadding(0, 16, 0, 0); title = new Text(getContext()); title.setTextSize(18); title.setTextFont(Font.DEFAULT_BOLD); title.setTextColor(getColor(ResourceTable.Color_text_primary)); title.setMaxTextLines(2); textLayout.addComponent(title); description = new Text(getContext()); description.setTextSize(14); description.setTextColor(getColor(ResourceTable.Color_text_secondary)); description.setMaxTextLines(3); description.setMargin(0, 8, 0, 0); textLayout.addComponent(description); addComponent(textLayout);
}
private ShapeElement createCardBackground() {
ShapeElement background = new ShapeElement();
background.setRgbColor(getColor(ResourceTable.Color_background_primary));
background.setCornerRadius(12);
background.setShadowRadius(8);
background.setShadowColor(0x33000000);
background.setShadowOffsetX(0);
background.setShadowOffsetY(4);
return background;
}public void setImage(int resId) {
image.setPixelMap(resId);
}public void setTitle(String text) {
title.setText(text);
}public void setDescription(String text) {
description.setText(text);
}
}
五、设计规范自动化检查 -
布局规范检查脚本
layout_validator.py
import json
from xml.dom import minidom
def validate_layout(file_path):
“”"
检查XML布局文件是否符合鸿蒙设计规范
“”"
doc = minidom.parse(file_path)
components = doc.getElementsByTagName(“Component”)
results = {
"errors": [],
"warnings": []
}
# 检查间距规范
for comp in components:
padding = comp.getAttribute("ohos:padding")
if padding:
paddings = [int(p) for p in padding.split(" ")]
if len(paddings) == 1:
if paddings[0] % 4 != 0:
results["warnings"].append(f"组件 {comp.getAttribute('ohos:id')} 的padding值不是4的倍数")
else:
for p in paddings:
if p % 4 != 0:
results["warnings"].append(f"组件 {comp.getAttribute('ohos:id')} 的padding值不是4的倍数")
# 检查字体使用
text_components = doc.getElementsByTagName("Text")
for text in text_components:
font_family = text.getAttribute("ohos:text_font")
if font_family and "harmony_sans" not in font_family:
results["errors"].append(f"文本组件使用了非标准字体: {font_family}")
# 检查颜色使用
for comp in components:
color_attrs = ["text_color", "background_element", "tint"]
for attr in color_attrs:
value = comp.getAttribute(f"ohos:{attr}")
if value and value.startswith("#") and not value.startswith("$color:"):
results["errors"].append(f"组件 {comp.getAttribute('ohos:id')} 使用了硬编码颜色值")
return results
- 资源命名规范检查
resource_validator.py
import os
import re
def validate_resource_naming(resource_dir):
“”"
检查资源文件命名是否符合鸿蒙规范
“”"
naming_patterns = {
“color”: r"^[a-z][a-z0-9_]$“,
“media”: r”^ic_[a-z0-9_]+$“,
“string”: r”^[a-z][a-z0-9_]$“,
“font”: r”^[a-z][a-z0-9_]*$"
}
results = {}
for res_type in naming_patterns:
res_dir = os.path.join(resource_dir, res_type)
if not os.path.exists(res_dir):
continue
pattern = naming_patterns[res_type]
for filename in os.listdir(res_dir):
name = os.path.splitext(filename)[0]
if not re.match(pattern, name):
results.setdefault(res_type, []).append(filename)
return results
六、设计资源自动化工作流
- 设计到开发工作流
- 设计师使用Figma/Sketch创建界面
- 导出设计资源(SVG图标、颜色JSON、字体等)
- 运行转换脚本生成鸿蒙资源
- 开发人员集成资源到项目
- 运行验证脚本确保符合规范
- 使用组件库实现界面
- CI/CD集成示例
.gitlab-ci.yml
stages:
- validate
- build
validate_design:
stage: validate
script:
- python scripts/layout_validator.py src/main/resources/base/layout
- python scripts/resource_validator.py src/main/resources/base
build_android:
stage: build
script:
- ./gradlew assembleRelease
only:
- master
七、设计系统主题切换
-
深色模式适配
// ThemeManager.java
public class ThemeManager {
private static ThemeManager instance;
private boolean isDarkMode = false;private ThemeManager() {}
public static ThemeManager getInstance() {
if (instance == null) {
instance = new ThemeManager();
}
return instance;
}public void setDarkMode(boolean darkMode) {
isDarkMode = darkMode;
applyTheme();
}private void applyTheme() {
// 获取所有Activity
List<Ability> abilities = AbilityManager.getInstance().getAllAbilities();
for (Ability ability : abilities) {
if (ability instanceof AbilitySlice) {
AbilitySlice slice = (AbilitySlice) ability;
updateComponents(slice.getComponent());
}
}
}private void updateComponents(Component component) {
if (component instanceof ComponentContainer) {
ComponentContainer container = (ComponentContainer) component;
for (Component child : container.getChildComponents()) {
updateComponents(child);
}
}// 更新组件样式 if (component instanceof Text) { Text text = (Text) component; if (isDarkMode) { text.setTextColor(getColor(ResourceTable.Color_text_primary_dark)); } else { text.setTextColor(getColor(ResourceTable.Color_text_primary)); } } // 更多组件样式更新...
}
} -
主题资源定义
// resources/dark/element/color.json
{
“color”: [
{
“name”: “text_primary”,
“value”: “#E0E0E0”
},
{
“name”: “text_secondary”,
“value”: “#9E9E9E”
},
{
“name”: “background_primary”,
“value”: “#121212”
},
{
“name”: “background_secondary”,
“value”: “#1E1E1E”
}
]
}
八、最佳实践与建议 -
设计资源管理
统一命名规范:遵循鸿蒙官方命名规则
版本控制:将设计资源纳入版本管理系统
资源分类:按功能模块组织资源文件
设计令牌:使用JSON定义设计系统变量 -
性能优化
使用矢量图标:优先使用SVG格式图标
资源压缩:使用工具优化图片资源
按需加载:动态加载非关键资源
资源缓存:实现资源缓存机制 -
团队协作
设计系统文档:维护设计系统文档
定期同步:设计开发定期沟通
组件库共享:建立共享组件库
自动化流程:减少手动操作
总结
鸿蒙5官方设计资源为开发者提供了:
标准化设计模板:Figma/Sketch模板文件
完整设计系统:包含颜色、字体、图标等资源
开发集成方案:资源转换脚本和组件实现
通过本文介绍的方法,您可以:
高效使用官方设计模板
自动化转换设计资源
实现规范化的组件库
确保设计开发一致性
实现主题切换功能
建议开发团队:
建立设计资源管理流程
实施自动化验证和转换
创建可复用的组件库
定期更新设计资源
遵循鸿蒙设计规范
通过合理利用官方设计资源和自动化工具,可以显著提高鸿蒙应用的开发效率和质量,确保应用在华为全场景设备上提供一致的用户体验。
