Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confetti direction #30

Open
odetoyama opened this issue Aug 23, 2017 · 1 comment
Open

Confetti direction #30

odetoyama opened this issue Aug 23, 2017 · 1 comment

Comments

@odetoyama
Copy link

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
Copy link
Author

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:

  • works with 1.1.1-SNAPSHOT version
  • I don't really know how this .setBound() works. I just tried to do some trial and error to set boundaries to fill the container. Unless, some clipped confetti will occurs
  • It's not 100% perfect animation, I noticed that they are some miss direction occurs when randomizing the initialX and initialY.
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;
                }


            }

        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant