HarmonyOS 如何自动倒计时组件

HarmonyOS 如何自动倒计时组件

HarmonyOS
2024-08-29 15:19:09
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

可参考:

class CircularCountDown { 
 private readonly context: CanvasRenderingContext2D; 
 private initialCountdownValue:number; 
 private countdownValue: number; 
 private countdownInterval: ESObject; 
 private radius: number; 
 private lineWidth: number; 
 private countdown: number; 
 private loadedColor: string; 
 private unloadedColor: string 
 constructor(context:CanvasRenderingContext2D, radius: number, lineWidth: number, countdown: number, loadedColor: string, unloadedColor: string) { 
  this.context = context 
  this.radius =radius 
  this.lineWidth = lineWidth 
  this.countdown = countdown 
  this.loadedColor = loadedColor 
  this.unloadedColor = unloadedColor 
  this.initialCountdownValue = this.countdown; 
  this.countdownValue = this.countdown; 
 } 
 
 start() { 
  this.countdownInterval = setInterval(() => { 
   this.countdownValue--; 
   if (this.countdownValue < 0) { 
    clearInterval(this.countdownInterval); 
    return; 
   } 
   this.draw(); 
  }, 1000); 
 } 
 
 draw() { 
  this.context.clearRect(0, 0, this.context.width, this.context.height); 
 
  // Draw unloaded part 
  this.context.beginPath(); 
  this.context.arc(this.radius, this.radius, this.radius - this.lineWidth / 2, 0, Math.PI * 2); 
  this.context.strokeStyle = this.unloadedColor; 
  this.context.lineWidth = this.lineWidth; 
  this.context.stroke(); 
 
  // Draw loaded part 
  let startAngle = -0.5 * Math.PI; 
  let endAngle = (2 * (this.initialCountdownValue - this.countdownValue) / this.initialCountdownValue - 0.5) * Math.PI; 
  this.context.beginPath(); 
  this.context.arc(this.radius, this.radius, this.radius - this.lineWidth / 2, startAngle, endAngle); 
  this.context.strokeStyle = this.loadedColor; 
  this.context.lineWidth = this.lineWidth; 
  this.context.stroke(); 
 
  // Draw remaining time 
  this.context.fillStyle = '#000'; 
  this.context.textAlign = 'center'; 
  this.context.textBaseline = 'middle'; 
  this.context.fillText(this.countdownValue.toString(), this.radius, this.radius); 
 } 
} 
// xxx.ets 
@Entry 
@Component 
struct Arc { 
 private settings: RenderingContextSettings = new RenderingContextSettings(true) 
 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 
 
 aboutToAppear(): void { 
 } 
 
 build() { 
  Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
   Canvas(this.context) 
    .width('100%') 
    .height('100%') 
    .backgroundColor('#ffff00') 
    .onReady(() => { 
     const circularCountDown = new CircularCountDown(this.context, 100, 10, 60, '#ff000000', '#ff00dddd'); 
     circularCountDown.start(); 
    }) 
  } 
  .width('100%') 
  .height('100%') 
 } 
}
分享
微博
QQ
微信
回复
2024-08-29 18:25:11
相关问题
如何实现文本类型的倒计时
588浏览 • 0回复 待解决
如何实现一个倒计时器?
141浏览 • 1回复 待解决
HarmonyOS Timer倒计时自定义
109浏览 • 1回复 待解决
如何禁止web组件自动加载图片?
525浏览 • 1回复 待解决
js相机组件拍照后自动保存吗
3840浏览 • 1回复 待解决
如何使用gradle自动打包?
4360浏览 • 1回复 待解决
TextInput如何取消自动获得焦点
148浏览 • 1回复 待解决
HarmonyOS Swiper里面的item高度自动刷新
107浏览 • 1回复 待解决
如何实现应用的屏幕自动旋转
1906浏览 • 1回复 待解决
HarmonyOS 如何扩大组件点击区域
81浏览 • 1回复 待解决
HarmonyOS应用的自动化测试工具
95浏览 • 1回复 待解决
HarmonyOS 如何从子组件控制手势分发
168浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人