DanielMartinus / Konfetti

Celebrate more with this lightweight confetti particle system 🎊
ISC License
3.18k stars 301 forks source link

Change position! #250

Closed brittle123 closed 3 years ago

brittle123 commented 3 years ago

Hello Sir!,.. i've used your simple approach but it starts konfetti from top left corner. i want it to display from top not only from top corner.Here are my both xml and java code. don't know why it's showing me like this. Please help me ASAP! Thanks and Regards.

    <?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:id="@+id/container"
        android:background="@color/tab_text_color"
        tools:context=".CongratulateActivity">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <nl.dionsegijn.konfetti.KonfettiView
        android:id="@+id/viewKonfetti"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/trophieImg"
        android:src="@drawable/trophie"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/nice_text"
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nice_text"
        android:text="Nice you've done it!"
        android:textSize="25sp"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/result_RL"
        android:textColor="@color/white"
        android:fontFamily="@font/lato_regular"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/result_RL"
        android:layout_centerInParent="true"
        android:layout_margin="50dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exe_RL"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/total_exe"
            android:text="16"
            android:textColor="@color/white"
            android:textSize="25sp"
            android:fontFamily="@font/lato_bold"
            android:layout_gravity="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exercises"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:fontFamily="@font/lato_bold"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dur_RL"
        android:orientation="vertical"
        android:layout_alignParentRight="true"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/total_dur"
            android:text="00:10"
            android:textColor="@color/white"
            android:textSize="25sp"
            android:fontFamily="@font/lato_bold"
            android:layout_gravity="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Duration"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:fontFamily="@font/lato_bold"/>
    </LinearLayout>
    </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:id="@+id/doitadain"
            android:textSize="20sp"
            android:text="DO IT AGAIN"
            android:fontFamily="@font/lato_regular"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/result_RL"
            android:layout_marginLeft="16dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/next"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:text="NEXT"
            android:fontFamily="@font/lato_regular"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/result_RL"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/share"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:text="SHARE"
            android:fontFamily="@font/lato_regular"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/result_RL"
            android:layout_marginRight="16dp"
            />

     </RelativeLayout>

          public class CongratulateActivity extends AppCompatActivity {
              TextView total_exe,total_dur;
              String GetTime;
              int get_exercise;

              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_congratulate);
                  Window window = this.getWindow();

          // clear FLAG_TRANSLUCENT_STATUS flag:
                  window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

          // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
                  window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

          // finally change the color
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                      window.setStatusBarColor(ContextCompat.getColor(this, R.color.transparent));
                  }

        Toolbar mToolbar=findViewById(R.id.toolbar);
        mToolbar.setBackgroundColor(getResources().getColor(R.color.tab_text_color));

        setSupportActionBar(mToolbar);
        // getSupportActionBar().setDisplayShowTitleEnabled(false);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_back);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

        final KonfettiView konfettiView = findViewById(R.id.viewKonfetti);
        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, 5f))
                .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                .streamFor(300, 3000L);

        total_exe=findViewById(R.id.total_exe);
        total_dur=findViewById(R.id.total_dur);
        GetTime = getIntent().getStringExtra("duration");
        get_exercise = getIntent().getIntExtra("size", 8);
        total_exe.setText(String.valueOf(get_exercise));
        total_dur.setText(String.valueOf(GetTime));

    }

       }

SceenShot

DanielMartinus commented 3 years ago

Hi @brittle123,

This issue is a duplicate of:

When starting Konfetti make sure the layout is properly initialized. This can be done by registering a GlobalLayoutListener on the ViewTreeObserver for example.

brittle123 commented 3 years ago

Thanks but using postdelay it works for me.

DanielMartinus commented 3 years ago

The following part of the code:

 final KonfettiView konfettiView = findViewById(R.id.viewKonfetti);
        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, 5f))
                .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                .streamFor(300, 3000L);

Needs to be executed when the layout is fully drawn. There are different ways to doing this. For example with an OnGlobalLayoutListener like here: https://stackoverflow.com/a/14298483

Haven't tested it but that would become something like:

final KonfettiView konfettiView = findViewById(R.id.viewKonfetti);
        konfettiView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                        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, 5f))
                          .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                          .streamFor(300, 3000L);
             }
        });