plattysoft / Leonids

A Particle System for standard Android UI: http://plattysoft.github.io/Leonids/
Apache License 2.0
2.28k stars 398 forks source link

Particle System does not show in a Fragment #92

Open JohnMerlino2 opened 6 years ago

JohnMerlino2 commented 6 years ago

I have a fragment:

AnimationFragment.java

public class AnimationFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.animation_fragment, container, false);

        new ParticleSystem(getActivity(), 80, R.drawable.poop_emoji, 10000)
                .setSpeedByComponentsRange(0f, 0f, 0.05f, 0.1f)
                .setAcceleration(0.00005f, 90)
                .emitWithGravity(view.findViewById(R.id.animationBox), Gravity.BOTTOM, 8);

        return view;
    }
}

I have a view in Fragment with animationBox id:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <View android:id="@+id/animationBox"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
</LinearLayout>

Notice I initialized the ParticleSystem in onCreateView. When I run the app, nothing shows. Can the Particle System with with Fragments?

MahmoudMabrok commented 4 years ago

i face same problem

MahmoudMabrok commented 4 years ago

Solution is to delay action of generating particles i used this Handler().postDelayed({ stratAnim() }, 1200) with kotlin

plattysoft commented 4 years ago

That is not the best solution, this is most likely the same problem as for Activities regarding that while onCreate the views are not yet measured. The suggested solution is to observe the view tree .

For more details see https://github.com/plattysoft/Leonids/issues/22

MahmoudMabrok commented 4 years ago

@plattysoft why not make it as internal implementation of lib to start anim when views are measured

plattysoft commented 4 years ago

if you create the ParticleSystem after the views are measured, you can don't get a ViewTreeObserver callback, so the system does not know if the views are been measured yet, or if they have been measured before creating. This needs to be tied to the ActivityLifecycle, maybe using the lifecycle components it could be done.