文档管理中心
FAQ应用框架开发UI框架UI界面如何实现球形粒子动画效果

如何实现球形粒子动画效果

问题现象

如何实现一个球形粒子动画的效果?

效果预览

背景知识

粒子动画是一种通过在一定范围内随机生成的大量粒子的运动来组成的动画效果。在HarmonyOS中,这种动画效果主要通过Particle组件来实现。

解决方案

  1. 通过Stack布局函数构建了一个包含粒子特效的界面。
  2. 创建了一个Particle粒子系统,配置了粒子的各种属性,例如粒子的发射率、生命周期、颜色、透明度、缩放、加速度等。
  3. 设置了一个扰动场(Disturbance Fields),用于影响粒子的行为。
  4. 监听视图区域的变化,并根据新的视图尺寸调整扰动场的大小和位置。

完整示例参考如下:

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct CircleDemo {
  4. @State viewWidth: number = 0;
  5. @State viewHeight: number = 0;
  6. build() {
  7. Stack() {
  8. Particle({
  9. particles: [
  10. {
  11. emitter: {
  12. particle: {
  13. type: ParticleType.POINT, // 粒子类型
  14. config: {
  15. radius: 1 // 圆点半径
  16. },
  17. count: -1, // 粒子总数
  18. lifetime: 5000, // 粒子生命周期,单位ms
  19. lifetimeRange: 100 // 粒子生命周期取值范围,单位ms
  20. },
  21. emitRate: 200, // 每秒发射粒子数
  22. position: [0, 0],
  23. shape: ParticleEmitterShape.CIRCLE // 发射器形状
  24. },
  25. color: {
  26. range: [Color.White, Color.White] // 初始颜色范围
  27. },
  28. opacity: {
  29. range: [0.0, 1.0], // 粒子透明度的初始值从【0.01.0】随机产生
  30. updater: {
  31. type: ParticleUpdater.CURVE, // 透明度的变化方式是随机变化
  32. config: [
  33. {
  34. from: 0.0,
  35. to: 1.0,
  36. startMillis: 0,
  37. endMillis: 2500,
  38. curve: Curve.EaseIn
  39. },
  40. {
  41. from: 1.0,
  42. to: 0.0,
  43. startMillis: 2500,
  44. endMillis: 5000,
  45. curve: Curve.EaseIn
  46. }
  47. ]
  48. }
  49. },
  50. scale: {
  51. range: [0.0, 0.0],
  52. updater: {
  53. type: ParticleUpdater.CURVE,
  54. config: [
  55. {
  56. from: 0.0,
  57. to: 1.0,
  58. startMillis: 0,
  59. endMillis: 1000,
  60. curve: Curve.EaseIn
  61. },
  62. {
  63. from: 1.0,
  64. to: 0,
  65. startMillis: 1000,
  66. endMillis: 5000,
  67. curve: Curve.EaseIn
  68. }
  69. ]
  70. }
  71. },
  72. acceleration: {
  73. // 加速度的配置,从大小和方向两个维度变化,speed表示加速度大小,angle表示加速度方向
  74. speed: {
  75. range: [3, 9],
  76. updater: {
  77. type: ParticleUpdater.RANDOM,
  78. config: [1, 20]
  79. }
  80. },
  81. angle: {
  82. range: [0, 360]
  83. }
  84. },
  85. spin: {
  86. range: [0, 1.0],
  87. updater: {
  88. type: ParticleUpdater.CURVE,
  89. config: [
  90. {
  91. from: 0,
  92. to: 180,
  93. startMillis: 0,
  94. endMillis: 5000,
  95. curve: Curve.EaseIn
  96. }
  97. ]
  98. }
  99. },
  100. velocity: {
  101. speed: [20, 30],
  102. angle: [0.0, 1.0]
  103. }
  104. }
  105. ]
  106. })
  107. .width('100%')
  108. .height('100%')
  109. .backgroundColor(Color.Black)
  110. .disturbanceFields([{
  111. strength: 80, // 场强,表示场从中心向外的排斥力的强度,默认值0。正数表示排斥力方向朝外,负数表示吸引力,方向朝内
  112. shape: DisturbanceFieldShape.CIRCLE, // 场的形状
  113. size: { width: this.viewWidth, height: this.viewHeight }, // 场的大小
  114. position: { x: this.viewWidth / 2, y: this.viewHeight / 2 } // 场的位置
  115. }])
  116. .onAreaChange((oldValue: Area, newValue: Area) => {
  117. // 监听视图区域的变化,并根据新的视图尺寸调整扰动场的大小和位置
  118. this.viewWidth = newValue.width as number;
  119. this.viewHeight = newValue.height as number;
  120. });
  121. }
  122. }
  123. }

总结

Particle组件通过配置多个粒子属性来实现了一个动态的、具有视觉吸引力的粒子动画效果。

在 FAQ 中进行搜索
请输入您想要搜索的关键词