当前位置:网站首页>#夏日挑战赛# 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开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- 将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
- [antenna] [3] some shortcut keys of CST
- Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
- What are the test items of juicer ul982
- Reading notes of top performance version 2 (V) -- file system monitoring
- Équipe tensflow: Nous ne sommes pas abandonnés
- 【云动向】6月上云新风向!云商店热榜揭晓
- 《QT+PCL第六章》点云配准icp系列2
- The difference between arrow function and ordinary function in JS
- Go zero actual combat demo (I)
猜你喜欢
精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
张驰课堂:六西格玛数据的几种类型与区别
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
[target tracking] |stark
【OpenCV 例程200篇】216. 绘制多段线和多边形
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)
HR面试:最常见的面试问题和技巧性答复
MySQL审计插件介绍
三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
S32K1xx 微控制器的硬件設計指南
随机推荐
An intrusion detection model
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
The difference between arrow function and ordinary function in JS
ABAP-屏幕切换时,刷新上一个屏幕
MySQL高级篇4
她就是那个「别人家的HR」|ONES 人物
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
【目标跟踪】|STARK
微信小程序01-底部导航栏设置
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
有些能力,是工作中学不来的,看看这篇超过90%同行
Using swiper to make mobile phone rotation map
How to realize clock signal frequency division?
Day 3 of rhcsa study
Pico,能否拯救消费级VR?
A unifying review of deep and shallow anomaly detection
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all