harmony 鸿蒙共享元素转场

  • 2022-08-09
  • 浏览 (629)

共享元素转场

当路由进行切换时,可以通过设置组件的 sharedTransition 属性将该元素标记为共享元素并设置对应的共享元素转场动效。

说明:

从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

属性

名称 参数 参数描述
sharedTransition id: string,
{
 duration?: number,
 curve?: Curve | string | ICurve10+,
 delay?: number,
 motionPath?:
{
 path: string,
 form?: number,
 to?: number,
 rotatable?: boolean
},
zIndex?: number,
type?: SharedTransitionEffectType
}
两个页面中id值相同且不为空字符串的组件即为共享元素,在页面转场时可显示共享元素转场动效。
- id:设置组件的id。
- duration:描述共享元素转场动效播放时长。
默认值:1000
单位:毫秒
取值范围:[0, +∞)
值为0时表示无动画。设置小于0的值时,按默认值1000处理。
- curve:默认曲线为Curve.Linear。
- delay:用来描述共享元素转场动效延迟播放的时长。
默认值:0
单位:毫秒
取值范围:[0, +∞)
设置小于0的值时,按值为0处理。
- motionPath:运动路径信息,详细说明请参考路径动画
{
- path:设置路径。
- from:设置起始值。
- to:设置终止值。
- rotatable:是否旋转。
默认值:false
}
- zIndex:设置Z轴。
默认值:0
- type:动画类型。
默认值:SharedTransitionEffectType.Exchange

说明:

type为SharedTransitionEffectType.Exchange时motionPath才会生效。

示例

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

// 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', { duration: 800, curve: Curve.Linear, delay: 100 })
    }.width('100%').height('100%')
  }
}

shared

你可能感兴趣的鸿蒙文章

harmony 鸿蒙基于ArkTS的声明式开发范式

harmony 鸿蒙@ohos.arkui.advanced.Counter(计数器组件)

harmony 鸿蒙@ohos.arkui.advanced.SegmentButton(分段按钮)

harmony 鸿蒙@ohos.multimedia.avCastPicker (投播组件)

harmony 鸿蒙属性动画

harmony 鸿蒙枚举说明

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙Checkbox

0  赞