鸿蒙运动开发实战:打造 Keep 式轨迹播放效果

前言

在运动类应用中,轨迹播放效果是提升用户体验的关键功能之一。它不仅能直观展示用户的运动路线,还能通过动态效果增强运动的趣味性。Keep 作为一款知名的运动健身应用,其轨迹播放效果深受用户喜爱。那么,如何在鸿蒙系统中开发出类似 Keep 的轨迹播放效果呢?本文将通过实际代码案例,深入解析实现这一功能的关键步骤和技术要点。

效果:

一、核心功能拆解

要实现类似 Keep 的轨迹播放效果,我们需要完成以下几个核心功能:

• 动态轨迹播放:通过定时器和动画效果,实现轨迹的动态播放,模拟用户运动过程。

• 地图交互:在地图上绘制轨迹,并根据播放进度更新地图中心点和旋转角度。

二、动态轨迹播放

1.播放逻辑

通过定时器和动画效果实现轨迹的动态播放。以下是播放轨迹的核心代码:

typescript 复制代码
private playTrack() {
  // 如果已经在播放,则停止
  if (this.playTimer) {
    this.mapController?.removeOverlay(this.polyline);
    clearInterval(this.playTimer);
    this.playTimer = undefined;
    if (this.animationTimer) {
      clearInterval(this.animationTimer);
    }
    if (this.movingMarker) {
      this.mapController?.removeOverlay(this.movingMarker);
      this.movingMarker = undefined;
    }
    this.currentPointIndex = 0;
    return;
  }

  // 创建动态位置标记
  this.movingMarker = new Marker({
    position: this.trackPoints[0],
    icon: new ImageEntity("rawfile://images/ic_run_detail_start.png"),
    isJoinCollision: SysEnum.CollisionBehavior.NOT_COLLIDE,
    located: SysEnum.Located.CENTER
  });
  this.mapController?.addOverlay(this.movingMarker);

  // 开始播放
  this.playTimer = setInterval(() => {
    this.currentPointIndex++;
    if (this.currentPointIndex >= this.trackPoints.length) {
      clearInterval(this.playTimer);
      this.playTimer = undefined;
      this.currentPointIndex = 0;
      if (this.movingMarker) {
        this.mapController?.removeOverlay(this.movingMarker);
        this.movingMarker = undefined;
      }
      return;
    }

    // 更新动态位置标记位置,使用setInterval实现平滑移动
    if (this.movingMarker && this.currentPointIndex < this.trackPoints.length - 1) {
      const currentPoint = this.trackPoints[this.currentPointIndex];
      const nextPoint = this.trackPoints[this.currentPointIndex + 1];
      let animationProgress = 0;

      // 清除之前的动画定时器
      if (this.animationTimer) {
        clearInterval(this.animationTimer);
      }

      // 创建新的动画定时器,每10ms更新一次位置
      this.animationTimer = setInterval(() => {
        animationProgress += 0.1; // 每次增加0.1的进度

        if (animationProgress >= 1) {
          clearInterval(this.animationTimer);
          this.animationTimer = undefined;
          this.movingMarker?.setPosition(new LatLng(nextPoint.lat, nextPoint.lng));
        } else {
          const interpolatedLat = currentPoint.lat + (nextPoint.lat - currentPoint.lat) * animationProgress;
          const interpolatedLng = currentPoint.lng + (nextPoint.lng - currentPoint.lng) * animationProgress;
          this.movingMarker?.setPosition(new LatLng(interpolatedLat, interpolatedLng));
        }
      }, 10); // 每10ms执行一次
    }

    // 绘制当前轨迹线段
    const currentPoints = this.trackPoints.slice(0, this.currentPointIndex + 1);
    const currentColors = PathGradientTool.getPathColors(this.record!.points.slice(0, this.currentPointIndex + 1), 100);

    if (this.polyline) {
      this.mapController?.removeOverlay(this.polyline);
      this.polyline.remove();
      this.polyline.destroy();
    }

    this.polyline = new Polyline({
      points: currentPoints,
      width: 5,
      join: SysEnum.LineJoinType.ROUND,
      cap: SysEnum.LineCapType.ROUND,
      isGradient: true,
      colorList: currentColors!
    });
    this.mapController?.addOverlay(this.polyline);

    // 更新地图中心点和旋转角度
    let bearing = 0;
    if (this.currentPointIndex < this.trackPoints.length - 1) {
      const currentPoint = this.trackPoints[this.currentPointIndex];
      const nextPoint = this.trackPoints[this.currentPointIndex + 1];
      bearing = Math.atan2(
        nextPoint.lat - currentPoint.lat,
        nextPoint.lng - currentPoint.lng
      ) * 180 / Math.PI;
      bearing = (bearing + 360) % 360;
      bearing = (360 - bearing + 90) % 360;
    }

    this.mapController?.mapStatus.setRotate(bearing).setOverlooking(90).setCenterPoint(new LatLng(this.trackPoints[this.currentPointIndex].lat, this.trackPoints[this.currentPointIndex].lng)).refresh();
  }, 100); // 每100ms移动一次
}

