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 如何自动倒计时组件
157浏览 • 1回复 待解决
如何实现文本类型的倒计时
598浏览 • 0回复 待解决
如何实现一个倒计时器?
155浏览 • 1回复 待解决
自定义弹窗自定义转场动画
664浏览 • 1回复 待解决
HarmonyOS 如何自定义BuildMode?
112浏览 • 1回复 待解决
HarmonyOS ArkUI加载自定义组件
153浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
114浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
234浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
178浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
317浏览 • 1回复 待解决
HarmonyOS补充nativgation的自定义实现
103浏览 • 1回复 待解决
HarmonyOS自定义相册选择页面咨询
96浏览 • 1回复 待解决
HarmonyOS如何自定义视频组件样式
161浏览 • 1回复 待解决
HarmonyOS ArkWeb是否支持自定义UserAgent
118浏览 • 1回复 待解决
HarmonyOS 自定义弹窗遮罩未全屏
143浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘问题
173浏览 • 1回复 待解决
hvigor自定义扩展demo
620浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人