2

I recently came across this library.

https://github.com/DanielMartinus/Konfetti

I tried to implement it in my application. I read the documentation and tried to implement it. Below is my code:

JAVA FILE:

final KonfettiView konfettiView = findViewById(R.id.viewConfetti);
konfettiView.build()
                .addColors(Color.YELLOW, Color.GREEN, Color.MAGENTA)
                .setDirection(0.0, 359.0)
                .setSpeed(1f, 5f)
                .setFadeOutEnabled(true)
                .setTimeToLive(2000L)
                .addShapes(Shape.Square.INSTANCE, Shape.Circle.INSTANCE)
                .addSizes(new Size(12, 5))
                .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                .streamFor(300, 1500L);

Below is my Layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/folk"
    tools:context=".TrueActivity">

<nl.dionsegijn.konfetti.KonfettiView
        android:id="@+id/viewConfetti"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/CongoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/congratulations"
        android:visibility="invisible"
        android:textSize="35dp"
        android:textColor= "#FFF"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

Everything is working fine for me. But the confetti is ejected from the top left only. I tried to change the KonfettiView object to different dimensions (i.e. trying to change the X-axis); but to no avail. I even read below answer on Stack overflow.

https://stackoverflow.com/a/46016959/13627957

But it doesn't seem to help. Is there any way I could get it ejected from full top as shown in the main library readme file instead of only top left.

Thanks.

1 Answers1

1

You can use the DisplayMetrics class' constant widthPixels instead of konfettiView.getWidth() method to achieve your results.

Speaking about the two; your konfettiView.getWidth() uses the Konfetti width whereas DisplayMetrics.widthPixels uses device width. Information about width pixels.

Try the code below:

DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(display);
final KonfettiView konfettiView = findViewById(R.id.viewConfetti);
konfettiView.build()
                .addColors(Color.YELLOW, Color.GREEN, Color.MAGENTA)
                .setDirection(0.0, 359.0)
                .setSpeed(1f, 5f)
                .setFadeOutEnabled(true)
                .setTimeToLive(2000L)
                .addShapes(Shape.Square.INSTANCE, Shape.Circle.INSTANCE)
                .addSizes(new Size(12, 5))
                .setPosition(-50f, display.widthPixels + 50f, -50f, -50f)
                .streamFor(300, 1500L);