鸿蒙 HarmonyOs 动画效果 快速入门

打印 上一主题 下一主题

主题 540|帖子 540|积分 1620

一、理论

1.1 animation属性

名称参数类型必填形貌
durationnumber设置动画时长,默认值:1000,单元:毫秒
temponumber动画播放速度。数值越大,速度越快,默认为1
curvestring | Curve 设置动画曲线。
默认值:Curve.EaseInOut,平滑开始和结束
delaynumber设置动画延迟实行的时长。默认值为0,毫秒
iterationsnumber 设置播放次数。
默认值:1,取值范围:[-1, +∞]
设置为 -1 表现无穷次播放。
playModePlayMode设置动画播放模式,默认播放完成后重头开始播放。默认值:PlayMode.Normal
onFinish()=>void状态回调,动画播放完成时触发

1.2 组件转场效果

参数名参数类型必填形貌
typeTransitionType类型,默认组件爱你新增和删除,默认为ALL
opacitynumber不透明度,为插入时出发点和删除时终点的值。默认值为1,取值范围为[0,1]
translate {
        x?:number | string,
        y?:number | string,
        z?:number | string
}
平移效果,为插入时出发点和删除时终点的值。
-x:横向的平移距离。
-y:纵向的平移距离。
-z:竖向的平移距离。
scale {
        x?:number,
        y?:number,
        z?:number,
        centerX?:number | string,
        centerY?:number | string
}
缩放效果,为插入时出发点和删除时终点的值。
-x:横向的缩放倍数
-y:纵向的缩放倍数
-z:为当前的二维显示,该参数无效
-centerX:centerY指缩放中心点,centerX和centerY默认值是"50%"
-中心点为0时,默认是组件的左上角
rotate {
        x?:number,
        y?:number,
        z?:number,
        angle:number | string,
        centerX?:number | string,
        centerY?:number | string
}
旋转效果:
angle是旋转角度

二、实操

2.1 示例:旋转图片

示例代码:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State angle:number = 0
  5.   build(){
  6.     Column(){
  7.       //显示图片
  8.       Image($r('app.media.one'))
  9.         .width(120)
  10.         .rotate({
  11.           angle:this.angle,
  12.           centerX:'50%',
  13.           centerY:'50%'
  14.         })
  15.         .animation({
  16.           duration:1000,
  17.           onFinish:()=>{
  18.             console.log('结束时:'+this.angle)
  19.           }
  20.         })
  21.       Button('动画开始')
  22.         .onClick(()=>{
  23.           this.angle += 360
  24.         })
  25.     }
  26.     .width('100%')
  27.     .height('100%')
  28.   }
  29. }
复制代码
2.2 示例:平移、旋转、缩放

其中scale的值为缩放比例,以是1为初始值,2则表现原有的1倍
其中rotate的值为1则表现以该轴旋转,0则不以该轴旋转
示例效果:

