鸿蒙Harmony(十)动画

属性动画

属性动画是通过设置组件的animation属性来给组件添加动画,当组件的width、height、Opacity、backgroundColor、scale 、rotate、translate等属性变更时,可以实现渐变过渡效果。

代码示例

@Entry
@Component
struct AnimPage {
   
  @State message: string = 'Hello World'
  @State scaleX: number = 1
  @State scaleY: number = 1
  @State animChange: boolean = false

  build() {
   
    Row() {
   
      Column() {
   
        Button("按钮")
          .scale({
    x: this.scaleX, y: this.scaleY, centerX: '50%', centerY: 0 })
          .animation({
    duration: 1000, curve: Curve.Ease })
          .onClick(() => {
   
            if (this.animChange) {
   
              this.scaleX = 3
              this.scaleY = 3
            } else {
   
              this.scaleX = 1
              this.scaleY = 1
            }
            this.animChange = !this.animChange
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Start)
    }
    .height('100%')
  }
}

显式动画

显式动画是通过全局animateTo函数来修改组件属性,实现属性变化时的渐变过渡效果.

animateTo(value: AnimateParam, event: () => void): void

代码示例

@Entry
@Component
struct AnimPage {
   
  @State message: string = 'Hello World'
  @State itemAlign: HorizontalAlign = HorizontalAlign.Start
  itemAlignList: HorizontalAlign[] = [HorizontalAlign.Start, HorizontalAlign.Center, HorizontalAlign.End]
  index: number=0

  build() {
   
    Row() {
   
      Column() {
   
        Button("按钮")
          .onClick(() => {
   
            animateTo({
    duration: 1000, curve: Curve.EaseInOut }, () => {
   
              this.itemAlign = this.itemAlignList[(this.index+1)%this.itemAlignList.length]
              this.index++
            });
          })
      }
      .alignItems(this.itemAlign)
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Start)
    }
    .height('100%')
  }
}

组件转场动画

组件的插入、删除过程即为组件本身的转场过程,组件的插入、删除动画称为组件内转场动画。通过组件内转场动画,可定义组件出现、消失的效果。通过组件的transition属性来配置。
参考文档

transition(value: TransitionOptions)

transition函数的入参为组件内转场的效果,可以定义平移、透明度、旋转、缩放这几种转场样式的单个或者组合的转场效果,必须和animateTo一起使用才能产生组件转场效果。

代码示例

@Entry
@Component
struct AnimPage {
   
  @State message: string = 'Hello World'
  @State isBegin: boolean = false

  build() {
   
    Row() {
   
      Column() {
   
        Button("按钮")
          .onClick(() => {
   
            this.animAction()
          })
        if (this.isBegin) {
   
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .margin({
    top: 100 })
            .transition({
   
              type: TransitionType.Insert,
              opacity: 0,
              rotate: {
    angle: -360 },
              scale: {
    x: 0, y: 0 },
            })
        }
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Start)
    }
    .height('100%')
  }

  animAction() {
   
    animateTo({
    duration: 1000 }, () => {
   
      this.isBegin = true
    })
  }
}

相关推荐

  1. 鸿蒙Harmony动画

    2023-12-30 03:44:03       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 03:44:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 03:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 03:44:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 03:44:03       20 阅读

热门阅读

  1. 编程笔记 html5&css&js 002 一些基本概念

    2023-12-30 03:44:03       30 阅读
  2. 送你一台云电脑

    2023-12-30 03:44:03       37 阅读
  3. 系列十一、解压文件到指定目录

    2023-12-30 03:44:03       25 阅读
  4. 面向对象进阶-继承

    2023-12-30 03:44:03       39 阅读
  5. Linux模块编译

    2023-12-30 03:44:03       44 阅读
  6. C++三剑客之std::variant(一)

    2023-12-30 03:44:03       35 阅读
  7. install mpirun

    2023-12-30 03:44:03       47 阅读
  8. 基于SpringBoot的足球社区管理系统

    2023-12-30 03:44:03       44 阅读
  9. LeetCode 热题 100——1.两数之和

    2023-12-30 03:44:03       45 阅读
  10. web worker的介绍和使用(包含使用案例)

    2023-12-30 03:44:03       35 阅读