-
Notifications
You must be signed in to change notification settings - Fork 13
SamLeung edited this page Dec 10, 2018
·
3 revisions
代码中,你首先需要设置形状分发器
Bloom.with('activity')
.setParticleRadius(15)
.setShapeDistributor(new CircleShapeDistributor())
//or setShapeDistributor(new RectShapeDistributor())
//or setShapeDistributor(new StarShapeDistributor())
.boom(view);
public abstract class ParticleShapeDistributor {
public abstract ParticleShape getShape(BloomParticle particle);
}
允许你为每个粒子设置对应的形状
-
在类CircleShapeDistributor中, 设置每个粒子为圆形
-
在类 RectShapeDistributor中, 设置每个粒子为矩形
-
在类StarShapeDistributor中, 设置每个粒子为星型
怎么设置一个自定义形状分发器或者形状?
继承类 ParticleShapeDistributor 然后给每个粒子返回 形状 .
我们来看一下 形状的基类:
public abstract class ParticleShape {
private float mRadius;
private float mCenterX;
private float mCenterY;
private Path mPath;
public ParticleShape(float centerX, float centerY, float radius){
mCenterX = centerX;
mCenterY = centerY;
mRadius = radius;
mPath = new Path();
}
public float getRadius() {
return mRadius;
}
public float getCenterX() {
return mCenterX;
}
public float getCenterY() {
return mCenterY;
}
public abstract void generateShape(Path path);
public Path getShapePath(){
return mPath;
}
}
当你需要实现自定义粒子形状时,继承这个类,然后实现generateShape方法,你需要注意这里提供的参数是粒子的中心点坐标(x,y),以及它的最大半径
最后,让我们实现三种形状的随机效果:
代码:
Bloom.with(this)
.setParticleRadius(5)
.setShapeDistributor(new ParticleShapeDistributor() {
@Override
public ParticleShape getShape(BloomParticle particle) {
switch (mRandom.nextInt(3)){
case 0:
return new ParticleCircleShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
case 1:
return new ParticleRectShape(2, 2, particle.getInitialX(), particle.getInitialY(), particle.getRadius());//设置圆角效果
case 2:
return new ParticleStarShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
}
return new ParticleCircleShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
}
})
.setEffector(new BloomEffector.Builder()
.setDuration(800)
.setAnchor(view.getWidth() / 2, view.getHeight() / 2)
.build())
.boom(view);
截图: