上一章我們講解了應(yīng)用編譯環(huán)境準(zhǔn)備,設(shè)備編譯環(huán)境準(zhǔn)備,開發(fā)板燒錄,將一個最簡單的 OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)程序安裝到我們的標(biāo)準(zhǔn)設(shè)備上。
本章是 OpenHarmony 標(biāo)準(zhǔn)設(shè)備應(yīng)用開發(fā)的第二篇文章。我們通過知識體系新開發(fā)的幾個基于 OpenHarmony3.1 Beta 標(biāo)準(zhǔn)系統(tǒng)的樣例:分布式音樂播放、傳炸彈、購物車等樣例,分別介紹下音樂播放、顯示動畫、動畫轉(zhuǎn)場(頁面間轉(zhuǎn)場)三個進(jìn)階技能。首先我們來講如何在 OpenHarmony 中實(shí)現(xiàn)音樂的播放。
分布式音樂播放
通過分布式音樂播放器,大家可以學(xué)到一些 ArkUI 組件和布局在 OpenHarmony 中是如何使用的,以及如何在自己的應(yīng)用中實(shí)現(xiàn)音樂的播放,暫停等相關(guān)功能。應(yīng)用效果如下圖所示:
1.1界面布局
首先是頁面整體布局,部分控件是以模塊的方式放在整體布局中的,如 operationPannel()、sliderPannel()、playPannel() 模塊。頁面整體布是由 Flex 控件中,包含 Image、Text 以及剛才所說的三個模塊所構(gòu)成。
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.End }) {
Image($r("app.media.icon_liuzhuan")).width(32).height(32)
}.padding({ right: 32 }).onClick(() => {
this.onDistributeDevice()
})
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center }) {
Image($r("app.media.Bg_classic")).width(312).height(312)
}.margin({ top: 24 })
Text(this.currentMusic.name).fontSize(20).fontColor("#e6000000").margin({ top: 10 })
Text("未知音樂家").fontSize(14).fontColor("#99000000").margin({ top: 10 })
}.flexGrow(1)
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) {
this.operationPannel()
this.sliderPannel()
this.playPannel()
}.height(200)
}
.linearGradient({
angle: 0,
direction: GradientDirection.Bottom,
colors: this.currentMusic.backgourdColor
}).padding({ top: 48, bottom: 24, left: 24, right: 24 })
.width('100%')
.height('100%')
}
operationPannel() 模塊的布局,該部分代碼對應(yīng)圖片中的心形圖標(biāo),下載圖標(biāo),評論圖標(biāo)更多圖標(biāo)這一部分布局。其主要是在 Flex 中包含 Image 所構(gòu)成代碼如下:
@Builder operationPannel() {
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Image($r("app.media.icon_music_like")).width(24).height(24)
Image($r("app.media.icon_music_download")).width(24).height(24)
Image($r("app.media.icon_music_comment")).width(24).height(24)
Image($r("app.media.icon_music_more")).width(24).height(24)
}.width('100%').height(49).padding({ bottom: 25 })
}
sliderPannel() 模塊代碼布局。該部分對應(yīng)圖片中的顯示播放時間那一欄的控件。整體構(gòu)成是在 Flex 中,包含 Text、Slider、Text 三個控件。具體代碼如下:
sliderPannel() {
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text(this.currentTimeText).fontSize(12).fontColor("ff000000").width(40)
Slider({
value: this.currentProgress,
min: 0,
max: 100,
step: 1,
style: SliderStyle.INSET
})
.blockColor(Color.White)
.trackColor(Color.Gray)
.selectedColor(Color.Blue)
.showSteps(true)
.flexGrow(1)
.margin({ left: 5, right: 5 })
.onChange((value: number, mode: SliderChangeMode) => {
if (mode == 2) {
CommonLog.info('aaaaaaaaaaaaaa1: ' + this.currentProgress)
this.onChangeMusicProgress(value, mode)
}
})
Text(this.totalTimeText).fontSize(12).fontColor("ff000000").width(40)
}.width('100%').height(18)
}
playPannel() 模塊代碼對應(yīng)圖片中的最底部播放那一欄五個圖標(biāo)所包含的一欄。整體布局是 Flex 方向?yàn)闄M向,其中包含五個 Image 所構(gòu)成。具體代碼如下:
playPannel() {
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Image($r("app.media.icon_music_changemode")).width(24).height(24).onClick(() => {
this.onChangePlayMode()
})
Image($r("app.media.icon_music_left")).width(32).height(32).onClick(() => {
this.onPreviousMusic()
})
Image(this.isPlaying ? $r("app.media.icon_music_play") : $r("app.media.icon_music_stop"))
.width(80)
.height(82)
.onClick(() => {
this.onPlayOrPauseMusic()
})
Image($r("app.media.icon_music_right")).width(32).height(32).onClick(() => {
this.onNextMusic()
})
Image($r("app.media.icon_music_list")).width(24).height(24).onClick(() => {
this.onShowMusicList()
})
}.width('100%').height(82)
}
希望通過上面這些布局的演示,可以讓大家學(xué)到一些部分控件在 OpenHarmony 中的運(yùn)用,這里使用的 Arkui 布局和 HarmonyOS* 是一致的,目前 HarmonyOS* 手機(jī)還沒有發(fā)布 Arkui 的版本,大家可以在 OpenHarmony 上搶先體驗(yàn)。常用的布局和控件還有很多,大家可以在下面的鏈接中找到更多的詳細(xì)信息。
*編者注:HarmonyOS 是基于開放原子開源基金會旗下開源項(xiàng)目 OpenHarmony 開發(fā)的面向多種全場景智能設(shè)備的商用版本。是結(jié)合其自有特性和能力開發(fā)的新一代智能終端操作系統(tǒng)。
1.2 播放音樂
play(seekTo) {
if (this.player.state == 'playing' && this.player.src == this.getCurrentMusic().url) {
return
}
if (this.player.state == 'idle' || this.player.src != this.getCurrentMusic().url) {
CommonLog.info('Preload music url = ' + this.getCurrentMusic().url)
this.player.reset()
this.player.src = this.getCurrentMusic().url
this.player.on('dataLoad', () => {
CommonLog.info('dataLoad duration=' + this.player.duration)
this.totalTimeMs = this.player.duration
if (seekTo > this.player.duration) {
seekTo = -1
}
this.player.on('play', (err, action) => {
if (err) {
CommonLog.info(`MusicPlayer[PlayerModel] error returned in play() callback`)
return
}
if (seekTo > 0) {
this.player.seek(seekTo)
}
})
this.player.play()
this.statusChangeListener()
this.setProgressTimer()
this.isPlaying = true
})
}
else {
if (seekTo > this.player.duration) {
seekTo = -1
}
this.player.on('play', (err, action) => {
if (err) {
CommonLog.info(`MusicPlayer[PlayerModel] error returned in play() callback`)
return
}
if (seekTo > 0) {
this.player.seek(seekTo)
}
})
this.player.play()
this.setProgressTimer()
this.isPlaying = true
}
}
1.3 音樂暫停
pause() {
CommonLog.info("pause music")
this.player.pause();
this.cancelProgressTimer()
this.isPlaying = false
}
接下來我們講解如何在 OpenHarmony 中實(shí)現(xiàn)顯示動畫的效果。
顯示動畫
我們以傳炸彈小游戲中的顯示動畫效果為例,效果如下圖所示。
通過本小節(jié),大家在上一小節(jié)的基礎(chǔ)上,學(xué)到更多 ArkUI 組件和布局在 OpenHarmony 中的應(yīng)用,以及如何在自己的應(yīng)用中實(shí)現(xiàn)顯示動畫的效果。
實(shí)現(xiàn)步驟:
2.1編寫彈窗布局:將游戲失敗文本、炸彈圖片和再來一局按鈕圖片放置于Column容器中;
2.2用變量來控制動畫起始和結(jié)束的位置:用Flex容器包裹炸彈圖片,并用 @State 裝飾變量 toggle,通過變量來動態(tài)修改 Flex 的 direction 屬性;布局代碼如下:
boolean = true toggle:
private controller: CustomDialogController
deviceList: RemoteDevice[]
private confirm: () => void
private interval = null
build() {
Column() {
Text('游戲失敗').fontSize(30).margin(20)
Flex({
direction: this.toggle ? FlexDirection.Column : FlexDirection.ColumnReverse,
alignItems: ItemAlign.Center
})
{
Image($r("app.media.bomb")).objectFit(ImageFit.Contain).height(80)
}.height(200)
Image($r("app.media.btn_restart")).objectFit(ImageFit.Contain).height(120).margin(10)
.onClick(() => {
this.controller.close()
this.confirm()
})
}
.width('80%')
.margin(50)
.backgroundColor(Color.White)
}
2.3設(shè)置動畫效果:使用 animateTo 顯式動畫接口炸彈位置切換時添加動畫,并且設(shè)置定時器定時執(zhí)行動畫,動畫代碼如下:
aboutToAppear() {
this.setBombAnimate()
}
setBombAnimate() {
let fun = () => {
this.toggle = !this.toggle;
}
this.interval = setInterval(() => {
animateTo({ duration: 1500, curve: Curve.Sharp }, fun)
}, 1600)
}
轉(zhuǎn)場動畫(頁面間轉(zhuǎn)場)
我們同樣希望在本小節(jié)中,可以讓大家看到更多的 ArkUI 中的組件和布局在 OpenHarmony 中的使用,如何模塊化的使用布局,讓自己的代碼更簡潔易讀,以及在應(yīng)用中實(shí)現(xiàn)頁面間的轉(zhuǎn)場動畫效果。
整體布局由 Column、Scroll、Flex、Image 以及 GoodsHome()、MyInfo()、HomeBottom() 構(gòu)成,該三個模塊我們會分別說明。具體代碼如下:
build() {
Column() {
Scroll() {
Column() {
if (this.currentPage == 1) {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.End }) {
Image($r("app.media.icon_share"))
.objectFit(ImageFit.Cover)
.height('60lpx')
.width('60lpx')
}
.width("100%")
.margin({ top: '20lpx', right: '50lpx' })
.onClick(() => {
this.playerDialog.open()
})
GoodsHome({ goodsItems: this.goodsItems})
}
else if (this.currentPage == 3) {
//我的
MyInfo()
}
}
.height('85%')
}
.flexGrow(1)
HomeBottom({ remoteData: this.remoteData})
}
.backgroundColor("white")
}
GoodsHome() 模塊對應(yīng)效果圖中間顯示商品的部分,其主要結(jié)構(gòu)為 TabContent 中包含的兩個 List 條目所構(gòu)成。具體代碼如下:
struct GoodsHome {
private goodsItems: GoodsData[]
pingCartsGoods :any[] Shop
build() {
Column() {
Tabs() {
TabContent() {
GoodsList({ goodsItems: this.goodsItems});
}
.tabBar("暢銷榜")
.backgroundColor(Color.White)
TabContent() {
GoodsList({ goodsItems: this.goodsItems});
}
.tabBar("推薦")
.backgroundColor(Color.White)
}
.barWidth(500)
.barHeight(50)
.scrollable(true)
.barMode(BarMode.Scrollable)
.height('980lpx')
}
.alignItems(HorizontalAlign.Start)
.width('100%')
}
}
上面代碼中的 GoodsList() 為每個 list 條目對應(yīng)顯示的信息,會便利集合中的數(shù)據(jù),然后顯示在對用的 item 布局中,具體代碼如下:
struct GoodsList {
private goodsItems: GoodsData[]
any[] ShoppingCartsGoods :
build() {
Column() {
List() {
ForEach(this.goodsItems, item => {
ListItem() {
GoodsListItem({ goodsItem: item})
}
}, item => item.id.toString())
}
.width('100%')
.align(Alignment.Top)
.margin({ top: '10lpx' })
}
}
}
最后就是 list 的 item 布局代碼。具體代碼如下:
struct GoodsListItem {
private goodsItem: GoodsData
1 scale: number =
1 opacity: number =
false active: boolean =
ShoppingCartsGoods :any[]
build() {
Column() {
Navigator({ target: 'pages/DetailPage' }) {
Row({ space: '40lpx' }) {
Column() {
Text(this.goodsItem.title)
.fontSize('28lpx')
Text(this.goodsItem.content)
.fontSize('20lpx')
Text('¥' + this.goodsItem.price)
.fontSize('28lpx')
.fontColor(Color.Red)
}
.height('160lpx')
.width('50%')
.margin({ left: '20lpx' })
.alignItems(HorizontalAlign.Start)
Image(this.goodsItem.imgSrc)
.objectFit(ImageFit.ScaleDown)
.height('160lpx')
.width('40%')
.renderMode(ImageRenderMode.Original)
.margin({ right: '20lpx', left: '20lpx' })
}
.height('180lpx')
.alignItems(VerticalAlign.Center)
.backgroundColor(Color.White)
}
.params({ goodsItem: this.goodsItem ,ShoppingCartsGoods:this.ShoppingCartsGoods})
.margin({ left: '40lpx' })
}
}
備注:MyInfo() 模塊對應(yīng)的事其它也免得布局,這里就不做說明。
最后我們來說一下,頁面間的頁面間的轉(zhuǎn)場動畫,其主要是通過在全局 pageTransition 方法內(nèi)配置頁面入場組件和頁面退場組件來自定義頁面轉(zhuǎn)場動效。具體代碼如下:
// 轉(zhuǎn)場動畫使用系統(tǒng)提供的多種默認(rèn)效果(平移、縮放、透明度等)
pageTransition() {
PageTransitionEnter({ duration: 1000 })
.slide(SlideEffect.Left)
PageTransitionExit({ duration: 1000 })
.slide(SlideEffect.Right)
}
}
通過上述講解,我們就在自己的代碼中實(shí)現(xiàn)音樂的播放,顯示動畫,頁面間轉(zhuǎn)場動畫等效果。在接下來的一章中,我們會講解如何在 OpenHarmony 通過分布式數(shù)據(jù)管理,實(shí)現(xiàn)設(shè)備之間數(shù)據(jù)如何同步刷新。
在接下來的一章中,我們將會講解分布式數(shù)據(jù)管理在 OpenHarmony 中如何實(shí)現(xiàn)多臺設(shè)備間的數(shù)據(jù)同步更新。
原文標(biāo)題:OpenHarmony標(biāo)準(zhǔn)設(shè)備應(yīng)用開發(fā)(二)——布局、動畫與音樂
文章出處:【微信公眾號:HarmonyOS官方合作社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
審核編輯:湯梓紅
-
顯示
+關(guān)注
關(guān)注
0文章
443瀏覽量
45228 -
播放
+關(guān)注
關(guān)注
0文章
13瀏覽量
13598 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3747瀏覽量
16587
原文標(biāo)題:OpenHarmony標(biāo)準(zhǔn)設(shè)備應(yīng)用開發(fā)(二)——布局、動畫與音樂
文章出處:【微信號:HarmonyOS_Community,微信公眾號:電子發(fā)燒友開源社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論