<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

鴻蒙HarmonyOS開發實例:【簡單時鐘】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-10 09:48 ? 次閱讀

簡單時鐘

介紹

本示例通過使用[@ohos.display]接口以及Canvas組件來實現一個簡單的時鐘應用。

效果預覽

image.png

使用說明

1.界面通過setInterval實現周期性實時刷新時間,使用Canvas繪制時鐘,指針旋轉角度通過計算得出。

例如:"2 * Math.PI / 60 * second"是秒針旋轉的角度。

鴻蒙開發應用知識已更新[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]參考前往。

搜狗高速瀏覽器截圖20240326151547.png

具體實現

鴻蒙學習文檔前往mau123789是v添加即可
  • 本示例展示簡單時鐘的方法主要封裝在Index中,源碼參考:[Index.ets] 。
/*

 * Copyright (c) 2022 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

import display from '@ohos.display'

import Logger from '../model/Logger'



const HOURS: Array< string > = ['3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '1', '2']

const HEIGHT_ADD: number = 150 // 表盤下面需要繪制時間,canvas高度是寬度加150

const TAG: string = 'Index'



@Entry

@Component

struct Clock {

  private settings: RenderingContextSettings = new RenderingContextSettings(true)

  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  @State canvasWidth: number = 300 // 300是表盤默認大小

  private radius: number = 150 // 默認表盤半徑

  private intervalId: number = 0

  updateTime = () = > {

    this.context.clearRect(0, 0, this.canvasWidth, this.canvasWidth + HEIGHT_ADD)

    let nowTime = new Date()

    let hour = nowTime.getHours()

    let minute = nowTime.getMinutes()

    let second = nowTime.getSeconds()

    let time = `${this.fillTime(hour)}:${this.fillTime(minute)}:${this.fillTime(second)}`

    this.drawBackGround()

    this.drawHour(hour, minute)

    this.drawMinute(minute)

    this.drawSecond(second)

    this.drawDot()

    this.drawTime(time)

    this.context.translate(-this.radius, -this.radius)

  }



  fillTime(time: number) {

    return time < 10 ? `0${time}` : `${time}`

  }



  aboutToAppear() {

    this.getSize()

  }



  // 獲取設備寬高計算表盤大小

  async getSize() {

    let mDisplay = await display.getDefaultDisplay()

    Logger.info(TAG, `getDefaultDisplay mDisplay = ${JSON.stringify(mDisplay)}`)

    this.canvasWidth = px2vp(mDisplay.width > mDisplay.height ? mDisplay.height * 0.6 : mDisplay.width * 0.6)

    this.radius = this.canvasWidth / 2

  }



  drawBackGround() {



    // 繪制背景

    let grad = this.context.createRadialGradient(this.radius, this.radius, this.radius - 32, this.radius,

      this.radius, this.radius)

    grad.addColorStop(0.0, 'white')

    grad.addColorStop(0.9, '#eee')

    grad.addColorStop(1.0, 'white')

    this.context.fillStyle = grad

    this.context.fillRect(0, 0, this.canvasWidth, this.canvasWidth)



    // 繪制外圈圓

    this.context.translate(this.radius, this.radius)

    this.context.lineWidth = 6

    this.context.beginPath()

    this.context.strokeStyle = '#fff'

    this.context.arc(0, 0, this.radius - 5, 0, 2 * Math.PI, false)

    this.context.stroke()



    // 繪制時間文字

    this.context.font = '30px'

    this.context.textAlign = "center"

    this.context.textBaseline = "middle"

    this.context.fillStyle = '#000'

    this.context.save()

    HOURS.forEach((num, index) = > {

      let rad = 2 * Math.PI / 12 * index

      let x = Math.cos(rad) * (this.radius - 38)

      let y = Math.sin(rad) * (this.radius - 38)

      this.context.fillText(num, x, y)

    })

    this.context.restore()



    // 繪制刻度

    for (let i = 0; i < 60; i++) {

      let rad = 2 * Math.PI / 60 * i

      let x = Math.cos(rad) * (this.radius - 12)

      let y = Math.sin(rad) * (this.radius - 12)

      this.context.beginPath()

      this.context.moveTo(x, y)

      if (i % 5 == 0) {

        let x1 = Math.cos(rad) * (this.radius - 20)

        let y1 = Math.sin(rad) * (this.radius - 20)

        this.context.strokeStyle = '#000'

        this.context.lineWidth = 2

        this.context.lineTo(x1, y1)

      } else {

        let x1 = Math.cos(rad) * (this.radius - 18)

        let y1 = Math.sin(rad) * (this.radius - 18)

        this.context.strokeStyle = '#ccc'

        this.context.lineWidth = 1

        this.context.lineTo(x1, y1)

      }

      this.context.stroke()

    }

    this.context.restore()

  }



  // 繪制時針

  drawHour(hour: number, minute: number) {

    this.context.save()

    this.context.beginPath()

    this.context.lineWidth = 8

    this.context.lineCap = 'round'

    let rad = 2 * Math.PI / 12 * hour

    let mrad = 2 * Math.PI / 12 / 60 * minute

    this.context.rotate(rad + mrad)

    this.context.moveTo(0, 10)

    this.context.strokeStyle = '#000'

    this.context.lineTo(0, -this.radius / 2)

    this.context.stroke()

    this.context.restore()

  }



  // 繪制分針

  drawMinute(minute: number) {

    this.context.save()

    this.context.beginPath()

    this.context.lineWidth = 5

    this.context.lineCap = 'round'

    let rad = 2 * Math.PI / 60 * minute

    this.context.rotate(rad)

    this.context.moveTo(0, 10)

    this.context.strokeStyle = '#000'

    this.context.lineTo(0, -this.radius + 40)

    this.context.stroke()

    this.context.restore()

  }



  // 繪制秒針

  drawSecond(second: number) {

    this.context.save()

    this.context.beginPath()

    this.context.lineWidth = 2

    this.context.lineCap = 'round'

    let rad = 2 * Math.PI / 60 * second

    this.context.rotate(rad)

    this.context.moveTo(0, 10)

    this.context.strokeStyle = '#05f'

    this.context.lineTo(0, -this.radius + 21)

    this.context.stroke()

    this.context.restore()

  }



  // 繪制中心

  drawDot() {

    this.context.save()

    this.context.beginPath()

    this.context.fillStyle = '#05f'

    this.context.arc(0, 0, 4, 0, 2 * Math.PI, false)

    this.context.fill()

    this.context.restore()

  }



  // 繪制表盤下面時間文本

  drawTime(time: string) {

    this.context.save()

    this.context.beginPath()

    this.context.font = '90px'

    this.context.textAlign = "center"

    this.context.textBaseline = "middle"

    this.context.fillStyle = '#000'

    this.context.fillText(time, 0, this.radius + 80)

    this.context.restore()

  }



  build() {

    Stack({ alignContent: Alignment.Center }) {

      Canvas(this.context)

        .padding({ top: 76 })

        .width(this.canvasWidth)

        .height(this.canvasWidth + HEIGHT_ADD)

        .onReady(() = > {

          this.updateTime()

          this.intervalId = setInterval(this.updateTime, 1000)

        })

    }

    .width('100%')

    .height('100%')

  }



  onPageHide() {

    clearInterval(this.intervalId)

  }



  aboutToDisappear(){

    clearInterval(this.intervalId)

  }

}
  • 設置表盤大?。和ㄟ^Index中的display.getDefaultDisplay()方法來獲取設備寬高計算表盤大??;
  • 獲取當前時間:調用updateTime函數,執行new Date().getHours()、new Date().getMinutes()、new Date().getSeconds()獲取當前時間。
  • 繪制表盤內容:通過[CanvasRenderingContext2D] 來畫表盤背景、時針、分針、秒針、圓心以及表盤下方文本;
  • 啟動時鐘:添加setInterval定時器,每隔1s執行一次updateTime函數。

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 時鐘
    +關注

    關注

    10

    文章

    1481

    瀏覽量

    130358
  • 鴻蒙
    +關注

    關注

    55

    文章

    1786

    瀏覽量

    42158
  • HarmonyOS
    +關注

    關注

    79

    文章

    1893

    瀏覽量

    29353
收藏 人收藏

    評論

    相關推薦

    免費學習鴻蒙HarmonyOS開發,一些地址分享

    國內一流高校。通過鴻蒙班的設立,高??梢詾閷W生提供專業的鴻蒙OS學習環境和豐富的實踐機會,培養出更多的鴻蒙開發人才,為鴻蒙OS系統的生態建設
    發表于 01-12 20:48

    HarmonyOS SDK,助力開發者打造煥然一新的鴻蒙原生應用

    的操作整合在一起,用戶一處會用,處處會用。 作為支撐鴻蒙原生應用開發的核心,HarmonyOS SDK 發揮著至關重要的作用。通過關鍵能力底層化,通用能力全局化,HarmonyOS S
    發表于 01-19 10:31

    2024款鴻蒙OS 最新HarmonyOS Next_HarmonyOS4.0系列教程分享

    鴻蒙的出現,標志著中國科技的崛起。HarmonyOS就是我們說的華為鴻蒙系統,截止到2023年8月4日已有超過7億臺設備搭載了鴻蒙OS系統。據多家媒體報道,2024年國內有21所985
    發表于 02-28 10:29

    如何獲取HarmonyOS開發板 ?鴻蒙開發板全匯總

    :http://t.elecfans.com/product/116.html潤和HarmonyOS鴻蒙開發板 HiSpark IPC DIY開發套件購買地址:http://t.ele
    發表于 09-10 17:16

    首批HarmonyOS系統課程開發者為您詳解鴻蒙系統開發與應用

    首批HarmonyOS系統課程開發者。簡介:在這里不僅有大神教你如何安裝應用,更有實力派講師帶領大家進行u-boot、內核、跟文件系統的移植。鴻蒙開發課程介紹:第一節
    發表于 09-14 14:26

    鴻蒙系統(HarmonyOS)精華問答集錦

    對于鴻蒙系統,各位小伙伴是不是和小編一樣,還是有很多問題不解。本期小編就整理了鴻蒙系統首批體驗者精選問答。他們從開發者的角度出發,首先介紹了HarmonyOS的體系、內核、系統特色,以
    發表于 10-10 15:13

    【每日精選】鴻蒙大咖HarmonyOS開發資料合集

    的各種開發資料,內容包括:設計參考、程序源碼、開發實例、教程筆記等等,為大家節省了大量的資料搜索時間,方便大家輕松下載HarmonyOS相關資料!H
    發表于 10-28 18:43

    鴻蒙HarmonyOS開發學習資料匯總推薦

    `一、鴻蒙IDE下載地址IDE下載 : https://developer.harmonyos.com/cn/develop/deveco-studio?&ha_source=d
    發表于 04-20 11:33

    華為鴻蒙HarmonyOS開發者資料合集

    HarmonyOS開發者資料下載內容包括以下十點:  1、DevEco Studio 1.0 使用指南  2、HarmonyOS-NFC開發指南  3、
    發表于 03-30 10:56

    HarmonyOS資料下載專題

    HarmonyOS資料下載專題:從鴻蒙出世到現在,對于鴻蒙資料查詢下載,大家是否有點迷茫-不知去何處查找。為此,本專題匯集了HarmonyOS從入門到精通的各種
    發表于 10-08 14:23
    <b class='flag-5'>HarmonyOS</b>資料下載專題

    鴻蒙系統是基于什么開發

    2021年6月2日晚,華為正式發布HarmonyOS 2及多款搭載HarmonyOS 2的新產品。鴻蒙系統是一款全新的面向全場景的分布式操作系統,鴻蒙系統一發布,網絡上就
    的頭像 發表于 07-05 17:12 ?1w次閱讀

    華為開發者分論壇HarmonyOS學生公開課-學習鴻蒙更全面的開發

    2021華為開發者分論壇HarmonyOS學生公開課-學習鴻蒙更全面的開發
    的頭像 發表于 10-24 10:37 ?1837次閱讀
    華為<b class='flag-5'>開發</b>者分論壇<b class='flag-5'>HarmonyOS</b>學生公開課-學習<b class='flag-5'>鴻蒙</b>更全面的<b class='flag-5'>開發</b>

    淘寶與華為合作將基于HarmonyOS NEXT啟動鴻蒙原生應用開發

    1月25日,淘寶與華為舉辦鴻蒙合作簽約儀式,宣布將基于HarmonyOS NEXT啟動鴻蒙原生應用開發。
    的頭像 發表于 01-26 16:14 ?592次閱讀

    華為宣布HarmonyOS NEXT鴻蒙星河版開發者預覽面向開發者開放申請

    華為宣布HarmonyOS NEXT鴻蒙星河版開發者預覽面向開發者開放申請,這意味著鴻蒙生態進入第二階段,將加速千行百業的應用
    的頭像 發表于 01-29 16:42 ?823次閱讀
    華為宣布<b class='flag-5'>HarmonyOS</b> NEXT<b class='flag-5'>鴻蒙</b>星河版<b class='flag-5'>開發</b>者預覽面向<b class='flag-5'>開發</b>者開放申請

    HarmonyOS攜手庫洛游戲推動《戰雙帕彌什》鴻蒙原生應用開發

    4 月 22 日,華為宣布庫洛游戲的《戰雙帕彌什》正式啟動鴻蒙原生應用開發項目,與 HarmonyOS NEXT 鴻蒙星河版的方舟引擎展開深度合作,旨在提升游戲的運行流暢度,為玩家提供
    的頭像 發表于 04-22 15:21 ?144次閱讀
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>