当前位置:网站首页>#夏日挑战赛# HarmonyOS canvas实现时钟
#夏日挑战赛# HarmonyOS canvas实现时钟
2022-07-01 15:41:00 【51CTO】
作者:郑瑶
前言
最近在学习HarmonyOS 相关的东西,看了很多网上的canvas 实现时钟,现在我也来写一个关于HarmonyOS 的时钟吧。
项目说明
工具版本:DevEco Studio 3.0 Beta3
SDK版本;3.1.5.5
主要用到知识:canvas js
效果展示

实现步骤
1. 在页面index.hml 中定义一个canvas 元素
2. 需要在index.js data 中定义两个变量用来存放目标元素和绘制2d
3. 需要在onShow 生命周期中拿到目标元素并且绘制 2d
4. 定义一个初始化函数initData(),调用绘制函数
5. 绘制函数clock函数
5.1 首先绘制外部最大的圆
此时的页面长这样:

5.2 绘制时间(时)刻度(12小时刻度)
此时页面:

5.3 绘制中心的小圆点
此时的页面长这样:

5.4 获取时间并且转化成北京时间
此时需要获取当前系统的本地时间,在此处遇到了一个小麻烦,鸿蒙开发者工具new Date() 获取到的时间为格林威治时间,而我们现在采用的是北京时间,需要写函数来转化时间,转化函数为getTimeStamp()
获取当前北京时间:
5.5 绘制时钟的指针
此时的页面长这样:

5.6 绘制分针的指针
此时页面长这样:

5.7 绘制秒针
此时页面长这样:

5.8 最后一步我们需要写一个更新函数用来调用所有绘画函数并且在更新函数中获取对应的北京时间:
6. 最后一步我们需要让这个钟表转动起来
那么就需要在initData 函数中写一个定时器没一秒钟执行一次绘制函数即可: 最后的图片如上图第一张图所示的效果
总结:
以上就是canvas 绘制一个时钟的全部步骤了。 最重要的一点是要能正确的获取到时间(北京时间)但是我发现在编辑器上new Date() 到的时间跟咋们的北京时间相差了八小时,在一位同事的提醒下才知道这个获取的时间为格林威治时间,那么我们需要将这个格林威治时间转化成北京时间,在网上找了一段转化方法终于转化成功。
更多原创内容请关注: 中软国际 HarmonyOS 技术团队
入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- The difference between arrow function and ordinary function in JS
- 《QT+PCL第六章》点云配准icp系列4
- 华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
- Qt+pcl Chapter 6 point cloud registration ICP series 3
- TensorFlow团队:我们没被抛弃
- Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
- 张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
- Rhcsa fourth day operation
- 张驰课堂:六西格玛数据的几种类型与区别
猜你喜欢

TensorFlow團隊:我們沒被拋弃

Introduction to MySQL audit plug-in

How to realize clock signal frequency division?
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)

雷神科技冲刺北交所,拟募集资金5.4亿元

Équipe tensflow: Nous ne sommes pas abandonnés
![[one day learning awk] function and user-defined function](/img/e1/a378211ef05fcc4d469363f3e509a7.png)
[one day learning awk] function and user-defined function

The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple

HR面试:最常见的面试问题和技巧性答复

有些能力,是工作中学不来的,看看这篇超过90%同行
随机推荐
Introduction to MySQL audit plug-in
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
vim 从嫌弃到依赖(22)——自动补全
说明 | 华为云云商店「商品推荐榜」
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
Microservice tracking SQL (support Gorm query tracking under isto control)
MySQL审计插件介绍
微信小程序01-底部导航栏设置
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
ABAP-调用Restful API
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
An intrusion detection model
SQL常用的四个排序函数梳理
Recommendation of data acquisition tools and detailed graphic process of data acquisition list
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
[cloud trend] new wind direction in June! Cloud store hot list announced
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication