OpenHarmony动画详解

简介

动画是组件的基础特性之一,精心设计的动画使 UI 变化更直观,平滑的动画效果能够很好地增强差异性功能的过渡,有助于改进应用程序的外观并改善用户体验。

OpenHarmony 动画分类:

  • 属性动画:组件的某些通用属性变化时,可以通过属性动画实现渐变过渡效果,提升用户体验。支持的属性包括 width、height、backgroundColor、opacity、scale、rotate、translate 等。
  • 显示动画:提供全局 animateTo 显式动画接口来指定由于闭包代码导致的状态变化插入过渡动效。
  • 转场动画
    • 页面间转场:在全局 pageTransition 方法内配置页面入场和页面退场时的自定义转场动效。
    • 组件内转场:组件内转场主要通过 transition 属性配置转场参数,在组件插入和删除时显示过渡动效,主要用于容器组件中的子组件插入和删除时,提升用户体验(需要配合animateTo 才能生效,动效时长、曲线、延时跟随 animateTo 中的配置)。
    • 共享元素转场:设置页面间转场时共享元素的转场动效。
  • 路径动画:设置组件进行位移动画时的运动路径。
  • 窗口动画:提供启动退出过程中控件动画和应用窗口联动动画能力。

动画详解

属性动画

通过控件的 animation 属性实现动画效果。

animation(value: {duration?: number, tempo?: number, curve?: string | Curve | ICurve, delay?:number, iterations: number, playMode?: PlayMode, onFinish?: () => void})

参数 类型 必填 描述
duration number 设置动画时长。单位为毫秒,默认动画时长为 1000 毫秒。 默认值:1000
tempo number 动画播放速度。数值越大,动画播放速度越快,数值越小,播放速度越慢 值为 0 时,表示不存在动画。 默认值:1
curve string Curve ICurve9+
delay number 设置动画延迟执行的时长。单位为毫秒,默认不延时播放。 默认值:0
iterations number 设置播放次数。默认播放一次,设置为-1 时表示无限次播放。 默认值:1
playMode PlayMode 设置动画播放模式,默认播放完成后重头开始播放。 默认值:PlayMode.Normal
onFinish () => void 状态回调,动画播放完成时触发。

示例

// xxx.ets
@Entry
@Component
struct AnimateToExample {
  @State widthSize: number = 250
  @State heightSize: number = 100
  @State rotateAngle: number = 0
  private flag: boolean = true

  build() {
    Column() {
      Button('change width and height')
        .width(this.widthSize)
        .height(this.heightSize)
        .margin(30)
        .onClick(() => {
          if (this.flag) {
            animateTo({
              duration: 2000,
              curve: Curve.EaseOut,
              iterations: 3,
              playMode: PlayMode.Normal,
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.widthSize = 100
              this.heightSize = 50
            })
          } else {
            animateTo({}, () => {
              this.widthSize = 250
              this.heightSize = 100
            })
          }
          this.flag = !this.flag
        })
      Button('change rotate angle')
        .margin(50)
        .rotate({ angle: this.rotateAngle })
        .onClick(() => {
          animateTo({
            duration: 1200,
            curve: Curve.Friction,
            delay: 500,
            iterations: -1, // 设置-1表示动画无限循环
            playMode: PlayMode.AlternateReverse,
            onFinish: () => {
              console.info('play end')
            }
          }, () => {
            this.rotateAngle = 90
          })
        })
    }.width('100%').margin({ top: 5 })
  }
}

显示动画

通过全局 animateTo 显式动画接口来定由于闭包代码导致的状态变化插入过渡动效。

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

参数 类型 是否必填 描述
value AnimateParam 设置动画效果相关参数。
event () => void 指定显示动效的闭包函数,在闭包函数中导致的状态变化系统会自动插入过渡动画。

AnimateParam 对象说明