示例代码:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State angle:number = 0
  5.   @State rotateX:number = 0
  6.   @State rotateY:number = 0
  7.   @State rotateZ:number = 0
  8.   @State translateX:number = 0
  9.   @State translateY:number = 0
  10.   @State scaleX:number =  1
  11.   @State scaleY:number =  1
  12.   build(){
  13.     Column(){
  14.       //显示图片
  15.       Image($r('app.media.one'))
  16.         .width(120)
  17.         .rotate({
  18.           angle:this.angle,
  19.           x:this.rotateX,
  20.           y:this.rotateY,
  21.           z:this.rotateZ,
  22.           centerX:'50%',
  23.           centerY: '50%',
  24.         })
  25.         .animation({
  26.           duration:1000,
  27.           onFinish:()=>{
  28.             console.log('结束时:'+this.angle)
  29.           }
  30.         })
  31.         .translate({
  32.           x:this.translateX,
  33.           y:this.translateY
  34.         })
  35.         .scale({
  36.           x:this.scaleX,
  37.           y:this.scaleY,
  38.           centerX:'50%',
  39.           centerY: '50%',
  40.         })
  41.       Blank()
  42.       Row(){
  43.         Column(){
  44.           Image($r('app.media.ic_public_arrow_up_0'))
  45.             .width(40)
  46.             .backgroundColor(Color.White)
  47.             .borderRadius(20)
  48.             .onClick(()=>{
  49.               this.translateY -= 50
  50.             })
  51.           Row(){
  52.             Image($r('app.media.ic_public_arrow_left'))
  53.               .width(40)
  54.               .backgroundColor(Color.White)
  55.               .borderRadius(20)
  56.               .onClick(()=>{
  57.                 this.translateX -= 50
  58.               })
  59.             Image($r('app.media.ic_public_arrow_right'))
  60.               .width(40)
  61.               .backgroundColor(Color.White)
  62.               .borderRadius(20)
  63.               .onClick(()=>{
  64.                 this.translateX += 50
  65.               })
  66.           }.width(130)
  67.           .justifyContent(FlexAlign.SpaceBetween)
  68.           Image($r('app.media.ic_public_arrow_down_0'))
  69.             .width(40)
  70.             .backgroundColor(Color.White)
  71.             .borderRadius(20)
  72.             .onClick(()=>{
  73.               this.translateY += 50
  74.             })
  75.         }.width(200)
  76.         Column({space:20}){
  77.           Button('旋转一圈动画开始')
  78.             .onClick(()=>{
  79.               this.rotateZ = 1
  80.               this.angle += 360
  81.             })
  82.           Button('放大一倍')
  83.             .onClick(()=>{
  84.               this.scaleX *= 2
  85.               this.scaleY *= 2
  86.             })
  87.           Button('缩小一倍')
  88.             .onClick(()=>{
  89.               this.scaleX /= 2
  90.               this.scaleY /= 2
  91.             })
  92.         }.padding(10)
  93.       }.width('100%')
  94.       .backgroundColor(Color.Gray)
  95.     }
  96.     .width('100%')
  97.     .height('100%')
  98.   }
  99. }
复制代码
2.3 示例:简朴摇骰子效果

示例效果:

示例代码:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State dice:Resource[] = [
  5.     $r('app.media.one'),
  6.     $r('app.media.two'),
  7.     $r('app.media.three'),
  8.     $r('app.media.four'),
  9.     $r('app.media.five'),
  10.     $r('app.media.six')
  11.   ]
  12.   @State currentDice:Resource = this.dice[0]
  13.   @State angle:number = 0
  14.   @State rotateX:number = 0
  15.   @State rotateY:number = 1
  16.   @State rotateZ:number = 0
  17.   @State opacityNumber:number = 1
  18.   @State runState:boolean = false
  19.   build(){
  20.     Column(){
  21.       //显示图片
  22.       Image(this.currentDice)
  23.         .width(120)
  24.         .rotate({
  25.           angle:this.angle,
  26.           x:this.rotateX,
  27.           y:this.rotateY,
  28.           z:this.rotateZ,
  29.           centerX:'50%',
  30.           centerY: '50%',
  31.         })
  32.         .animation({
  33.           duration:1000,
  34.           curve:Curve.EaseInOut,
  35.           onFinish:()=>{
  36.             this.opacityNumber = 1
  37.             console.log('结束时:'+this.angle)
  38.           },
  39.         })
  40.         .opacity(this.opacityNumber)
  41.       Button('摇骰子')
  42.         .width(200)
  43.         .onClick(()=>{
  44.           this.opacityNumber = 0.8
  45.           this.angle += 360
  46.           setTimeout(()=>{
  47.             this.currentDice = this.dice[Math.floor(Math.random() * this.dice.length)];
  48.           },1000)
  49.          
  50.         })
  51.       
  52.     }
  53.     .width('100%')
  54.     .height('100%')
  55.   }
  56. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表