开发者在使用粒子动画时,可能会遇到以下几类典型场景:
针对以上场景,该如何实现?
import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(): void {
try {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
} catch (err) {
hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err));
}
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
AppStorage.setOrCreate('flag', true);
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
AppStorage.setOrCreate('flag', false);
}
}; @Entry
@Component
struct ParticleExample {
@StorageLink('flag') flag: boolean | undefined = undefined;
build() {
Stack() {
Text()
.width(300).height(300).backgroundColor('rgb(240, 250, 255)');
if (this.flag) {
Particle({
particles: [
{
emitter: {
particle: {
type: ParticleType.POINT, // 粒子类型
config: {
radius: 5 // 圆点半径
},
count: 100, // 粒子总数
},
},
color: {
range: ['rgb(39, 135, 217)', 'rgb(0, 74, 175)'], // 初始颜色范围
},
},
]
})
.width(250).height(250);
}
}.width('100%').height('100%').align(Alignment.Center);
}
} @Entry
@Component
struct ParticleFollowExample {
imageWidth: number = 40;
imageHeight: number = 40;
positionX: number = 100;
positionY: number = 0;
@State emitterProperties: Array<EmitterProperty> = [
{
index: 0,
emitRate: 0, // 每秒发射粒子数
position: { x: this.positionX, y: this.positionY }, // 设置粒子发射器位置
size: { width: 20, height: 20 }
}
];
build() {
Column() {
Stack() {
Particle({
particles: [
{
emitter: {
particle: {
type: ParticleType.IMAGE, // 粒子类型
config: {
src: $r('app.media.startIcon'), // 此处仅为示例,请开发者替换为可用图片
size: [this.imageWidth, this.imageHeight],
objectFit: ImageFit.Contain
},
lifetime: 1400, // 单个粒子的生命周期
lifetimeRange: 200, // 粒子生命周期取值范围
count: -1, // 粒子总数
},
emitRate: 0,
shape: ParticleEmitterShape.CIRCLE, // 发射器形状
size: [20, 20],
position: [100, 100]
},
color: {
range: [Color.White, Color.White]
},
opacity: {
range: [1.0, 1.0],
updater: {
type: ParticleUpdater.CURVE,
config: [
{
from: 1.0,
to: 0,
startMillis: 0,
endMillis: 1600,
curve: Curve.Linear
}
]
}
},
scale: {
range: [1.0, 1.0],
updater: {
type: ParticleUpdater.CURVE,
config: []
}
},
acceleration: {
speed: {
range: [300, 400],
updater: {
type: ParticleUpdater.RANDOM,
config: [0, 0]
}
},
angle: {
range: [0, 360],
updater: {
type: ParticleUpdater.RANDOM,
config: [0, 0]
}
}
},
spin: {
range: [0, 360],
updater: {
type: ParticleUpdater.CURVE,
config: [
{
from: 0,
to: 360,
startMillis: 0,
endMillis: 1500,
curve: Curve.EaseIn
}
]
}
},
},
]
})
.width('100%').height('100%')
.emitter(this.emitterProperties)
.gesture(
PanGesture()
.onActionUpdate((event: GestureEvent) => {
// 动态调整粒子发射器的位置
if (event && event.fingerList.length > 0) {
this.positionX = event.fingerList[0].localX;
this.positionY = event.fingerList[0].localY;
this.emitterProperties = [{
index: 0,
emitRate: 100,
position: { x: this.positionX, y: this.positionY },
size: { width: 20, height: 20 }
}];
}
})
.onActionEnd(() => {
this.emitterProperties = [{
index: 0,
emitRate: 0,
position: { x: this.positionX, y: this.positionY },
size: { width: 20, height: 20 }
}];
})
);
}.width('100%')
.height('100%')
.align(Alignment.Center);
}.width('100%')
.height('100%');
}
} const LIFE_TIME: number = 1000;
@Entry
@Component
struct ParticleMoveExample {
imageWidth: number = 40;
imageHeight: number = 40;
cnt: number = -1;
positionX: number = 200;
positionY: number = 500;
build() {
Column() {
Stack() {
Particle({
particles: [
{
emitter: {
particle: {
type: ParticleType.IMAGE, // 粒子类型
config: {
src: $r('app.media.startIcon'),
size: [this.imageWidth, this.imageHeight],
objectFit: ImageFit.Contain
},
lifetime: LIFE_TIME, // 单个粒子的生命周期
lifetimeRange: 100, // 粒子生命周期取值范围
count: this.cnt, // 粒子总数
},
emitRate: 3,
shape: ParticleEmitterShape.CIRCLE, // 发射器形状
size: [20, 20],
position: [this.positionX, this.positionY]
},
opacity: {
range: [1.0, 1.0],
updater: {
type: ParticleUpdater.CURVE,
config: [
{
from: 1.0,
to: 0,
startMillis: 0,
endMillis: LIFE_TIME,
curve: Curve.Linear
}
]
}
},
scale: {
range: [1.0, 1.0],
updater: {
type: ParticleUpdater.CURVE,
config: [
{
from: 1,
to: 0.2,
startMillis: 0,
endMillis: LIFE_TIME,
curve: Curve.EaseIn
}
]
}
},
velocity: {
speed: [300, 400],
angle: [-90, -90]
},
acceleration: {
speed: {
range: [300, 400],
updater: {
type: ParticleUpdater.RANDOM,
config: [0, 0]
}
},
angle: {
range: [-90, -90],
updater: {
type: ParticleUpdater.RANDOM,
config: [0, 0]
}
}
},
spin: {
range: [0, 0],
updater: {
type: ParticleUpdater.RANDOM,
config: [-60, 60]
}
},
}
]
})
.disturbanceFields([{
// 增加扰动场
strength: 5,
shape: DisturbanceFieldShape.RECT,
size: { width: 900, height: 900 },
position: { x: this.positionX, y: this.positionY },
feather: 15,
noiseScale: 10,
noiseFrequency: 15,
noiseAmplitude: 5
}])
.width('100%').height('100%');
}.width('100%')
.height('100%')
.align(Alignment.Center);
}.width('100%')
.height('100%');
}
}