名称 类型 描述
duration number 动画持续时间,单位为毫秒。 默认值:1000
tempo number 动画的播放速度,值越大动画播放越快,值越小播放越慢,为 0 时无动画效果。 默认值:1.0
curve Curve Curves
delay number 单位为 ms(毫秒),默认不延时播放。 默认值:0
iterations number 默认播放一次,设置为-1 时表示无限次播放。 默认值:1
playMode PlayMode 设置动画播放模式,默认播放完成后重头开始播放。 默认值:PlayMode.Normal
onFinish () => void 动效播放完成回调。

示例

// xxx.ets
@Entry
@Component
struct AnimateToExample {
  @State widthSize: number = 250
  @State heightSize: number = 100
  @State rotateAngle: number = 0
  private flag: boolean = true

  build() {
    Column() {
      Button('change width and height')
        .width(this.widthSize)
        .height(this.heightSize)
        .margin(30)
        .onClick(() => {
          if (this.flag) {
            animateTo({
              duration: 2000,
              curve: Curve.EaseOut,
              iterations: 3,
              playMode: PlayMode.Normal,
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.widthSize = 100
              this.heightSize = 50
            })
          } else {
            animateTo({}, () => {
              this.widthSize = 250
              this.heightSize = 100
            })
          }
          this.flag = !this.flag
        })
      Button('change rotate angle')
        .margin(50)
        .rotate({ angle: this.rotateAngle })
        .onClick(() => {
          animateTo({
            duration: 1200,
            curve: Curve.Friction,
            delay: 500,
            iterations: -1, // 设置-1表示动画无限循环
            playMode: PlayMode.AlternateReverse,
            onFinish: () => {
              console.info('play end')
            }
          }, () => {
            this.rotateAngle = 90
          })
        })
    }.width('100%').margin({ top: 5 })
  }
}

注:效果和属性动画等价

转场动画

页面间转场

在全局 pageTransition 方法内配置页面入场和页面退场时的自定义转场动效。

名称 参数 参数描述
PageTransitionEnter { type: RouteType, duration: number, curve:Curve string, delay: number }
PageTransitionExit { type: RouteType, duration: number, curve: Curve string, delay: number }

RouteType 枚举说明

名称 描述
Pop 重定向指定页面。PageA 跳转到 PageB 时,PageA 为 Exit+Pop,PageB 为 Enter+Pop。
Push 跳转到下一页面。PageB 返回至 PageA 时,PageA 为 Enter+Push,PageB 为 Exit+Push。
None 页面未重定向。

属性

参数名称 参数类型 必填 参数描述
slide SlideEffect 设置页面转场时的滑入滑出效果。 默认值:SlideEffect.Right
translate { x? : number string, y? : number string, z? : number
scale { x? : number, y? : number, z? : number, centerX? : number string, centerY? : number string }
opacity number 设置入场的起点透明度值或者退场的终点透明度值。 默认值:1

SlideEffect 枚举说明

名称 描述
Left 设置到入场时表示从左边滑入,出场时表示滑出到左边。
Right 设置到入场时表示从右边滑入,出场时表示滑出到右边。
Top 设置到入场时表示从上边滑入,出场时表示滑出到上边。
Bottom 设置到入场时表示从下边滑入,出场时表示滑出到下边。

事件

事件 功能描述
onEnter(event: (type?: RouteType, progress?: number) => void) 回调入参为当前入场动画的归一化进度[0 - 1]。 - type:跳转方法。 - progress:当前进度。
onExit(event: (type?: RouteType, progress?: number) => void) 回调入参为当前退场动画的归一化进度[0 - 1]。 - type:跳转方法。 - progress:当前进度。

组件内转场

组件内转场主要通过 transition 属性配置转场参数,在组件插入和删除时显示过渡动效,主要用于容器组件中的子组件插入和删除时,提升用户体验(需要配合 animateTo) 才能生效,动效时长、曲线、延时跟随 animateTo 中的配置)。

属性

名称 参数类型 参数描述
transition TransitionOptions 所有参数均为可选参数,详细描述见 TransitionOptions 参数说明。

TransitionOptions 参数说明

