目录

播放进度条的进度动画不流畅

问题现象

视频播放进度条动画卡顿,如何解决该问题。

背景知识

问题定位

  1. 使用ArkUI Inspector确认进度条组件的实现方式,确定为Progress组件实现。
  2. 检查Progress的value值的实现逻辑,参考以下示例,当value值使用简单的按1s时间自增,进度条便会出现非线性流畅的、跳跃的播放显示。
    Progress({ value: this.value, total: 10, type: ProgressType.Linear })
      .style({ strokeWidth: 10, enableSmoothEffect: true })
      .width('80%')
      .height(150)
      .backgroundColor(Color.Pink)
    Button('start').onClick(() => {
      this.timer = setInterval((() => {
        this.value += 1
      }), 1000)
    })

分析结论

使用Progress进度条组件时,value值使用了简单自增的更新逻辑,导致进度条非线性流畅的播放。

修改建议

参考以下示例,使用animation动画自定义实现进度条的更新效果:

@Entry
@Component
struct ProgressCase {
  @State value: number = 0;
  uiContext: UIContext | undefined = undefined;

  build() {
    Column() {
      Column() {
        Progress({ value: this.value, total: 10, type: ProgressType.Linear })
          .style({ strokeWidth: 10, enableSmoothEffect: true })
          .width('80%')
          .height(150)
          .backgroundColor(Color.Gray)
        Button('start').onClick(() => {
          this.uiContext = this.getUIContext();
          this.uiContext?.animateTo({
            duration: 10 * 1000, // 动画持续时间
            curve: Curve.Linear, // 动画曲线
            playMode: PlayMode.Normal, // 动画播放模式
            onFinish: () => {
              console.info('play end');
            }
          }, () => {
            this.value = 10;
          });
        })
      }.width('100%').height('100%')
    }
  }
}