HarmonyOS 动画必须搭配@State修饰的变量才能生效吗?

HarmonyOS
7h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

一般情况下动画是需要搭配@State修饰的变量才能生效,以下demo加上 Text().width(300).height(300).backgroundColor(Color.Black)就可以做动画了

@Entry
@Component
export default struct ParticleStar {
  aboutToAppear(): void {
  }

  build() {
    Stack() {
      Text()
        .width(300).height(300).backgroundColor(Color.Black)
      Particle({
        particles: [
          {
            emitter: {
              particle: {
                type: ParticleType.POINT, //粒子类型
                config: {
                  radius: 10//圆点半径
                },
                count: 500, //粒子总数
                lifetime: 10000//粒子生命周期,单位ms
              },
              emitRate: 10, //每秒发射粒子数
              position: [200, 0],
              shape: ParticleEmitterShape.RECTANGLE//发射器形状
            },
            color: {
              range: [Color.Red, Color.Yellow], //初始颜色范围
              updater: {
                type: ParticleUpdater.CURVE, //变化方式为曲线变化
                config: [
                  {
                    from: Color.White, //变化起始值
                    to: Color.Pink, //变化终点值
                    startMillis: 0, //开始时间
                    endMillis: 3000, //结束时间
                    curve: Curve.EaseIn//变化曲线
                  },
                  {
                    from: Color.Pink,
                    to: Color.Orange,
                    startMillis: 3000,
                    endMillis: 5000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: Color.Orange,
                    to: Color.Pink,
                    startMillis: 5000,
                    endMillis: 8000,
                    curve: Curve.EaseIn
                  },
                ]
              }
            },
            opacity: {
              range: [0.0, 1.0], //粒子透明度的初始值从【0.0到1.0】随机产生
              updater: {
                type: ParticleUpdater.CURVE, //透明度的变化方式是随机变化
                config: [
                  {
                    from: 0.0,
                    to: 1.0,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: 1.0,
                    to: 0.0,
                    startMillis: 5000,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            scale: {
              range: [0.0, 0.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0.0,
                    to: 0.5,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            acceleration: {
              //加速度的配置,从大小和方向两个维度变化,speed表示加速度大小,angle表示加速度方向
              speed: {
                range: [3, 9],
                updater: {
                  type: ParticleUpdater.RANDOM,
                  config: [1, 20]
                }
              },
              angle: {
                range: [90, 90]
              }
            }
          }
        ]
      }).width(300).height(300)
    }.width("100%").height("100%").align(Alignment.Center)
  }
}
分享
微博
QQ
微信
回复
5h前
相关问题
HarmonyOS " @State可以修饰ArrayList"
383浏览 • 1回复 待解决
@State 修饰变量值改变,界面不刷新
1511浏览 • 1回复 待解决
关于状态变量@state必须知道
1140浏览 • 1回复 待解决
var能否修饰ArkTS中变量
498浏览 • 1回复 待解决
LocalStorageLink修饰变量会自动保存
975浏览 • 1回复 待解决
定位是否必须联网才能成功?
1787浏览 • 1回复 待解决
ListItemGroup能跟LazyForEach搭配使用
860浏览 • 1回复 待解决
点击事件,@State 页面未生效,在线等
2815浏览 • 0回复 待解决