参数名称 参数类型 必填 参数描述
type TransitionType 默认包括组件新增和删除。 默认值:TransitionType.All****说明:不指定 Type 时说明插入删除使用同一种效果。
opacity number 设置组件转场时的透明度效果,为插入时起点和删除时终点的值。 默认值:1
translate { x? : number, y? : number, z? : number } 设置组件转场时的平移效果,为插入时起点和删除时终点的值。 -x:横向的平移距离。 -y:纵向的平移距离。 -z:竖向的平移距离。
scale { x? : number, y? : number, z? : number, centerX? : number, centerY? : number } 设置组件转场时的缩放效果,为插入时起点和删除时终点的值。 -x:横向放大倍数(或缩小比例)。 -y:纵向放大倍数(或缩小比例)。 -z:竖向放大倍数(或缩小比例)。 - centerX、centerY 缩放中心点。 - 中心点为 0 时,默认的是组件的左上角。
rotate { x?: number, y?: number, z?: number, angle?: Angle, centerX?: Length, centerY?: Length } 设置组件转场时的旋转效果,为插入时起点和删除时终点的值。 -x:横向的旋转向量。 -y:纵向的旋转向量。 -z:竖向的旋转向量。 - centerX,centerY 指旋转中心点。 - 中心点为(0,0)时,默认的是组件的左上角。

示例

// xxx.ets
@Entry
@Component
struct TransitionExample {
  @State flag: boolean = true
  @State show: string = 'show'

  build() {
    Column() {
      Button(this.show).width(80).height(30).margin(30)
        .onClick(() => {
          // 点击Button控制Image的显示和消失
          animateTo({ duration: 1000 }, () => {
            if (this.flag) {
              this.show = 'hide'
            } else {
              this.show = 'show'
            }
            this.flag = !this.flag
          })
        })
      if (this.flag) {
        // Image的显示和消失配置为不同的过渡效果
        Image($r('app.media.testImg')).width(300).height(300)
          .transition({ type: TransitionType.Insert, scale: { x: 0, y: 1.0 } })
          .transition({ type: TransitionType.Delete, rotate: { angle: 180 } })
      }
    }.width('100%')
  }
}

共享元素转场

设置页面间转场时共享元素的转场动效。

属性

名称 参数 参数描述
sharedTransition id: string, { duration?: number, curve?: Curve string, delay?: number, motionPath?: { path: string, form?: number, to?: number, rotatable?: boolean }, zIndex?: number, type?:SharedTransitionEffectType}

示例

示例代码为点击图片跳转页面时,显示共享元素图片的自定义转场动效。

// xxx.ets
@Entry
@Component
struct SharedTransitionExample {
  @State active: boolean = false

  build() {
    Column() {
      Navigator({ target: 'pages/PageB', type: NavigationType.Push }) {
        Image($r('app.media.ic_health_heart')).width(50).height(50)
          .sharedTransition('sharedImage', { duration: 800, curve: Curve.Linear, delay: 100 })
      }.padding({ left: 20, top: 20 })
      .onClick(() => {
        this.active = true
      })
    }
  }
}

// PageB.ets
@Entry
@Component
struct pageBExample {
  build() {
    Stack() {
      Image($r('app.media.ic_health_heart')).width(150).height(150).sharedTransition('sharedImage')
    }.width('100%').height('100%')
  }
}

路径动画

设置组件进行位移动画时的运动路径。

属性

名称 参数类型 默认值 描述
motionPath { path: string, from?: number, to?: number, rotatable?: boolean }****说明:path 中支持使用 start 和 end 进行起点和终点的替代,如: 'Mstart.x start.y L50 50 Lend.x end.y Z' { '', 0.0, 1.0, false } 设置组件的运动路径,入参说明如下: - path:位移动画的运动路径,使用 svg 路径字符串。 - from:运动路径的起点,默认为 0.0。 - to:运动路径的终点,默认为 1.0。 - rotatable:是否跟随路径进行旋转。

示例

// xxx.ets
@Entry
@Component
struct MotionPathExample {
  @State toggle: boolean = true

