-
Notifications
You must be signed in to change notification settings - Fork 13
Shape
In the code, you need to set the distributor firstly like below:
Bloom.with('activity')
.setParticleRadius(15)
.setShapeDistributor(new CircleShapeDistributor())
//or setShapeDistributor(new RectShapeDistributor())
//or setShapeDistributor(new StarShapeDistributor())
.boom(view);
The base class:
public abstract class ParticleShapeDistributor {
public abstract ParticleShape getShape(BloomParticle particle);
}
allows you to configure the corresponding shape for each particle.
-
In the CircleShapeDistributor class, it sets a circular shape for each particle.
-
In the RectShapeDistributor class, it sets a rectangle with rounded corners for each particle.
-
In the StarShapeDistributor class, it sets a starshape for each particle.
How to set a custom shape distributor or shape?
Inherit this class ParticleShapeDistributor and then return the corresponding shape for each particle.
Let's take a look at the base class of shape:
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;
}
}
When you need to implement a custom particle shape, inherit this class, and then implement the generateShape method, you need to pay attention to the parameters provided here are the particle's center point coordinates (x, y), and its maximum radius
Finally, let's implement the random effects of the three shapes.
Code:
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());//set rounded effect.
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);
screenshot: