應(yīng)用場景簡介
- 智能家居。
今天打造的這一款全新智能家庭控制系統(tǒng),凸顯應(yīng)用在智能控制和用戶體驗的特點,開創(chuàng)國內(nèi)智能家居系統(tǒng)體驗新局面。新的系統(tǒng)主要應(yīng)用在鴻蒙生態(tài)。
工程版本
- 系統(tǒng)版本/API版本:OpenHarmony SDK API 8
- IDE版本:DevEco Studio 3.0 Beta4
快速上手
準(zhǔn)備硬件環(huán)境
- [獲取OpenHarmony系統(tǒng)版本]:標(biāo)準(zhǔn)系統(tǒng)解決方案(二進(jìn)制)
- [搭建標(biāo)準(zhǔn)系統(tǒng)環(huán)境]
- [完成Dayu200開發(fā)板的燒錄]
準(zhǔn)備開發(fā)環(huán)境
- 安裝最新版[DevEco Studio]。
- 請參考[配置OpenHarmony SDK],完成DevEco Studio的安裝和開發(fā)環(huán)境配置。
- 開發(fā)環(huán)境配置完成后,請參考[使用工程向?qū) 創(chuàng)建工程(模板選擇“Empty Ability”),選擇eTS語言開發(fā)。
- 工程創(chuàng)建完成后,選擇使用[真機(jī)進(jìn)行調(diào)測]。
HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿
準(zhǔn)備工程
工程下載
git clone https://gitee.com/openharmony-sig/knowledge_demo_smart_home.git --depth=1
工程導(dǎo)入
- DevEco Studio導(dǎo)入本工程;
打開DevEco Studio,點擊File->Open->下載路徑/FA/SmartHomeCenter
編譯
- 點擊File > Project Structure > Project > Signing Configs界面勾選“ Automatically generate signing ”,等待自動簽名完成即可,點擊“ OK ”。如下圖所示:
- 點擊Build->Build Hap/APPs 編譯,編譯成功生成entry-debug-rich-signed.hap
燒錄/安裝
- 識別到設(shè)備后點擊,或使用默認(rèn)快捷鍵Shift+F10(macOS為Control+R)運行應(yīng)用。
- [安裝應(yīng)用]如果IDE沒有識別到設(shè)備就需要通過命令安裝,如下
打開OpenHarmony SDK路徑 toolchains 文件夾下,執(zhí)行如下hdc_std命令,其中path為hap包所在絕對路徑。hdc_std install -r pathentry-debug-rich-signed.hap//安裝的hap包需為xxx-signed.hap,即安裝攜帶簽名信息的hap包。
好的接下來我將詳細(xì)講解如何制作
開發(fā)教學(xué)
創(chuàng)建好的 eTS工程目錄
新建工程的ETS目錄如下圖所示。
各個文件夾和文件的作用:
- index.ets :用于描述UI布局、樣式、事件交互和頁面邏輯。
- app.ets :用于全局應(yīng)用邏輯和應(yīng)用生命周期管理。
- pages :用于存放所有組件頁面。
- resources :用于存放資源配置文件。
接下來開始正文。
我們的主要操作都是在在pages目錄中,然后我將用不到10分鐘的時間,帶大家實現(xiàn)這個功能。
鴻蒙開發(fā)參考指導(dǎo)文件
鴻蒙開發(fā)指導(dǎo)文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
拆解
根據(jù)設(shè)計圖,我們可以分層展示,用Column包裹,大致分為這幾步
可以看下本頁的結(jié)構(gòu):
再詳細(xì)一點:
import { SettingDetails } from './common/SettingDetails';
import router from '@ohos.router';
@Entry
@Component
struct Index {
@State title: string = '智能家居體驗'
@State message: string = '你現(xiàn)在想要打開那些設(shè)置?'
@State desc: string = '點擊所有適用的選項。這將幫助我們n自定義您的主頁'
@State Number: String[] = ['0', '1', '2', '3', '4']
@State private isSelect: boolean = true;
build() {
Column() {
Text(this.title)
.fontSize(80)
.fontWeight(FontWeight.Bold).onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
}).margin({ bottom: 60, top: 40 })
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold).onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
}).margin({ bottom: 60 })
Text(this.desc)
.fontSize(30)
.textAlign(TextAlign.Center)
.fontWeight(FontWeight.Bold)
.onClick(() = > {
})
.margin({ bottom: 60 })
Row() {
SettingDetails({
image: "common/images/setting.png",
title: "MaintenancenRequests",
isSelected: this.isSelect!
})
SettingDetails({ image: "common/images/grain.png", title: "Integrationsn", isSelected: this.isSelect! })
SettingDetails({
image: "common/images/ic_highlight.png",
title: "LightnControl",
isSelected: this.isSelect!
})
}
Row() {
SettingDetails({ image: "common/images/opacity.png", title: "LeaknDetector", isSelected: this.isSelect! })
SettingDetails({
image: "common/images/ac_unit.png",
title: "TemperaturenControl",
isSelected: this.isSelect!
})
SettingDetails({ image: "common/images/key.png", title: "GuestnAccess", isSelected: this.isSelect! })
}
Button("NEXT")
.fontSize(60)
.fontColor(Color.Black)
.width(600)
.height(100)
.backgroundColor(Color.Red)
.margin({ top: 100 })
.onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
})
}
.width('100%')
.height('100%').backgroundColor("#F5F5F5")
}
}
具體布局
具體布局設(shè)計到一些細(xì)節(jié)的地方,例如間隔,邊框,當(dāng)前組件尺寸設(shè)置等一些特殊情況,基本上就是嵌套,一層一層去實現(xiàn)。
代碼結(jié)構(gòu)
編碼
Index.ets
import { SettingDetails } from './common/SettingDetails';
import router from '@ohos.router';
@Entry
@Component
struct Index {
@State title: string = '智能家居體驗'
@State message: string = '你現(xiàn)在想要打開那些設(shè)置?'
@State desc: string = '點擊所有適用的選項。這將幫助我們n自定義您的主頁'
@State Number: String[] = ['0', '1', '2', '3', '4']
@State private isSelect: boolean = true;
build() {
Column() {
Text(this.title)
.fontSize(80)
.fontWeight(FontWeight.Bold).onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
}).margin({ bottom: 60, top: 40 })
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold).onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
}).margin({ bottom: 60 })
Text(this.desc)
.fontSize(30)
.textAlign(TextAlign.Center)
.fontWeight(FontWeight.Bold)
.onClick(() = > {
})
.margin({ bottom: 60 })
Row() {
SettingDetails({
image: "common/images/setting.png",
title: "MaintenancenRequests",
isSelected: this.isSelect!
})
SettingDetails({ image: "common/images/grain.png", title: "Integrationsn", isSelected: this.isSelect! })
SettingDetails({
image: "common/images/ic_highlight.png",
title: "LightnControl",
isSelected: this.isSelect!
})
}
Row() {
SettingDetails({ image: "common/images/opacity.png", title: "LeaknDetector", isSelected: this.isSelect! })
SettingDetails({
image: "common/images/ac_unit.png",
title: "TemperaturenControl",
isSelected: this.isSelect!
})
SettingDetails({ image: "common/images/key.png", title: "GuestnAccess", isSelected: this.isSelect! })
}
Button("NEXT")
.fontSize(60)
.fontColor(Color.Black)
.width(600)
.height(100)
.backgroundColor(Color.Red)
.margin({ top: 100 })
.onClick(() = > {
router.push({ url: 'pages/SensorScreen' })
})
}
.width('100%')
.height('100%').backgroundColor("#F5F5F5")
}
}
針對這一頁:首先是頭部
代碼如下:
Row() {
Image($r("app.media.back"))
.objectFit(ImageFit.Contain)
.width(80)
.height(80)
.onClick(() = > {
router.back()
})
Blank()
Text('Home')
.fontSize(45)
.fontWeight(FontWeight.Bold)
Blank()
Image($r("app.media.notifications_none"))
.objectFit(ImageFit.Contain)
.width(80)
.height(80)
.onClick(() = > {
router.back()
})
}
.width('100%')
其次是個人信息,包括頭像等信息:
代碼如下:
Row() {
Image($r("app.media.logo"))//頭像
.objectFit(ImageFit.Contain)
.width(200)
.height(200)
.borderRadius(21)
Column() {
Text('June 14, 2022')
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('Good Morning,nJianGuo',)
.fontSize(60)
.fontWeight(FontWeight.Bold)
}
}
接下來就是溫度和濕度
代碼如下:
ow({ space: 120 }) {
Column() {
Text('40°',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('TEMPERATURE',)
.fontSize(40).opacity(0.4)
}.margin({ left: 60 })
Column() {
Text('59%',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('HUMIDITY',)
.fontSize(40).opacity(0.4)
}.margin({ right: 60 })
}.margin({ top: 20 })
SensorScreen.ets
import { HomeDetails } from './common/homedetails';
// second.ets
import router from '@ohos.router';
@Entry
@Component
struct Second {
@State message: string = 'Hi there'
@State private isSelect: boolean = true;
build() {
Column() {
Row() {
Image($r("app.media.back"))
.objectFit(ImageFit.Contain)
.width(80)
.height(80)
.onClick(() = > {
router.back()
})
Blank()
Text('Home')
.fontSize(45)
.fontWeight(FontWeight.Bold)
Blank()
Image($r("app.media.notifications_none"))
.objectFit(ImageFit.Contain)
.width(80)
.height(80)
.onClick(() = > {
router.back()
})
}
.width('100%')
Row() {
Image($r("app.media.logo"))
.objectFit(ImageFit.Contain)
.width(200)
.height(200)
.borderRadius(21)
Column() {
Text('June 14, 2022')
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('Good Morning,nJianGuo',)
.fontSize(60)
.fontWeight(FontWeight.Bold)
}
}
Row({ space: 120 }) {
Column() {
Text('40°',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('TEMPERATURE',)
.fontSize(40).opacity(0.4)
}.margin({ left: 60 })
Column() {
Text('59%',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('HUMIDITY',)
.fontSize(40).opacity(0.4)
}.margin({ right: 60 })
}.margin({ top: 20 })
Row() {
HomeDetails({})
HomeDetails({ image: "common/images/lightbull.png", isSelected: this.isSelect! })
}
Row() {
HomeDetails({ image: "common/images/opacity.png" })
HomeDetails({ image: "common/images/yytem0.png" })
}
Row(){
Column(){
Text('ADD',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('NEW CONTROL',)
.fontSize(40).opacity(0.4)
}
Blank()
Image($r("app.media.add"))
.objectFit(ImageFit.Contain)
.width(100)
.height(100)
.borderRadius(21).margin({right:40})
}.border({
color:Color.White,
width:8,
radius:20
}).width("88%").height(150)
}.width("100%")
.height('100%').backgroundColor("#F5F5F5")
}
}
我們可以對下面的這塊進(jìn)行封裝
代碼如下
@Entry
@Component
export struct SettingDetails {
@State private image: string = "common/images/setting.png"
@State private title: string = "MaintenancenRequests"
@State private isSelected: boolean = true;
build() {
Column() {
Image(this.image)
.objectFit(ImageFit.Contain)
.width(140)
.height(120)
.margin(20)
.border({
width: 12, color: this.isSelected ? Color.White : Color.Red,
radius: 20
})
.onClick(() = > {
this.isSelected = !this.isSelected;
})
Text(this.title).fontSize(32).width(200).textAlign(TextAlign.Center)
}
}}
我們可以對,下面的這塊進(jìn)行封裝
代碼如下
@Entry
@Component
export struct SettingDetails {
@State private image: string = "common/images/setting.png"
@State private title: string = "MaintenancenRequests"
@State private isSelected: boolean = true;
build() {
Column() {
Image(this.image)
.objectFit(ImageFit.Contain)
.width(140)
.height(120)
.margin(20)
.border({
width: 12, color: this.isSelected ? Color.White : Color.Red,
radius: 20
})
.onClick(() = > {
this.isSelected = !this.isSelected;
})
Text(this.title).fontSize(32).width(200).textAlign(TextAlign.Center)
}
}}
最后就是底部
代碼如下:
Row(){
Column(){
Text('ADD',)
.fontSize(40).opacity(0.4)
.fontWeight(FontWeight.Bold)
Text('NEW CONTROL',)
.fontSize(40).opacity(0.4)
}
Blank()
Image($r("app.media.add"))
.objectFit(ImageFit.Contain)
.width(100)
.height(100)
.borderRadius(21).margin({right:40})
}.border({
color:Color.White,
width:8,
radius:20
}).width("88%").height(150)
審核編輯 黃宇
-
智能家居
+關(guān)注
關(guān)注
1928文章
9562瀏覽量
185118 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2352瀏覽量
42858 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3722瀏覽量
16317
發(fā)布評論請先 登錄
相關(guān)推薦
評論