当前位置:网站首页>#夏日挑战赛# 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开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- 二叉树的前序,中序,后续(非递归版本)
- What time do you get off work?!!!
- 入侵检测模型(An Intrusion-Detection Model)
- Implementation of wechat web page subscription message
- Wechat applet 01 bottom navigation bar settings
- The last picture is seamlessly connected with the first picture in the swiper rotation picture
- 精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
- 《QT+PCL第六章》点云配准icp系列4
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- Go zero actual combat demo (I)
猜你喜欢

【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB

采集数据工具推荐,以及采集数据列表详细图解流程

What time do you get off work?!!!

An intrusion detection model

MySQL backup and restore single database and single table

Reading notes of top performance version 2 (V) -- file system monitoring
![[one day learning awk] function and user-defined function](/img/e1/a378211ef05fcc4d469363f3e509a7.png)
[one day learning awk] function and user-defined function

微信小程序01-底部导航栏设置

软件测试的可持续发展,必须要学会敲代码?

她就是那个「别人家的HR」|ONES 人物
随机推荐
《QT+PCL第六章》点云配准icp系列2
Summary of point cloud reconstruction methods I (pcl-cgal)
Tableapi & SQL and Kafka message acquisition of Flink example
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
[antenna] [3] some shortcut keys of CST
swiper 轮播图,最后一张图与第一张图无缝衔接
MySQL service is starting. MySQL service cannot be started. Solution
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
Tensorflow team: we haven't been abandoned
ABAP-调用Restful API
华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
入侵检测模型(An Intrusion-Detection Model)
Microservice tracking SQL (support Gorm query tracking under isto control)
说明 | 华为云云商店「商品推荐榜」
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据
微信小程序02-轮播图实现与图片点击跳转
《QT+PCL第六章》点云配准icp系列3
Recommendation of data acquisition tools and detailed graphic process of data acquisition list