HarmonyOS鸿蒙NEXT之各类动画实现详解

HarmonyOS鸿蒙NEXT之各类动画实现详解 动画官方链接: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-animation-V5

1. 渐变动画(淡入淡出)

通过修改组件的透明度实现淡入淡出效果。

代码实现

// FadeAnimation.ets
@Component
struct FadeAnimationExample {
  @State opacityValue: number = 0.2;

  build() {
    Column() {
      Button('点击淡入/淡出')
        .opacity(this.opacityValue)
        .onClick(() => {
          animateTo({ duration: 1000, curve: Curve.EaseInOut }, () => {
            this.opacityValue = this.opacityValue === 0.2 ? 1 : 0.2;
          });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

2. 位移动画(平移)

通过修改组件的位置属性实现水平移动。

代码实现

// TranslateAnimation.ets
@Component
struct TranslateAnimationExample {
  @State translateX: number = 0;

  build() {
    Column() {
      Button('向右移动')
        .translate({ x: this.translateX })
        .onClick(() => {
          animateTo({ duration: 800, curve: Curve.Spring }, () => {
            this.translateX = this.translateX === 0 ? 200 : 0;
          });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

3. 旋转动画

通过修改组件的旋转角度实现旋转效果。

代码实现

// RotateAnimation.ets
@Component
struct RotateAnimationExample {
  @State rotateValue: number = 0;

  build() {
    Column() {
      Image($r('app.media.icon'))
        .width(100)
        .height(100)
        .rotate({ angle: this.rotateValue })
        .onClick(() => {
          animateTo({ duration: 1200 }, () => {
            this.rotateValue += 360;
          });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

4. 组合动画(缩放 + 颜色渐变)

同时修改多个属性实现复合动画效果。

代码实现

// CompositeAnimation.ets
@Component
struct CompositeAnimationExample {
  @State scaleValue: number = 1;
  @State colorValue: Color = Color.Blue;

  build() {
    Column() {
      Text('缩放与变色')
        .fontSize(20)
        .fontColor(Color.White)
        .backgroundColor(this.colorValue)
        .padding(20)
        .scale({ x: this.scaleValue, y: this.scaleValue })
        .onClick(() => {
          animateTo({ duration: 500 }, () => {
            this.scaleValue = this.scaleValue === 1 ? 1.5 : 1;
            this.colorValue = this.colorValue === Color.Blue ? Color.Red : Color.Blue;
          });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

5. 转场动画(页面切换)

使用 transition 实现组件出现/消失的过渡效果。

代码实现

// TransitionAnimation.ets
@Component
struct TransitionAnimationExample {
  @State isShow: boolean = true;

  build() {
    Column() {
      if (this.isShow) {
        Text('消失动画')
          .fontSize(24)
          .transition({ type: TransitionType.Insert, opacity: 0, translate: { x: 100 } })
      }

      Button(this.isShow ? '隐藏' : '显示')
        .onClick(() => {
          animateTo({ duration: 600 }, () => {
            this.isShow = !this.isShow;
          });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

以上是一些动画小案例,希望可以在项目中有用到,增加一些动感,谢谢!


更多关于HarmonyOS鸿蒙NEXT之各类动画实现详解的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

HarmonyOS鸿蒙NEXT中的动画实现主要依赖于ArkUI框架,提供了丰富的动画API和组件。以下是一些常见的动画实现方式:

  1. 属性动画:通过Animator类实现,可以控制组件的属性变化,如位置、大小、透明度等。例如,使用Animatorstart()stop()方法控制动画的启动和停止。

  2. 转场动画:Transition组件用于实现页面之间的转场效果。支持多种转场类型,如fadeslidescale等,通过transition属性进行配置。

  3. 关键帧动画:KeyframeAnimation允许定义关键帧,通过addKeyframe方法设置关键帧的属性值,系统会自动在关键帧之间进行插值计算。

  4. 路径动画:PathAnimation用于沿指定路径移动组件。通过Path类定义路径,然后使用PathAnimation沿路径移动组件。

  5. 组合动画:AnimationGroup用于组合多个动画,可以同时或顺序播放多个动画效果。通过addAnimation方法添加动画,设置playMode控制播放顺序。

  6. 物理动画:SpringAnimation模拟物理弹簧效果,通过设置spring参数控制弹簧的刚度、阻尼等属性,实现自然流畅的动画效果。

  7. 自定义动画:通过继承Animation类,实现自定义动画逻辑。重写onUpdate方法,在每一帧更新时执行自定义的动画效果。

这些动画API和组件可以灵活组合,实现复杂的动画效果,提升用户界面的交互体验。

更多关于HarmonyOS鸿蒙NEXT之各类动画实现详解的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙NEXT中,动画实现主要通过以下几种方式:

  1. 属性动画:通过修改组件的属性值(如位置、透明度等)来实现动画效果。可使用 Animator 类进行控制,支持线性、加速、减速等插值器。

  2. 帧动画:通过逐帧播放图片序列实现动画。使用 ImageAnimator 组件,适合复杂的动画效果。

  3. 转场动画:在页面或组件切换时使用,通过 PageTransitionComponentTransition 实现平滑过渡效果。

  4. 物理动画:模拟物理效果(如弹簧、摩擦力等),使用 SpringAnimationFrictionAnimation,增强用户体验。

  5. 自定义动画:通过 CanvasCustomAnimator 实现高度定制化的动画效果。

开发者可根据需求选择合适的动画方式,结合 ArkTS 语言和 ArkUI 框架,实现流畅的交互体验。

回到顶部