Text(this.message)
.onClick(() => {
this.message = this.message + '1';
})
.maxLines(1)
.fontSize(40)
.fontWeight(800)
.fontColor(Color.White) .linearGradient({
angle: 110,
colors: [[Color.White, this.leftlocation], [Color.Orange, this.location],
[Color.White, this.rightlocation]] // 设置渐变色起始点位
}) .blendMode(BlendMode.DST_IN, BlendApplyType.OFFSCREEN) // 设置混合模式
aboutToAppear(): void {
setInterval(() => {
this.location += 0.01;
this.leftlocation += 0.01;
this.rightlocation += 0.01;
if (this.location >= 1) {
this.location = 0.0;
this.leftlocation = -0.1;
this.rightlocation = 0.1;
}
}, 50);
} 效果预览:

完整示例参考如下:
@Entry
@Component
struct LightWord {
@State message: string = 'Hello World';
@State location: number = 0.0;
@State leftlocation: number = -0.1;
@State rightlocation: number = 0.1;
// 配置渐变色起始点位周期性移动
aboutToAppear(): void {
setInterval(() => {
this.location += 0.01;
this.leftlocation += 0.01;
this.rightlocation += 0.01;
if (this.location >= 1) {
this.location = 0.0;
this.leftlocation = -0.1;
this.rightlocation = 0.1;
}
}, 50);
}
build() {
Column({ space: 5 }) {
Row() {
Text(this.message)
.onClick(() => {
this.message = this.message + '1';
})
.maxLines(1)
.fontSize(40)
.fontWeight(800)
.fontColor(Color.White)
.padding(10)
.blendMode(BlendMode.DST_IN, BlendApplyType.OFFSCREEN) // 设置混合模式
}
.linearGradient({
angle: 110,
colors: [[Color.White, this.leftlocation], [Color.Orange, this.location],
[Color.White, this.rightlocation]] // 设置渐变色起始点位
})
.blendMode(BlendMode.SRC_OVER, BlendApplyType.OFFSCREEN)
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
.justifyContent(FlexAlign.Center)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
}
} 
完整示例参考如下:
@Entry
@Component
struct Index {
@State gradientPosition: number = 0;
textContent: string = '生活就像海洋,只有意志坚强的人';
COLOR_START: Color = Color.Yellow;
COLOR_END: Color = Color.Blue;
build() {
Column() {
Stack() {
Text(this.textContent + this.textContent)
.blendMode(BlendMode.DST_IN, BlendApplyType.OFFSCREEN)
.fontSize(30)
.fontWeight(FontWeight.Bold)
}
.blendMode(BlendMode.SCREEN, BlendApplyType.OFFSCREEN)
.linearGradient({
angle: 135,
colors: [
[this.COLOR_START, 0],
[this.COLOR_END, this.gradientPosition],
[this.COLOR_START, 1],
]
})
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.onAppear(() => {
this.animateStart();
})
}
private animateStart() {
this.getUIContext().animateTo({
duration: 3000,
curve: Curve.Linear,
iterations: 1,
onFinish: () => {
this.gradientPosition = 0;
const COLOR_START = this.COLOR_START;
const COLOR_END = this.COLOR_END;
// 互换起始与终点的颜色
this.COLOR_START = COLOR_END;
this.COLOR_END = COLOR_START;
this.animateStart();
}
}, () => {
// 启动动画:修改gradientPosition触发渐变移动
this.gradientPosition = 1;
});
}
}