paint(ctx: CanvasRenderingContext2D) {
// 清除之前的绘制指令
ctx.reset();
// 绘制进度条背景
ctx.lineCap = 'round';
ctx.lineWidth = this.lineWidth;
ctx.strokeStyle = '#ff999696';
ctx.beginPath();
ctx.arc(ctx.width / 2, ctx.height / 2, this.radius, this.startAngle, this.endAngle);
ctx.stroke();
// 绘制进度条刻度
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
const startR = this.radius - 4;
const endR = this.radius + 4;
ctx.beginPath();
const count = 50;
const startAngle = this.startAngle + 2 / count * (this.endAngle - this.startAngle);
const deltaAngle = (1 - 3 / count) * (this.endAngle - this.startAngle);
for (let i = 0; i < count; i++) {
ctx.moveTo(ctx.width / 2 + startR * Math.cos(startAngle + deltaAngle * i / count),
ctx.height / 2 + startR * Math.sin(startAngle + deltaAngle * i / count));
ctx.lineTo(ctx.width / 2 + endR * Math.cos(startAngle + deltaAngle * i / count),
ctx.height / 2 + endR * Math.sin(startAngle + deltaAngle * i / count));
}
ctx.stroke();
// 绘制进度条内容
ctx.strokeStyle = '#ff0';
ctx.beginPath();
ctx.arc(ctx.width / 2, ctx.height / 2, this.radius, this.startAngle,
this.startAngle + this.currentValue * (this.endAngle - this.startAngle));
ctx.stroke();
} .onActionStart(event => {
const x = event.fingerList[0].localX - this.ctx.width / 2;
const y = event.fingerList[0].localY - this.ctx.height / 2;
const r = Math.sqrt(x * x + y * y);
const cos = x / r;
const theta = Math.atan(y / x);
this.isUpdate =
r >= this.radius - this.lineWidth / 2 && r <= this.radius + this.lineWidth / 2 && cos >= 0 &&
theta >= -Math.PI / 2 && theta <= Math.PI / 2;
}) function getAngleFromCoordinate(x: number, y: number) {
let angle = Math.atan(Math.abs(y) / Math.abs(x));
if (x >= 0) {
if (y < 0) {
return -angle;
} else {
return angle;
}
} else {
if (y >= 0) {
return Math.PI - angle;
} else {
return angle + Math.PI;
}
}
} Image($r('app.media.startIcon')) // 此处仅为示例,请开发者自行替换为可用图片。
.width(10)
.translate({ x: this.radius * Math.cos(this.startAngle), y: this.radius * Math.sin(this.startAngle) });
Image($r('app.media.startIcon'))
.width(10)
.translate({ x: this.radius * Math.cos(this.endAngle), y: this.radius * Math.sin(this.endAngle) }); 完整示例参考如下:
function getAngleFromCoordinate(x: number, y: number) {
let angle = Math.atan(Math.abs(y) / Math.abs(x));
if (x >= 0) {
if (y < 0) {
return -angle;
} else {
return angle;
}
} else {
if (y >= 0) {
return Math.PI - angle;
} else {
return angle + Math.PI;
}
}
}
@Entry
@Component
struct DraggableCircularProgress {
startAngle: number = -Math.PI / 2;
@State endAngle: number = Math.PI / 2;
radius: number = 100;
private ctx = new CanvasRenderingContext2D(new RenderingContextSettings(true));
currentValue: number = 0;
private isUpdate: boolean = false;
private lineWidth: number = 20;
paint(ctx: CanvasRenderingContext2D) {
// 清除之前的绘制指令
ctx.reset();
// 绘制进度条背景
ctx.lineCap = 'round';
ctx.lineWidth = this.lineWidth;
ctx.strokeStyle = '#ff999696';
ctx.beginPath();
ctx.arc(ctx.width / 2, ctx.height / 2, this.radius, this.startAngle, this.endAngle);
ctx.stroke();
// 绘制进度条刻度
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
const startR = this.radius - 4;
const endR = this.radius + 4;
ctx.beginPath();
const count = 50;
const startAngle = this.startAngle + 2 / count * (this.endAngle - this.startAngle);
const deltaAngle = (1 - 3 / count) * (this.endAngle - this.startAngle);
for (let i = 0; i < count; i++) {
ctx.moveTo(ctx.width / 2 + startR * Math.cos(startAngle + deltaAngle * i / count),
ctx.height / 2 + startR * Math.sin(startAngle + deltaAngle * i / count));
ctx.lineTo(ctx.width / 2 + endR * Math.cos(startAngle + deltaAngle * i / count),
ctx.height / 2 + endR * Math.sin(startAngle + deltaAngle * i / count));
}
ctx.stroke();
// 绘制进度条内容
ctx.strokeStyle = '#ff0';
ctx.beginPath();
ctx.arc(ctx.width / 2, ctx.height / 2, this.radius, this.startAngle,
this.startAngle + this.currentValue * (this.endAngle - this.startAngle));
ctx.stroke();
}
build() {
Stack() {
Stack() {
Canvas(this.ctx)
.width('100%')
.height('100%')
.onReady(() => {
this.paint(this.ctx);
})
.gesture(
PanGesture({ fingers: 1 })
.onActionStart(event => {
const x = event.fingerList[0].localX - this.ctx.width / 2;
const y = event.fingerList[0].localY - this.ctx.height / 2;
const r = Math.sqrt(x * x + y * y);
const cos = x / r;
const theta = Math.atan(y / x);
this.isUpdate =
r >= this.radius - this.lineWidth / 2 && r <= this.radius + this.lineWidth / 2 && cos >= 0 &&
theta >= -Math.PI / 2 && theta <= Math.PI / 2;
})
.onActionUpdate(event => {
if (this.isUpdate) {
const x = event.fingerList[0].localX - this.ctx.width / 2;
const y = event.fingerList[0].localY - this.ctx.height / 2;
this.endAngle = getAngleFromCoordinate(x, y);
this.paint(this.ctx);
}
})
.onActionEnd(() => {
this.isUpdate = false;
})
);
Image($r('app.media.startIcon')) // 此处仅为示例,请开发者自行替换为可用图片。
.width(10)
.translate({ x: this.radius * Math.cos(this.startAngle), y: this.radius * Math.sin(this.startAngle) });
Image($r('app.media.startIcon'))
.width(10)
.translate({ x: this.radius * Math.cos(this.endAngle), y: this.radius * Math.sin(this.endAngle) });
Text()
.width(145)
.aspectRatio(1)
.backgroundColor('#ff999696')
.borderRadius('50%');
}
.width(120 * 2)
.aspectRatio(1)
.backgroundColor('#000')
.borderRadius('50%');
}
.height('100%')
.width('100%');
}
}