Leonids icon indicating copy to clipboard operation
Leonids copied to clipboard

Particle System does not show in a Fragment

Open JohnMerlino2 opened this issue 7 years ago • 5 comments

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?

JohnMerlino2 avatar Feb 10 '18 02:02 JohnMerlino2

i face same problem

MahmoudMabrok avatar May 29 '20 17:05 MahmoudMabrok

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

MahmoudMabrok avatar May 29 '20 18:05 MahmoudMabrok

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

plattysoft avatar May 29 '20 19:05 plattysoft

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

MahmoudMabrok avatar May 29 '20 19:05 MahmoudMabrok

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.

plattysoft avatar May 29 '20 20:05 plattysoft