0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁時鐘

共熵服務(wù)中心 ? 來源:未知 ? 2022-12-05 20:15 ? 次閱讀
原文引自:51CTO 開源基礎(chǔ)軟件社區(qū) #夏日挑戰(zhàn)賽# OpenHarmony - 《ArkUI(TS)開發(fā)翻頁時鐘

63824d32-7496-11ed-8abf-dac502259ad0.png

1. 項(xiàng)目背景

翻頁時鐘(Flip Clock)是一種有趣的機(jī)電數(shù)字計時設(shè)備,用電腦動畫的方式實(shí)現(xiàn)翻頁時鐘,也是一種特別的復(fù)古UI交互體驗(yàn)。

本項(xiàng)目豈在通過OpenHarmony的ArkUI框架,用TS擴(kuò)展的聲明式開發(fā)范式eTS,來實(shí)現(xiàn)翻頁時鐘的體驗(yàn)。

本項(xiàng)目的開發(fā)環(huán)境如下:

  • 工具版本:DevEco Studio 3.0 Beta4

  • SDK版本:3.1.6.6(API Version 8 Release)

具體實(shí)現(xiàn)的效果是這樣的:

63a47eca-7496-11ed-8abf-dac502259ad0.gif

本項(xiàng)目的主要知識點(diǎn)如下:

  • UI狀態(tài):@Prop、@Link、@Watch

  • 形狀裁剪屬性:clip

  • 顯式動畫:animateTo

2. eTS開發(fā)范式基于eTS的聲明式開發(fā)范式的方舟開發(fā)框架是一套開發(fā)極簡、高性能、跨設(shè)備應(yīng)用的UI開發(fā)框架,支持開發(fā)者高效的構(gòu)建跨設(shè)備應(yīng)用UI界面。

使用基于eTS的聲明式開發(fā)范式的方舟開發(fā)框架,采用更接近自然語義的編程方式,讓開發(fā)者可以直觀地描述UI界面,不必關(guān)心框架如何實(shí)現(xiàn)UI繪制和渲染,實(shí)現(xiàn)極簡高效開發(fā)。開發(fā)框架不僅從組件、動效和狀態(tài)管理三個維度來提供UI能力,還提供了系統(tǒng)能力接口,實(shí)現(xiàn)系統(tǒng)能力的極簡調(diào)用。

63e5af44-7496-11ed-8abf-dac502259ad0.png

關(guān)于語法和概念詳細(xì)請直接看官網(wǎng)官方文檔地址:

https://docs.openharmony.cn/pages/v3.1/zh-cn/application-dev/ui/ui-ts-overview.md/

3. 實(shí)現(xiàn)思路時鐘翻頁效果,用到四個Text組件,使用堆疊容器Stack。底層:用到兩個裁剪過后的Text上下顯示;頂層:也是用兩個裁剪后的Text做動畫效果,進(jìn)行X軸角度旋轉(zhuǎn)。

3.1 裁剪Text

裁剪前:

63fc5e4c-7496-11ed-8abf-dac502259ad0.png

裁剪后:

64170030-7496-11ed-8abf-dac502259ad0.png

使用形狀裁剪屬性clip

裁剪Text上半部:從坐標(biāo)(0,0)往下裁剪,clip(new Rect({ width: this.width, height: this.height / 2 }))

裁剪Text下半部:從坐標(biāo)(0,height / 2)往下裁剪,clip(new Path().commands(this.bottomPath))

@Entry
@Component
struct Test {
  private width = 90
  private height = 110
  private fontSize = 70
  private defaultBgColor = '#ffe6e6e6'
  private borderRadius = 10


  // 下半部裁剪路徑
  private bottomPath = `M0 ${vp2px(this.height / 2)}
  L${vp2px(this.width)} ${vp2px(this.height / 2)}
  L${vp2px(this.width)} ${vp2px(this.height)}
  L0 ${vp2px(this.height)} Z`