  build() {
    Column() {
      Button('click me')
        // 执行动画:从起点移动到(300,200),再到(300,500),再到终点
        .motionPath({ path: 'Mstart.x start.y L300 200 L300 500 Lend.x end.y', from: 0.0, to: 1.0, rotatable: true })
        .onClick(() => {
          animateTo({ duration: 4000, curve: Curve.Linear }, () => {
            this.toggle = !this.toggle // 通过this.toggle变化组件的位置
          })
        })
    }.width('100%').height('100%').alignItems(this.toggle ? HorizontalAlign.Start : HorizontalAlign.Center)
  }
}

窗口动画

窗口动画管理器,可以监听应用启动退出时应用的动画窗口,提供启动退出过程中控件动画和应用窗口联动动画能力。

导入模块

import windowAnimationManager from '@ohos.animation.windowAnimationManager'

windowAnimationManager.setController

setController(controller: WindowAnimationController): void

设置窗口动画控制器

在使用 windowAnimationManager 的其他接口前,需要预先调用本接口设置窗口动画控制器。

参数:

参数名 类型 必填 说明
controller WindowAnimationController 窗口动画的控制器。

windowAnimationManager.minimizeWindowWithAnimation

minimizeWindowWithAnimation(windowTarget: WindowAnimationTarget): Promise

最小化动画目标窗口,并返回动画完成的回调。使用 Promise 异步回调。

参数:

参数名 类型 必填 说明
windowTarget WindowAnimationTarget 动画目标窗口。

返回值:

类型 说明
PromiseWindowAnimationFinishedCallback Promise 对象,返回动画完成的回调。

WindowAnimationController

窗口动画控制器。在创建一个 WindowAnimationController 对象时,需要实现其中的所有回调函数。

onStartAppFromLauncher

onStartAppFromLauncher(startingWindowTarget: WindowAnimationTarget,finishCallback: WindowAnimationFinishedCallback): void

从桌面启动应用时的回调。

参数名 类型 必填 说明
startingWindowTarget WindowAnimationTarget 动画目标窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onStartAppFromRecent

onStartAppFromRecent(startingWindowTarget: WindowAnimationTarget,finishCallback:WindowAnimationFinishedCallback): void

从最近任务列表启动应用时的回调。

参数名 类型 必填 说明
startingWindowTarget WindowAnimationTarget 动画目标窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onStartAppFromOther

onStartAppFromOther(startingWindowTarget: WindowAnimationTarget,finishCallback: WindowAnimationFinishedCallback): void

从除了桌面和最近任务列表以外其他地方启动应用时的回调。

参数名 类型 必填 说明
startingWindowTarget WindowAnimationTarget 动画目标窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onAppTransition

onAppTransition(fromWindowTarget: WindowAnimationTarget, toWindowTarget: WindowAnimationTarget,finishCallback: WindowAnimationFinishedCallback): void

应用转场时的回调。

参数名 类型 必填 说明
fromWindowTarget WindowAnimationTarget 转场前的动画窗口。
toWindowTarget WindowAnimationTarget 转场后的动画窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onMinimizeWindow

onMinimizeWindow(minimizingWindowTarget: WindowAnimationTarget,finishCallback: WindowAnimationFinishedCallback): void

最小化窗口时的回调。

参数名 类型 必填 说明
minimizingWindowTarget WindowAnimationTarget 动画目标窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onCloseWindow

onCloseWindow(closingWindowTarget: WindowAnimationTarget,finishCallback: WindowAnimationFinishedCallback): void

关闭窗口时的回调。

参数名 类型 必填 说明
closingWindowTarget WindowAnimationTarget 动画目标窗口。
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onScreenUnlock

onScreenUnlock(finishCallback: WindowAnimationFinishedCallback): void

屏幕解锁时的回调。

参数名 类型 必填 说明
finishCallback WindowAnimationFinishedCallback 动画完成后的回调。

onWindowAnimationTargetsUpdate

onWindowAnimationTargetsUpdate(fullScreenWindowTarget: WindowAnimationTarget, floatingWindowTargets: Array): void

