jinatonic / confetti

An Android particle system library for displaying confetti!
https://medium.com/@jinatonic/its-parfetti-time-f40634472608
Apache License 2.0
1.31k stars 151 forks source link

Confetti direction #30

Open odetoyama opened 7 years ago

odetoyama commented 7 years ago

As we know, the confetti direction is from middle to the edge of the container. Is there any function make it reversed, from edge of container to center of container?

odetoyama commented 7 years ago

I've figured it out how its works with this awesome library. The idea is to generate confetti randomly from the inner edges of the screen to move(setVelocityX, setVelocityY) to center of the screen by some sort of calculation.

Note:

private void startCustomConfetti(final ViewGroup container) {
        final List<Bitmap> allPossibleConfetti = Utils.generateConfettiBitmaps(new int[] { Color.WHITE }, 20 /* size */);
        // Alternatively, we provide some helper methods inside `Utils` to generate square, circle,
        // and triangle bitmaps.
        // Utils.generateConfettiBitmaps(new int[] { Color.BLACK }, 20 /* size */);

        final int numConfetti = allPossibleConfetti.size();
        final ConfettoGenerator confettoGenerator = new ConfettoGenerator() {
            @Override
            public Confetto generateConfetto(Random random) {
                final Bitmap bitmap = allPossibleConfetti.get(random.nextInt(numConfetti));
                return new BitmapConfetto(bitmap);
            }
        };

        final int initialX = 0;
        final int initialY = 0;
        Resources res = container.getResources();
        final float middleX = container.getWidth() /2;
        final float middleY = container.getHeight() / 2;
        final ConfettiSource confettiSource = new ConfettiSource(initialX, initialY);
        final float[] lastPositionX = {initialX};
        final float[] lastVelocityX = {middleX};
        final float[] lastPositionY = {initialY};
        final float[] lastVelocityY = {middleY};

        int explosionRadius = res.getDimensionPixelOffset(com.github.jinatonic.confetti.R.dimen.default_explosion_radius);
        int defaultVelocity = res.getDimensionPixelOffset(com.github.jinatonic.confetti.R.dimen.default_velocity_normal);
        final int[] emissionPosition = {0};

        final ConfettiManager confettiManager = new ConfettiManager(getContext(), confettoGenerator, confettiSource, container)
                .setEmissionDuration(1500)
                .setEmissionRate(50)
                .setVelocityX(middleX)
                .setVelocityY(middleY)
                .enableFadeOut(Utils.getDefaultAlphaInterpolator())
                .setInitialRotation(180, 180)
                .setRotationalAcceleration(360, 180)
                .setTargetRotationalVelocity(360)
                .setTTL(1000)
                .animate();

        confettiManager.setConfettiAnimationListener(new ConfettiManager.ConfettiAnimationListener() {
            @Override
            public void onAnimationStart(ConfettiManager confettiManager) {

            }

            @Override
            public void onAnimationEnd(ConfettiManager confettiManager) {

            }

            @Override
            public void onConfettoEnter(Confetto confetto) {
                Log.d("onConfettoEnter", "onConfettoEnter");

                randomizeConfettiImplosion(confetto, confettiManager, middleX, middleY,
                        container.getWidth(), container.getHeight());

            }

            @Override
            public void onConfettoExit(Confetto confetto) {
            }

            private void randomizeConfettiImplosion(Confetto confetto, ConfettiManager confettiManager,
                                                    float middleX, float middleY, int width, int height) {
                int chosenRegion = new Random().nextInt(4);
                Log.d("confetti", "chosenRegion: "+chosenRegion);
                int randomX, randomY;
                Random random = new Random();
                Log.d("randomConfetti", "width: "+width+" | height: "+height);

                switch (chosenRegion) {
                    case 0:
                        randomX = random.nextInt(width);
                        if (lastPositionX[0] != randomX) {
                            //set confetto initial position to prevent back to 0,0
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(0);

                            lastPositionX[0] = randomX;
                            lastVelocityX[0] = middleX - randomX;
                            lastPositionY[0] = 0;
                            lastVelocityY[0] = middleY;

                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    container.getWidth() * -1, 0,
                                    container.getWidth(), container.getHeight()
                            ));
                        } else {
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    container.getWidth() * -1, 0,
                                    container.getWidth(), container.getHeight()
                            ));
                        }
                        break;
                    case 1:
                        randomY = random.nextInt(height);
                        if (lastPositionY[0] != randomY) {
                            //set confetto initial position to prevent back to 0,0
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);

                            lastPositionX[0] = width;
                            lastVelocityX[0] = -middleX;
                            lastPositionY[0] = randomY;
                            lastVelocityY[0] = middleY-randomY;
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    container.getWidth() * -1, -randomY,
                                    container.getWidth(), container.getHeight()
                            ));

                        } else {
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    container.getWidth() * -1, -randomY,
                                    container.getWidth(), container.getHeight()
                            ));
                        }
                        break;
                    case 2:
                        randomX = random.nextInt(width);
                        if (lastPositionX[0] != randomX) {
                            //set confetto initial position to prevent back to 0,0
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);

                            lastPositionX[0] = randomX;
                            lastVelocityX[0] = middleX - randomX;
                            lastPositionY[0] = height;
                            lastVelocityY[0] = -middleY;
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    width* -1, -height,
                                    width, height
                            ));

                        } else {
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    width* -1, -height,
                                    width, height
                            ));
                        }

                        break;
                    case 3:
                        randomY = random.nextInt(height);
                        if (lastPositionY[0] != randomY) {
                            //set confetto initial position to prevent back to 0,0
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);

                            lastPositionX[0] = 0;
                            lastVelocityX[0] = middleX;
                            lastPositionY[0] = randomY;
                            lastVelocityY[0] = middleY-randomY;
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    width* -1, -randomY,
                                    width, height
                            ));

                        } else {
                            confetto.setInitialX(lastPositionX[0]);
                            confetto.setInitialY(lastPositionY[0]);
                            confettiManager.setVelocityX(lastVelocityX[0]);
                            confettiManager.setVelocityY(lastVelocityY[0]);
                            confettiManager.setBound(new Rect(
                                    width* -1, -randomY,
                                    width, height
                            ));
                        }

                        break;
                }

            }

        });
    }