  build() {
    Row() {


      Text('24')
        .width(this.width)
        .height(this.height)
        .fontColor(Color.Black)
        .fontSize(this.fontSize)
        .textAlign(TextAlign.Center)
        .borderRadius(this.borderRadius)
        .backgroundColor(this.defaultBgColor)
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      Text('25')
        .margin({left:20})
        .width(this.width)
        .height(this.height)
        .fontColor(Color.Black)
        .fontSize(this.fontSize)
        .textAlign(TextAlign.Center)
        .borderRadius(this.borderRadius)
        .backgroundColor(this.defaultBgColor)
        .clip(new Path().commands(this.bottomPath))


    }.width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
3.2放入堆疊容器

四個裁剪后的Text放入到堆疊容器中(代碼片段):

    Stack() {
      // 底層文字上部
      Text(this.newValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      // 底層文字下部
      Text(this.oldValue)
        ......
        .clip(new Path().commands(this.bottomPath))


      // 頂層文字上部動畫
      Text(this.oldValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))
        .rotate({ x: 1, centerY: '50%', angle: this.angleTop })


      // 頂層文字下部動畫
      Text(this.newValue)
        ......
        .margin({ top: 3 })
        .clip(new Path().commands(this.bottomPath))
        .rotate({ x: 1, centerY: '50%', angle: this.angleBottom })
    }
3.3使用顯式動畫

先頂層上部的動畫,上部旋轉(zhuǎn)角度從0到90停止,接下來執(zhí)行頂層下部的動畫,下部旋轉(zhuǎn)角度從-90到0停止,停止完后重置初始狀態(tài),上部旋轉(zhuǎn)角度 = 0、下部旋轉(zhuǎn)角度 = -90(代碼片段)

  /**
   * 啟動頂層文字上部動畫
   */
  startTopAnimate() {
    animateTo({
      duration: 400,
      onFinish: () => {
        this.startBottomAnimate()
        this.animateBgColor = '#ffededed'
      }
    }, () => {
      this.angleTop = 90
      this.animateBgColor = '#ffc5c5c5'
    })
  }


  /**
   * 啟動頂層文字下部動畫
   */
  startBottomAnimate() {
    animateTo({
      duration: 400,
      onFinish: () => {
        this.angleTop = 0
        this.angleBottom = -90
        this.animateBgColor = this.defaultBgColor
        this.oldValue = this.newValue
      }
    }, () => {
      this.angleBottom = 0
      this.animateBgColor = this.defaultBgColor
    })
  }
3.4組件封裝

翻頁邏輯封裝成組件,提供給外部調(diào)用,根據(jù)外部傳入的雙向數(shù)據(jù)綁定:newValue,監(jiān)聽數(shù)據(jù)變化,有變化則啟動翻頁動畫(代碼片段):

@Component
export struct FlipPage {
  // 頂層上部動畫角度
  @State angleTop: number = 0
  // 頂層下部動畫角度
  @State angleBottom: number = -90
  // 舊值
  @Prop oldValue: string
  // 新值,加入監(jiān)聽
  @Link @Watch('valueChange') newValue: string


  /**
   * 監(jiān)聽新值變化
   */
  valueChange() {
    if (this.oldValue === this.newValue) return
    this.startTopAnimate()
  }


  build() {
    Stack() {
      // 底層文字上部
      Text(this.newValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      // 底層文字下部
      Text(this.oldValue)
        ......
        .clip(new Path().commands(this.bottomPath))


      // 頂層文字上部動畫
      Text(this.oldValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))
        .rotate({ x: 1, centerY: '50%', angle: this.angleTop })


      // 頂層文字下部動畫
      Text(this.newValue)
        ......
        .margin({ top: 3 })
        .clip(new Path().commands(this.bottomPath))
        .rotate({ x: 1, centerY: '50%', angle: this.angleBottom })
    }
  }
  /**
  * 啟動頂層文字上部動畫
  */
  startTopAnimate() {
    ......
  }
3.5外部調(diào)用

界面加載成功后,開啟循環(huán)定時器setInterval、間隔1秒更新時間。更改newValue的值,翻頁組件內(nèi)部進(jìn)行動畫翻頁。

import { FlipPage } from '../componet/FlipPage'


@Entry
@Component
struct Index {
  // 小時-舊值
  @State oldHours: string = ''
  // 小時-新值
  @State newHours: string = ''
  // 分鐘-舊值
  @State oldMinutes: string = ''
  // 分鐘-新值
  @State newMinutes: string = ''
  // 秒數(shù)-舊值
  @State oldSeconds: string = ''
  // 秒數(shù)-新值
  @State newSeconds: string = ''


  @Builder Colon() {
    Column() {
      Circle().width(8).height(8).fill(Color.Black)
      Circle().width(8).height(8).fill(Color.Black).margin({ top: 10 })
    }.padding(10)
  }


  build() {
    Row() {
      // 翻頁組件-顯示小時
      FlipPage({ oldValue: this.oldHours, newValue: $newHours })
      // 冒號
      this.Colon()
      // 翻頁組件-顯示分鐘
      FlipPage({ oldValue: this.oldMinutes, newValue: $newMinutes })
      // 冒號
      this.Colon()
      // 翻頁組件-顯示秒數(shù)
      FlipPage({ oldValue: this.oldSeconds, newValue: $newSeconds })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
    .onAppear(() => {
      // 開啟定時器
      this.initDate()
      setInterval(() => {
        this.updateDate()
      }, 1000)
    })
  }


  /**
   * 初始化時間
   */
  initDate() {
    let date = new Date()
    // 設(shè)置小時
    this.oldHours = this.format(date.getHours())
    // 設(shè)置分鐘
    this.oldMinutes = this.format(date.getMinutes())
    // 設(shè)置秒數(shù)
    this.oldSeconds = this.format(date.getSeconds())
    // 設(shè)置新的秒數(shù)
    this.newSeconds = date.getSeconds() + 1 === 60 ? '00' : this.format(date.getSeconds() + 1)
  }


  /**
   * 更新時間
   */
  updateDate() {
    let date = new Date()
    console.log(`${date.getHours()}${date.getMinutes()}${date.getSeconds()}秒`)
    // 當(dāng)新值改變,才有動畫
    if (date.getSeconds() === 59) {
      this.newSeconds = '00'
      this.newMinutes = date.getMinutes() + 1 === 60 ? '00' : this.format(date.getMinutes() + 1)
      if (date.getMinutes() === 59) {
        this.newHours = date.getHours() + 1 === 24 ? '00' : this.format(date.getHours() + 1)
      }
    } else {
      this.newSeconds = this.format(date.getSeconds() + 1)
    }
  }


  /**
   * 不足十位前面補(bǔ)零
   */
  format(param) {
    let value = '' + param
    if (param < 10) {
      value = '0' + param
    }
    return value
  }
}
4.總結(jié)根據(jù)上面的實(shí)現(xiàn)思路和5個步驟流程,相信你也掌握了翻頁時鐘原理,拆分成一步一步還是很簡單的,最主要還是對API的熟悉和聲明式語法的掌握。HarmonyOS的API是根據(jù)OpenHarmony去更新的,兩者區(qū)別語法都一樣,只是OpenHarmony的API比較新,功能比較完善和成熟的,所以本項(xiàng)目直接使用OpenHarmony SDK開發(fā)。645d457c-7496-11ed-8abf-dac502259ad0.gif 本文完寫在最后我們最近正帶著大家玩嗨OpenHarmony。如果你有好玩的東東,歡迎投稿,讓我們一起嗨起來!有點(diǎn)子,有想法,有Demo,立刻聯(lián)系我們:合作郵箱:zzliang@atomsource.org
646c344c-7496-11ed-8abf-dac502259ad0.gif

6497e1fa-7496-11ed-8abf-dac502259ad0.png

64cde110-7496-11ed-8abf-dac502259ad0.png64de3970-7496-11ed-8abf-dac502259ad0.png6501ab08-7496-11ed-8abf-dac502259ad0.png

650cc628-7496-11ed-8abf-dac502259ad0.png

652c6190-7496-11ed-8abf-dac502259ad0.png

654d7074-7496-11ed-8abf-dac502259ad0.png

65721000-7496-11ed-8abf-dac502259ad0.png

65a1d57e-7496-11ed-8abf-dac502259ad0.png

65ca526a-7496-11ed-8abf-dac502259ad0.png

663c7336-7496-11ed-8abf-dac502259ad0.png

66afe622-7496-11ed-8abf-dac502259ad0.png

66c4d104-7496-11ed-8abf-dac502259ad0.png

66d3bbba-7496-11ed-8abf-dac502259ad0.png

66fd970a-7496-11ed-8abf-dac502259ad0.png

671687ba-7496-11ed-8abf-dac502259ad0.png

672c55fe-7496-11ed-8abf-dac502259ad0.png

674d70ea-7496-11ed-8abf-dac502259ad0.png

676f6902-7496-11ed-8abf-dac502259ad0.png

6781e2b2-7496-11ed-8abf-dac502259ad0.png


原文標(biāo)題:玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁時鐘

文章出處:【微信公眾號:開源技術(shù)服務(wù)中心】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 開源技術(shù)
    +關(guān)注

    關(guān)注

    0

    文章

    389

    瀏覽量

    7891
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3611

    瀏覽量

    15964

原文標(biāo)題:玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁時鐘

文章出處:【微信號:開源技術(shù)服務(wù)中心,微信公眾號:共熵服務(wù)中心】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    第三屆OpenHarmony技術(shù)大會星光璀璨、致謝OpenHarmony社區(qū)貢獻(xiàn)者

    10月12日,在上海舉辦的第三屆OpenHarmony技術(shù)大會上,32家高校OpenHarmony技術(shù)俱樂部璀璨亮相,30家高校OpenHarmony開發(fā)者協(xié)會盛大啟幕。還分別致謝了年度星光TSG
    的頭像 發(fā)表于 10-21 14:10 ?83次閱讀

    基于ArkTS語言的OpenHarmony APP應(yīng)用開發(fā):HelloOpenharmony

    1、程序簡介該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)編寫的UI應(yīng)用類:HelloOpenHarmony。本案例是基于API9接口開發(fā)。本案例已在OpenHarmony凌蒙派-RK3568開發(fā)
    的頭像 發(fā)表于 09-15 08:09 ?215次閱讀
    基于ArkTS語言的<b class='flag-5'>OpenHarmony</b> APP應(yīng)用開發(fā):Hello<b class='flag-5'>Openharmony</b>

    基于ArkTS語言的OpenHarmony APP應(yīng)用開發(fā):HelloOpenharmony

    1、程序簡介 該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)編寫的UI應(yīng)用類:HelloOpenHarmony。 本案例是基于API 9接口開發(fā)。 本案例已在OpenHarmony凌蒙派-RK3568
    發(fā)表于 09-14 12:47

    鴻蒙開發(fā)ArkUI-X基礎(chǔ)知識:【ArkUI代碼工程及構(gòu)建介紹】

    ArkUI作為OpenHarmony的默認(rèn)開發(fā)框架,在本項(xiàng)目(ArkUI-X)中需要做到一套代碼同時支持多平臺構(gòu)建,所以會采取共倉開發(fā)的方式,部分倉直接指向OpenHarmony相關(guān)開
    的頭像 發(fā)表于 05-25 16:45 ?1929次閱讀
    鴻蒙開發(fā)<b class='flag-5'>ArkUI</b>-X基礎(chǔ)知識:【<b class='flag-5'>ArkUI</b>代碼工程及構(gòu)建介紹】

    鴻蒙開發(fā)學(xué)習(xí):【OpenHarmony HAR】

    OpenHarmony js/ts三方庫使用的是OpenHarmony靜態(tài)共享包,即HAR(Harmony Archive),可以包含js/ts代碼、c++庫、資源和配置文件。通過HAR,可以實(shí)現(xiàn)
    的頭像 發(fā)表于 03-18 16:27 ?609次閱讀

    淺談兼容 OpenHarmony 的 Flutter

    OpenHarmony SIG 組織在 Gitee 開源了兼容 OpenHarmony 的 Flutter。該組織主要用于孵化 OpenHarmony 相關(guān)的開源生態(tài)項(xiàng)目。 ? ? ▲ 倉庫地址
    的頭像 發(fā)表于 02-02 15:22 ?534次閱讀
    淺談兼容 <b class='flag-5'>OpenHarmony</b> 的 Flutter

    鴻蒙開發(fā)OpenHarmony組件復(fù)用案例

    和響應(yīng)速度。 在OpenHarmony應(yīng)用開發(fā)時,自定義組件被@Reusable裝飾器修飾時表示該自定義組件可以復(fù)用。在父自定義組件下創(chuàng)建的可復(fù)用組件從組件樹上移除后,會被加入父自定義組件的可復(fù)用節(jié)點(diǎn)
    發(fā)表于 01-15 17:37

    openharmony開發(fā)應(yīng)用

    隨著智能設(shè)備的普及和多樣化,開發(fā)者們對于更加靈活、高效的操作系統(tǒng)需求與日俱增。在這個背景下,華為推出了OpenHarmony,一個全場景智能終端操作系統(tǒng)和生態(tài)平臺。本文將詳細(xì)探討
    的頭像 發(fā)表于 12-19 09:42 ?583次閱讀

    OpenHarmony亮相MTSC 2023 | 質(zhì)量&amp;效率共進(jìn),賦能應(yīng)用生態(tài)發(fā)展

    的保障策略等內(nèi)容。 OpenHarmony 兼容性工作組成員任熠分享了《OpenHarmony ArkUI-X 跨平臺應(yīng)用自動化測試能力實(shí)踐》,ArkUI-X 跨平臺在 2023 年
    發(fā)表于 11-28 15:41

    OpenHarmony之NAPI框架介紹

    戶調(diào)用了 NAPI 接口 napi_create_int64(), 對于 Nodejs 而言,它會去訪問 V8 引擎的 api 創(chuàng)建一個 js 的數(shù)字變量,而對于 OpenHarmony,則是去訪問 ArkUI 框架
    發(fā)表于 11-23 15:36

    OpenHarmony技術(shù)大會 | OpenHarmony技術(shù)俱樂部分論壇嘉賓金句

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個人的 OpenHarmony 原文標(biāo)題:OpenHarmony技術(shù)大會 | OpenHarmony技術(shù)俱樂部分論壇嘉賓金
    的頭像 發(fā)表于 11-10 20:25 ?421次閱讀

    OpenHarmony 4.0 Release版本發(fā)布,邀您體驗(yàn)

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個人的 OpenHarmony OpenHarmony 4.0 Release版本如期而至,開發(fā)套件同步升級到API 10。相比3.2
    的頭像 發(fā)表于 11-10 20:15 ?476次閱讀

    【開源三方庫】Easyui:基于OpenAtom OpenHarmony ArkUI深度定制的組件框架

    量、可靠的移動端組件庫,它是對OpenAtom OpenHarmony(以下簡稱“OpenHarmony”) ArkUI進(jìn)行深度定制的組件框架。Easyui可擴(kuò)展性較強(qiáng),可以基于源碼進(jìn)行二次開發(fā),修改
    的頭像 發(fā)表于 11-09 10:55 ?624次閱讀
    【開源三方庫】Easyui:基于OpenAtom <b class='flag-5'>OpenHarmony</b> <b class='flag-5'>ArkUI</b>深度定制的組件框架

    OpenHarmony社區(qū)運(yùn)營報告(2023年10月)

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個人的 OpenHarmony ● 截至2023年10月,OpenHarmony社區(qū)共有51家共建單位,累計超過6200名貢獻(xiàn)者產(chǎn)生
    的頭像 發(fā)表于 11-07 21:15 ?784次閱讀
    <b class='flag-5'>OpenHarmony</b>社區(qū)運(yùn)營報告(2023年10月)

    議程直擊 | 第二屆OpenHarmony技術(shù)大會——OpenHarmony技術(shù)俱樂部分論壇

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個人的 OpenHarmony 原文標(biāo)題:議程直擊 | 第二屆OpenHarmony技術(shù)大會——OpenHarmony技術(shù)
    的頭像 發(fā)表于 11-01 09:25 ?399次閱讀
    議程直擊 | 第二屆<b class='flag-5'>OpenHarmony</b>技術(shù)大會——<b class='flag-5'>OpenHarmony</b>技術(shù)俱樂部分論壇