Skip to content
SamLeung edited this page Dec 7, 2018 · 2 revisions

In this library, supports three shapes of particles

Circle

Circle

Rect

Rect

Star

Star

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);

What is the shape distributor?

The base class:

public abstract class ParticleShapeDistributor {
    public abstract ParticleShape getShape(BloomParticle particle);
}

allows you to configure the corresponding shape for each particle.

Custom shape distributor and shape

How to set a custom shape distributor or shape?

Custom distributor

Inherit this class ParticleShapeDistributor and then return the corresponding shape for each particle.

Custom shape

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:

Random