HarmonyOS Timer倒计时自定义

HarmonyOS Timer倒计时自定义

HarmonyOS
2024-08-29 11:07:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

可参考:

@Entry 
@Component 
struct TextTimer_self { 
 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(Color.White) 
    .onReady(() => { 
     this.context.font = '150px sans-serif' 
     const circularCountDown = new CircularCountDown(this.context, 100, 10, 60, '#ff000000', '#ff00dddd'); 
     circularCountDown.start(); 
    }) 
  } 
  .width('100%') 
  .height('100%') 
 } 
} 
 
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); 
 } 
}
分享
微博
QQ
微信
回复
2024-08-29 16:44:49
相关问题
HarmonyOS 倒计时天数
184浏览 • 1回复 待解决
HarmonyOS 如何自动倒计时组件
381浏览 • 1回复 待解决
如何实现文本类型的倒计时
826浏览 • 0回复 待解决
如何实现一个倒计时器?
389浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现倒计时功能?
31035浏览 • 8回复 待解决
自定义弹窗自定义转场动画
1040浏览 • 1回复 待解决
HarmonyOS 自定义键盘
177浏览 • 1回复 待解决
HarmonyOS 使用自定义字体
144浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
279浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘
290浏览 • 1回复 待解决
HarmonyOS 如何自定义注解?
150浏览 • 1回复 待解决
HarmonyOS 如何自定义tab
322浏览 • 2回复 待解决
HarmonyOS 如何自定义BuildMode?
407浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
173浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
298浏览 • 1回复 待解决
HarmonyOS 自定义Slider样式
250浏览 • 1回复 待解决
HarmonyOS 用CustomDialog自定义Dialog
183浏览 • 1回复 待解决
HarmonyOS WebView设置自定义UserAgent
163浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
127浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人