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%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.

实现效果

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

适配的版本信息

IDE:DevEco Studio 4.0.1.601

SDK:HarmoneyOS 4.0.10.8

分享
微博
QQ
微信
回复
2024-05-27 16:04:41


相关问题
HarmonyOS JSBridge哪些实现方式
450浏览 • 1回复 待解决
关于图文混排实现方式哪些
785浏览 • 1回复 待解决
哪些创建线程方式
2740浏览 • 1回复 待解决
数据持久化方式哪些
1606浏览 • 1回复 待解决
查看文件列表方式哪些
1040浏览 • 1回复 待解决
获取MainAbility name方式哪些呢?
2059浏览 • 1回复 待解决
HarmonyOS ArkTS 关于重载实现方式
483浏览 • 1回复 待解决
ArkTS生成密钥问题哪些
652浏览 • 1回复 待解决
应用性能问题优化方式哪些
1219浏览 • 1回复 待解决
ArkTS哪些语法规则,懂得吗?
2739浏览 • 1回复 待解决
气泡组件推荐实现方式么?
1068浏览 • 1回复 待解决
ArkTs解决循环引用方式
2686浏览 • 1回复 待解决
消息推送都有哪些方式?
694浏览 • 1回复 待解决