本章是 OpenHarmony 標準設備應用開發的第二篇文章。我們通過知識體系新開發的幾個基于 OpenHarmony3.1 Beta 標準系統的樣例:分布式音樂播放、傳炸彈、購物車等樣例,分別介紹下音樂播放、顯示動畫、動畫轉場(頁面間轉場)三個進階技能。首先我們來講如何在 OpenHarmony 中實現音樂的播放。
一、分布式音樂播放
通過分布式音樂播放器,大家可以學到一些 ArkUI 組件和布局在 OpenHarmony 中是如何使用的,以及如何在自己的應用中實現音樂的播放,暫停等相關功能。應用效果如下圖所示:
1.1 界面布局
整體布局效果如下圖所示:
首先是頁面整體布局,部分控件是以模塊的方式放在整體布局中的,如 operationPannel()、sliderPannel()、playPannel() 模塊。頁面整體布是由 Flex 控件中,包含 Image、Text 以及剛才所說的三個模塊所構成。
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() 模塊的布局,該部分代碼對應圖片中的心形圖標,下載圖標,評論圖標更多圖標這一部分布局。其主要是在 Flex 中包含 Image 所構成代碼如下:
@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() 模塊代碼布局。該部分對應圖片中的顯示播放時間那一欄的控件。整體構成是在 Flex 中,包含 Text、Slider、Text 三個控件。具體代碼如下:
@Builder 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() 模塊代碼對應圖片中的最底部播放那一欄五個圖標所包含的一欄。整體布局是 Flex 方向為橫向,其中包含五個 Image 所構成。具體代碼如下:
@Builder 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)}
希望通過上面這些布局的演示,可以讓大家學到一些部分控件在 OpenHarmony 中的運用,這里使用的 Arkui 布局和 HarmonyOS* 是一致的,目前 HarmonyOS* 手機還沒有發布 Arkui 的版本,大家可以在 OpenHarmony 上搶先體驗。常用的布局和控件還有很多,大家可以在下面的鏈接中找到更多的詳細信息。
*編者注:HarmonyOS 是基于開放原子開源基金會旗下開源項目 OpenHarmony 開發的面向多種全場景智能設備的商用版本。是結合其自有特性和能力開發的新一代智能終端操作系統。
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().urlthis.player.on('dataLoad', () => {CommonLog.info('dataLoad duration=' + this.player.duration)this.totalTimeMs = this.player.durationif (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 中實現顯示動畫的效果。
二、顯示動畫
我們以傳炸彈小游戲中的顯示動畫效果為例,效果如下圖所示。
通過本小節,大家在上一小節的基礎上,學到更多 ArkUI 組件和布局在 OpenHarmony 中的應用,以及如何在自己的應用中實現顯示動畫的效果。
實現步驟:
**2.1 編寫彈窗布局:**將游戲失敗文本、炸彈圖片和再來一局按鈕圖片放置于 Column 容器中;
**2.2 用變量來控制動畫起始和結束的位置:**用 Flex 容器包裹炸彈圖片,并用 @State 裝飾變量 toggle,通過變量來動態修改 Flex 的 direction 屬性;布局代碼如下:
@State toggle: boolean = true
private controller: CustomDialogController
@Consume deviceList: RemoteDevice[]
private confirm: () => void
private interval = nullbuild() {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 設置動畫效果:**使用 animateTo 顯式動畫接口炸彈位置切換時添加動畫,并且設置定時器定時執行動畫,動畫代碼如下:
aboutToAppear() {this.setBombAnimate()
}setBombAnimate() {let fun = () => {this.toggle = !this.toggle;}this.interval = setInterval(() => {animateTo({ duration: 1500, curve: Curve.Sharp }, fun)}, 1600)
}
三、轉場動畫(頁面間轉場)
我們同樣希望在本小節中,可以讓大家看到更多的 ArkUI 中的組件和布局在 OpenHarmony 中的使用,如何模塊化的使用布局,讓自己的代碼更簡潔易讀,以及在應用中實現頁面間的轉場動畫效果。
下圖是分布式購物車項目中的轉場動畫效果圖:
頁面布局效果圖:
整體布局由 Column、Scroll、Flex、Image 以及 GoodsHome()、MyInfo()、HomeBottom() 構成,該三個模塊我們會分別說明。具體代碼如下:
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() 模塊對應效果圖中間顯示商品的部分,其主要結構為 TabContent 中包含的兩個 List 條目所構成。具體代碼如下:
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")}
上面代碼中的 GoodsList() 為每個 list 條目對應顯示的信息,會便利集合中的數據,然后顯示在對用的 item 布局中,具體代碼如下:
@Component
struct GoodsList {private goodsItems: GoodsData[]@Consume ShoppingCartsGoods :any[]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 布局代碼。具體代碼如下:
@Component
struct GoodsListItem {private goodsItem: GoodsData@State scale: number = 1@State opacity: number = 1@State active: boolean = false@Consume 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() 模塊對應的事其它也免得布局,這里就不做說明。
最后我們來說一下,頁面間的頁面間的轉場動畫,其主要是通過在全局 pageTransition 方法內配置頁面入場組件和頁面退場組件來自定義頁面轉場動效。具體代碼如下:
// 轉場動畫使用系統提供的多種默認效果(平移、縮放、透明度等)pageTransition() {PageTransitionEnter({ duration: 1000 }).slide(SlideEffect.Left)PageTransitionExit({ duration: 1000 }).slide(SlideEffect.Right)}
}
為了幫助到大家能夠更有效的學習OpenHarmony 開發的內容,下面特別準備了一些相關的參考學習資料:
OpenHarmony 開發環境搭建:https://qr18.cn/CgxrRy
《OpenHarmony源碼解析》:https://qr18.cn/CgxrRy
- 搭建開發環境
- Windows 開發環境的搭建
- Ubuntu 開發環境搭建
- Linux 與 Windows 之間的文件共享
- ……
系統架構分析:https://qr18.cn/CgxrRy
- 構建子系統
- 啟動流程
- 子系統
- 分布式任務調度子系統
- 分布式通信子系統
- 驅動子系統
- ……