ArkTS实现时钟的方式有哪些?

ArkTS实现时钟

HarmonyOS
2024-05-26 12:18:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
yu_qingbo

本文主要介绍ArkTs实现时钟的方式

典型的使用场景如下:

时钟功能在首页应用比较广泛,除了数字时间显示外,表盘时钟在多个场景都有广泛使用。

使用的核心API

  •  background
  •  rotate
  •  animation
  •  systemDateTime

核心代码解释

1.使用精灵图技术分别获取时钟的表盘、时针、分针、秒针的图像,调整好大小后使用position定位属性将各个指针定位到表盘中间的正确位置。

2.使用旋转属性分别将各个指针的旋转坐标轴及原点调整好,保持一致,测试旋转后不会偏位。

3.使用动画属性将动画效果调整为线性匀速,使指针旋转效果更自然。

4.获得时间戳,加上时间差后分别计算出当前系统时间的时、分、秒值,以此设置时钟初始值。

5.根据各指针的旋转角度不同分别设置每秒的步进。

6.步进控制由定时器实现,分别给时针、分针、秒针三者每秒设置不同的步进。

核心代码如下:

import { BusinessError } from '@ohos.base' 
import systemDateTime from '@ohos.systemDateTime'; 
 
@Entry 
@Component 
struct Index { 
  @State time: number = 0; 
  @State hours: number = 0; 
  @State minute: number = 0; 
  @State Seconds: number = 0; 
  timeId: number = -1; 
 
  onPageShow() { 
    try { 
      if (this.timeId != -1) { 
        console.log('当前Id为:' + this.timeId) 
        return 
      } 
      systemDateTime.getCurrentTime(false, (error: BusinessError, time: number) => { 
        if (error) { 
          console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 
          return; 
        } 
        console.info(`Succeeded in getting currentTime : ${time}`); 
        this.time = time; 
        //获取当前时间的小时,因为有8小时时间差,所以要加上 
        //使用 ~~ 和 Math.floor都可实现向下取整 
        this.hours = ((time + 3600 * 1000 * 8) / (3600 * 1000)) % 24 
        this.minute = ((time + 3600 * 1000 * 8) / (1000 * 60)) % 60 
        this.Seconds = Math.floor(((time + 3600 * 1000 * 8) / 1000) % 60) 
        console.info('hours is :' + this.hours) 
        console.info('minute is :' + this.minute) 
        console.info('Seconds is :' + this.Seconds) 
      }); 
    } catch (e) { 
      let error = e as BusinessError; 
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 
    } 
 
    //指针移动角度 
    this.timeId = setInterval(() => { 
      this.hours += (1 / 3600); //时针角度 
      this.minute += (1 / 60) //分针角度) 
      this.Seconds += 1; //秒针角度 
      console.info('当前秒数:' + this.Seconds) 
    }, 1000) 
  } 
 
  build() { 
    Row() { 
      Column({ space: 20 }) { 
        Text('获取的当前时间为:' + ~~(this.hours % 24) + ':' + ~~(this.minute % 60) + ':' + (this.Seconds % 60)) 
        //使用精灵图分别获取表盘、时针、分针、秒针 
        Stack() { 
          //表盘 
          Row() 
            .width('740px') 
            .height('710px') 
            .backgroundImage('comment/clock.png', ImageRepeat.NoRepeat) 
            .backgroundImageSize({ 
              width: '988px', 
              height: '800px' 
            }) 
            .backgroundImagePosition({ 
              x: '-190px', 
              y: '-64px' 
            }) 
 
          //时针 
          Row() 
            .width('40px') 
            .height('162px') 
            .backgroundImage('comment/clock.png', ImageRepeat.NoRepeat) 
            .backgroundImageSize({ 
              width: '988px', 
              height: '800px' 
            }) 
            .backgroundImagePosition({ 
              x: '-111px', 
              y: '-536px' 
            }) 
            .position({ 
              x: '350px', 
              y: '212px' 
            }) 
            .rotate({ 
              x: 0, 
              y: 0, 
              z: 1, 
              centerX: '50%', 
              centerY: '89%', 
              centerZ: 0, 
              angle: this.hours * 30 
            }) 
            .animation({ 
              duration: 1000, 
              iterations: 1, 
              curve: Curve.Linear 
            }) 
 
          //分针 
          Row() 
            .width('30px') 
            .height('216px') 
            .backgroundImage('comment/clock.png', ImageRepeat.NoRepeat) 
            .backgroundImageSize({ 
              width: '988px', 
              height: '800px' 
            }) 
            .backgroundImagePosition({ 
              x: '-70px', 
              y: '-480px' 
            }) 
            .position({ 
              x: '356px', 
              y: '156px' 
            }) 
            .rotate({ 
              x: 0, 
              y: 0, 
              z: 1, 
              centerX: '50%', 
              centerY: '94%', 
              centerZ: 0, 
              angle: this.minute * 6, //一分钟走6度 
            }) 
            .animation({ 
              duration: 1000, 
              iterations: 1, 
              curve: Curve.Linear 
            }) 
 
          //秒针 
          Row() 
            .width('36px') 
            .height('320px') 
            .backgroundImage('comment/clock.png', ImageRepeat.NoRepeat) 
            .backgroundImageSize({ 
              width: '988px', 
              height: '800px' 
            }) 
            .backgroundImagePosition({ 
              x: '-22px', 
              y: '-444px' 
            }) 
            .position({ 
              x: '352px', 
              y: '122px' 
            }) 
            .rotate({ 
              x: 0, 
              y: 0, 
              z: 1, 
              centerX: '50%', 
              centerY: '74%', 
              centerZ: 0, 
              angle: this.Seconds * 6 
            }) 
            .animation({ 
              duration: 1000, 
              iterations: 1, 
              curve: Curve.Linear 
            }) 
        } 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

实现效果

预览器不支持获取时间戳,此效果是由测试机运行后投屏到电脑端录制,会有卡顿延迟,实测在测试机上运行无卡顿。

适配的版本信息

IDE:DevEco Studio 4.0.1.601

SDK:HarmoneyOS 4.0.10.8

分享
微博
QQ
微信
回复
2024-05-27 16:04:41
相关问题
哪些创建线程方式
840浏览 • 1回复 待解决
数据持久化方式哪些
350浏览 • 1回复 待解决
获取MainAbility name方式哪些呢?
473浏览 • 1回复 待解决
ArkTS哪些语法规则,懂得吗?
525浏览 • 1回复 待解决
如何通过定时器和画布实现一个时钟
356浏览 • 1回复 待解决
阿里云Redis集群实现细节哪些
1580浏览 • 1回复 待解决
PolarDB 支持哪些种扩容方式
2105浏览 • 1回复 待解决
关于如何获取时钟id问题
4399浏览 • 1回复 待解决
NoSQLMongoDB哪些优点?
2635浏览 • 1回复 待解决
MongoDB优势哪些
2604浏览 • 1回复 待解决
哪些好用MySQL监控软件?
865浏览 • 1回复 待解决
ability主题哪些可以设置?
15382浏览 • 3回复 待解决
redis相比memcached优势哪些
1146浏览 • 1回复 待解决
支持鸿蒙系统手机哪些
3628浏览 • 2回复 待解决
WaterFlow使用范例哪些
292浏览 • 1回复 待解决
PolarDB MySQL 参数哪些
1644浏览 • 1回复 待解决
HarmonyOS拉起弹窗方式那几种
504浏览 • 1回复 待解决