动画目标窗口更新时的回调

参数名 类型 必填 说明
fullScreenWindowTarget WindowAnimationTarget 全屏状态的动画目标窗口。
floatingWindowTargets Array WindowAnimationTarget 悬浮状态的动画目标窗口

WindowAnimationFinishedCallback

动画完成后的回调。
onAnimationFinish
onAnimationFinish():void

结束本次动画。

应用层使用

OpenHarmony 中应用层的窗口动画定义在 Launcher 系统应用中,由 Launcher 应用统一规范应用的窗口动画

WindowController 的实现见 WindowAnimationControllerImpl.ts,定义了 onStartAppFromLauncher、onStartAppFromRecent、onStartAppFromOther、onAppTransition、onMinimizeWindow、onCloseWindow、onScreenUnlock 等实现方式

import Prompt from '@ohos.prompt';
import windowAnimationManager from '@ohos.animation.windowAnimationManager';
import { CheckEmptyUtils } from '@ohos/common';
import { Log } from '@ohos/common';
import RemoteConstants from '../../constants/RemoteConstants';

const TAG = 'WindowAnimationControllerImpl';

class WindowAnimationControllerImpl implements windowAnimationManager.WindowAnimationController {
  onStartAppFromLauncher(startingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                         finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void
  {
    Log.showInfo(TAG, `remote window animaion onStartAppFromLauncher`);
    this.setRemoteAnimation(startingWindowTarget, null, finishCallback, RemoteConstants.TYPE_START_APP_FROM_LAUNCHER);
    this.printfTarget(startingWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onStartAppFromRecent(startingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                       finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void {
    Log.showInfo(TAG, `remote window animaion onStartAppFromRecent`);
    this.setRemoteAnimation(startingWindowTarget, null, finishCallback, RemoteConstants.TYPE_START_APP_FROM_RECENT);
    this.printfTarget(startingWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onStartAppFromOther(startingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                      finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void {
    Log.showInfo(TAG, `remote window animaion onStartAppFromOther`);
    this.setRemoteAnimation(startingWindowTarget, null, finishCallback, RemoteConstants.TYPE_START_APP_FROM_OTHER);
    this.printfTarget(startingWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onAppTransition(fromWindowTarget: windowAnimationManager.WindowAnimationTarget,
                  toWindowTarget: windowAnimationManager.WindowAnimationTarget,
                  finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void{
    Log.showInfo(TAG, `remote window animaion onAppTransition`);
    this.setRemoteAnimation(toWindowTarget, fromWindowTarget, finishCallback, RemoteConstants.TYPE_APP_TRANSITION);
    this.printfTarget(fromWindowTarget);
    this.printfTarget(toWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onMinimizeWindow(minimizingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                   finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void {
    Log.showInfo(TAG, `remote window animaion onMinimizeWindow`);
    this.setRemoteAnimation(null, minimizingWindowTarget, finishCallback, RemoteConstants.TYPE_MINIMIZE_WINDOW);
    this.printfTarget(minimizingWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onCloseWindow(closingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void {
    Log.showInfo(TAG, `remote window animaion onCloseWindow`);
    this.setRemoteAnimation(null, closingWindowTarget, finishCallback, RemoteConstants.TYPE_CLOSE_WINDOW);
    this.printfTarget(closingWindowTarget);
    finishCallback.onAnimationFinish();
  }

  onScreenUnlock(finishCallback: windowAnimationManager.WindowAnimationFinishedCallback): void {
    Log.showInfo(TAG, `remote window animaion onScreenUnlock`);
    this.setRemoteAnimation(null, null, finishCallback, RemoteConstants.TYPE_SCREEN_UNLOCK);
    finishCallback.onAnimationFinish();
  }

  onWindowAnimationTargetsUpdate(fullScreenWindowTarget: windowAnimationManager.WindowAnimationTarget, floatingWindowTargets: Array<windowAnimationManager.WindowAnimationTarget>): void {}

  printfTarget(target: windowAnimationManager.WindowAnimationTarget): void {
    if (CheckEmptyUtils.isEmpty(target) || CheckEmptyUtils.isEmpty(target.windowBounds)) {
      Log.showInfo(TAG, `remote window animaion with invalid target`);
      return;
    }
    Log.showInfo(TAG, `remote window animaion bundleName: ${target.bundleName} abilityName: ${target.abilityName}`);
    Log.showInfo(TAG, `remote window animaion windowBounds left: ${target.windowBounds.left} top: ${target.windowBounds.top}
      width: ${target.windowBounds.width} height: ${target.windowBounds.height} radius: ${target.windowBounds.radius}`);
  }

  private setRemoteAnimation(startingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                             closingWindowTarget: windowAnimationManager.WindowAnimationTarget,
                             finishCallback: windowAnimationManager.WindowAnimationFinishedCallback,
                             remoteAnimationType: number): void {
    if (!CheckEmptyUtils.isEmpty(startingWindowTarget)) {
      AppStorage.SetOrCreate('startingWindowTarget', startingWindowTarget);
    }

    if (!CheckEmptyUtils.isEmpty(closingWindowTarget)) {
      AppStorage.SetOrCreate('closingWindowTarget', closingWindowTarget);
    }

    if (!CheckEmptyUtils.isEmpty(finishCallback)) {
      AppStorage.SetOrCreate('remoteAnimationFinishCallback', finishCallback);
    }

    AppStorage.SetOrCreate('remoteAnimationType', remoteAnimationType);
  }
}

export default WindowAnimationControllerImpl

RemoteWindowWrapper.ets 中定义了具体的窗口动画实现效果(通过 animateTo 实现具体的动画效果)

calculateAppProperty(remoteVo: RemoteVo, finishCallback: windowAnimationManager.WindowAnimationFinishedCallback) {
  Log.showDebug(TAG, `calculateAppProperty ${remoteVo.remoteAnimationType}`);
  if (remoteVo.remoteAnimationType == RemoteConstants.TYPE_START_APP_FROM_LAUNCHER) {
    Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION);
    const callback = Object.assign(finishCallback);
    const count = remoteVo.count;
    localEventManager.sendLocalEventSticky(EventConstants.EVENT_ANIMATION_START_APPLICATION, null);
    animateTo({
      duration: 180,
      delay: 100,
      curve: Curve.Friction,
      onFinish: () => {
      }
    }, () => {
      remoteVo.startAppIconWindowAlpha = 0.0;
      remoteVo.remoteWindowWindowAlpha = 1.0;
    })

    animateTo({
      duration: 500,
      // @ts-ignore
      curve: curves.springMotion(0.32, 0.99, 0),
      onFinish: () => {
        callback.onAnimationFinish();
        Trace.end(Trace.CORE_METHOD_START_APP_ANIMATION);
        const startCount: number = AppStorage.Get(remoteVo.remoteWindowKey);
        Log.showDebug(TAG, `calculateAppProperty ${remoteVo.remoteAnimationType}, count: ${count}, startCount: ${startCount}`);
        if (startCount === count || count == 0) {
          this.removeRemoteWindowFromList(remoteVo.remoteWindowKey);
          AppStorage.SetOrCreate(remoteVo.remoteWindowKey, 0);
        }
      }
    }, () => {
      remoteVo.remoteWindowScaleX = 1.0;
      remoteVo.remoteWindowScaleY = 1.0;
      remoteVo.remoteWindowTranslateX = 0.0;
      remoteVo.remoteWindowTranslateY = 0.0;
      remoteVo.startAppIconScaleX = remoteVo.mScreenWidth / remoteVo.iconInfo?.appIconSize;
      remoteVo.startAppIconTranslateX = remoteVo.mScreenWidth / 2 - remoteVo.iconInfo?.appIconPositionX - remoteVo.iconInfo?.appIconSize / 2;
      remoteVo.remoteWindowRadius = 0;
      if (remoteVo.startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_CARD) {
        remoteVo.startAppIconScaleY = remoteVo.mScreenHeight / remoteVo.iconInfo?.appIconHeight;
        remoteVo.startAppIconTranslateY = remoteVo.mScreenHeight / 2 + px2vp(remoteVo.target.windowBounds.top) - remoteVo.iconInfo?.appIconPositionY - remoteVo.iconInfo?.appIconHeight / 2;
      } else {
        remoteVo.startAppIconScaleY = remoteVo.mScreenHeight / remoteVo.iconInfo?.appIconSize;
        remoteVo.startAppIconTranslateY = remoteVo.mScreenHeight / 2 + px2vp(remoteVo.target.windowBounds.top) - remoteVo.iconInfo?.appIconPositionY - remoteVo.iconInfo?.appIconSize / 2;
      }
    })
  } else if (remoteVo.remoteAnimationType  == RemoteConstants.TYPE_MINIMIZE_WINDOW) {
    Trace.start(Trace.CORE_METHOD_CLOSE_APP_ANIMATION);
    const res = remoteVo.calculateCloseAppProperty();
    const callback = Object.assign(finishCallback);
    const count = remoteVo.count;
    localEventManager.sendLocalEventSticky(EventConstants.EVENT_ANIMATION_CLOSE_APPLICATION, null);
    animateTo({
      duration: 700,
      // @ts-ignore
      curve: curves.springMotion(0.40, 0.99, 0),
      onFinish: () => {
        callback.onAnimationFinish();
        Trace.end(Trace.CORE_METHOD_CLOSE_APP_ANIMATION);
        const startCount: number = AppStorage.Get(remoteVo.remoteWindowKey);
        Log.showDebug(TAG, `calculateAppProperty ${remoteVo.remoteAnimationType}, count: ${count}, startCount: ${startCount}`);
        if (startCount === count || count == 0) {
          this.removeRemoteWindowFromList(remoteVo.remoteWindowKey);
          AppStorage.SetOrCreate(remoteVo.remoteWindowKey, 0);
        }
      }
    }, () => {
      remoteVo.remoteWindowScaleX = 1 / res.closeAppCalculateScaleX;
      remoteVo.remoteWindowScaleY = 1 / res.closeAppCalculateScaleY;
      remoteVo.remoteWindowTranslateX = res.closeAppCalculateTranslateX;
      remoteVo.remoteWindowTranslateY = res.closeAppCalculateTranslateY;

      remoteVo.startAppIconScaleX = 1.0;
      remoteVo.startAppIconScaleY = 1.0;
      remoteVo.startAppIconTranslateX = 0.0;
      remoteVo.startAppIconTranslateY = 0.0;
      remoteVo.remoteWindowRadius = 96;
    })

    animateTo({
      duration: 140,
      delay: 350,
      curve: Curve.Friction,
      onFinish: () => {
      }
    }, () => {
      remoteVo.startAppIconWindowAlpha = 1.0;
      remoteVo.remoteWindowWindowAlpha = 0;
    })
  } else if (remoteVo.remoteAnimationType  == RemoteConstants.TYPE_CLOSE_WINDOW) {
  } else if (remoteVo.remoteAnimationType  == RemoteConstants.TYPE_APP_TRANSITION) {
    const callback = Object.assign(finishCallback);
    animateTo({
      duration: 500,
      curve: Curve.Friction,
      onFinish: () => {
        callback.onAnimationFinish();
        this.removeRemoteWindowFromList(remoteVo.remoteWindowKey);
      }
    }, () => {
      remoteVo.remoteWindowRadius = 0;
      remoteVo.remoteWindowTranslateX = 0;
      remoteVo.fromRemoteWindowTranslateX = px2vp(remoteVo.fromWindowTarget?.windowBounds.left - remoteVo.fromWindowTarget?.windowBounds.width);
    })

    animateTo({
      duration: 150,
      curve: Curve.Friction,
      onFinish: () => {
        callback.onAnimationFinish();
        this.removeRemoteWindowFromList(remoteVo.remoteWindowKey);
      }
    }, () => {
      remoteVo.remoteWindowScaleX = 0.9
      remoteVo.remoteWindowScaleY = 0.9
      remoteVo.fromRemoteWindowScaleX = 0.9
      remoteVo.fromRemoteWindowScaleY = 0.9
    })

    animateTo({
      duration: 350,
      delay: 150,
      curve: Curve.Friction,
      onFinish: () => {
        callback.onAnimationFinish();
        this.removeRemoteWindowFromList(remoteVo.remoteWindowKey);
      }
    }, () => {
      remoteVo.remoteWindowScaleX = 1.0
      remoteVo.remoteWindowScaleY = 1.0
      remoteVo.fromRemoteWindowScaleX = 1.0
      remoteVo.fromRemoteWindowScaleY = 1.0
    })
  }
}

注:若想修改系统的窗口动画效果,可通过修改对应的动画实现

底层实现

窗口动画的底层实现具体见:foundation\window\window_manager\wmserver\src\remote_animation.cpp

WMError RemoteAnimation::SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller)
{
    WLOGFI("RSWindowAnimation: set window animation controller!");
    if (!isRemoteAnimationEnable_) {
        WLOGE("RSWindowAnimation: failed to set window animation controller, remote animation is not enabled");
        return WMError::WM_ERROR_NO_REMOTE_ANIMATION;
    }
    if (controller == nullptr) {
        WLOGFE("RSWindowAnimation: failed to set window animation controller, controller is null!");
        return WMError::WM_ERROR_NULLPTR;
    }

    if (windowAnimationController_ != nullptr) {
        WLOGFI("RSWindowAnimation: maybe user switch!");
    }

    windowAnimationController_ = controller;
    return WMError::WM_OK;
}

RSIWindowAnimationController 具体定义见:foundation\graphic\graphic_2d\interfaces\kits\napi\graphic\animation\window_animation_manager\rs_window_animation_controller.cpp

void RSWindowAnimationController::HandleOnStartApp(StartingAppType type,
    const sptr<RSWindowAnimationTarget>& startingWindowTarget,
    const sptr<RSIWindowAnimationFinishedCallback>& finishedCallback)
{
    WALOGD("Handle on start app.");
    NativeValue* argv[] = {
        RSWindowAnimationUtils::CreateJsWindowAnimationTarget(engine_, startingWindowTarget),
        RSWindowAnimationUtils::CreateJsWindowAnimationFinishedCallback(engine_, finishedCallback),
    };

    switch (type) {
        case StartingAppType::FROM_LAUNCHER:
            CallJsFunction("onStartAppFromLauncher", argv, ARGC_TWO);
            break;
        case StartingAppType::FROM_RECENT:
            CallJsFunction("onStartAppFromRecent", argv, ARGC_TWO);
            break;
        case StartingAppType::FROM_OTHER:
            CallJsFunction("onStartAppFromOther", argv, ARGC_TWO);
            break;
        default:
            WALOGE("Unknow starting app type.");
            break;
    }
}

void RSWindowAnimationController::HandleOnAppTransition(const sptr<RSWindowAnimationTarget>& fromWindowTarget,
    const sptr<RSWindowAnimationTarget>& toWindowTarget,
    const sptr<RSIWindowAnimationFinishedCallback>& finishedCallback)
{
    WALOGD("Handle on app transition.");
    NativeValue* argv[] = {
        RSWindowAnimationUtils::CreateJsWindowAnimationTarget(engine_, fromWindowTarget),
        RSWindowAnimationUtils::CreateJsWindowAnimationTarget(engine_, toWindowTarget),
        RSWindowAnimationUtils::CreateJsWindowAnimationFinishedCallback(engine_, finishedCallback),
    };
    CallJsFunction("onAppTransition", argv, ARGC_THREE);
}
...

通过 CallJsFunction 实现具体的 napi 调用,实现具体的动画效果

写在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙

  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
  • 想要获取更多完整鸿蒙最新学习知识点,请移步前往小编:https://gitee.com/MNxiaona/733GH/blob/master/jianshu
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容