2.动画效果

通过定时器和线性插值实现动态轨迹的平滑移动效果。以下是动画效果的核心代码:

typescript 复制代码
if (this.movingMarker && this.currentPointIndex < this.trackPoints.length - 1) {
  const currentPoint = this.trackPoints[this.currentPointIndex];
  const nextPoint = this.trackPoints[this.currentPointIndex + 1];
  let animationProgress = 0;

  // 清除之前的动画定时器
  if (this.animationTimer) {
    clearInterval(this.animationTimer);
  }

  // 创建新的动画定时器,每10ms更新一次位置
  this.animationTimer = setInterval(() => {
    animationProgress += 0.1; // 每次增加0.1的进度

    if (animationProgress >= 1) {
      clearInterval(this.animationTimer);
      this.animationTimer = undefined;
      this.movingMarker?.setPosition(new LatLng(nextPoint.lat, nextPoint.lng));
    } else {
      const interpolatedLat = currentPoint.lat + (nextPoint.lat - currentPoint.lat) * animationProgress;
      const interpolatedLng = currentPoint.lng + (nextPoint.lng - currentPoint.lng) * animationProgress;
      this.movingMarker?.setPosition(new LatLng(interpolatedLat, interpolatedLng));
    }
  }, 10); // 每10ms执行一次
}

三、地图交互

1.地图中心点和旋转角度更新

在播放轨迹的过程中,动态更新地图的中心点和旋转角度,以确保用户始终能看到当前播放的位置。以下是更新地图中心点和旋转角度的代码:

typescript 复制代码
let bearing = 0;
if (this.currentPointIndex < this.trackPoints.length - 1) {
  const currentPoint = this.trackPoints[this.currentPointIndex];
  const nextPoint = this.trackPoints[this.currentPointIndex + 1];
  bearing = Math.atan2(
    nextPoint.lat - currentPoint.lat,
    nextPoint.lng - currentPoint.lng
  ) * 180 / Math.PI;
  bearing = (bearing + 360) % 360;
  bearing = (360 - bearing + 90) % 360;
}

this.mapController?.mapStatus.setRotate(bearing).setOverlooking(90).setCenterPoint(new LatLng(this.trackPoints[this.currentPointIndex].lat, this.trackPoints[this.currentPointIndex].lng)).refresh();

四、总结

通过上述步骤,我们成功实现了类似 Keep 的轨迹播放效果。不仅提升了用户体验,还为运动数据的可视化提供了有力支持。

相关推荐
Vence08152 小时前
Flutter3.22适配运行鸿蒙系统问题记录
flutter·华为·harmonyos·鸿蒙
郑知鱼6 小时前
【拥抱鸿蒙】Flutter+Cursor轻松打造HarmonyOS应用(二)
flutter·华为·harmonyos·鸿蒙·cursor·移动端·鸿蒙next·ohos
Vue102421 小时前
uniapp vue3 鸿蒙支持的 HTML5+接口
前端·uni-app·html5·鸿蒙
郑知鱼1 天前
【拥抱鸿蒙】Flutter+Cursor轻松打造HarmonyOS应用(一)
flutter·华为·harmonyos·鸿蒙·cursor·移动端·鸿蒙next·ohos
郑知鱼1 天前
【拥抱鸿蒙】基于 Cocos Creator 的 HarmonyOS 自动构建
华为·harmonyos·鸿蒙·移动端·鸿蒙next·ohos
颜颜yan_1 天前
【HarmonyOS5】Stage模型应用程序包结构详解
架构·系统架构·harmonyos·鸿蒙
HarmonyOS小助手2 天前
鸿蒙版微信小程序不可用,一文告诉你10分钟修复
harmonyos·鸿蒙·harmonyos next
小学生波波3 天前
鸿蒙ArkTS-发请求第三方接口显示实时新闻列表页面
华为·harmonyos·arkts·鸿蒙·请求第三方接口
王二蛋和他的狗3 天前
HarmonyOS运动开发:如何绘制运动速度轨迹
鸿蒙