This repository was archived by the owner on Aug 5, 2022. It is now read-only.
forked from plattysoft/Leonids
-
Notifications
You must be signed in to change notification settings - Fork 4
FAQ
Thomas Orlando edited this page Jul 4, 2020
·
11 revisions
It is due to how Android works.
For the particle system to function correctly, it's anchor view must already be measured when it is created by ParticleSystem.emit()
or ParticleSystem.oneShot()
.
Inside onCreate()
or onStart()
, the views are not yet measured, meaning that while instances of ParticleSystem
can be created by those methods, calling emit()
or oneShot()
from inside them will cause the particle system to always appear in the top left corner.
To fix this issue, a ViewTreeObserver
can be used to start the particle system when the layout has been measured:
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Remove the layout listener if you only want to shoot/emit once
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// START PARTICLE SYSTEMS HERE:
// ...
}
});
}
val viewTreeObserver = view.viewTreeObserver
if (viewTreeObserver.isAlive) {
viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Remove the layout listener if you only want to shoot/emit once
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
// START PARTICLE SYSTEMS HERE:
// ...
}
})
}
This is a standard issue on Android; for further information check out this question on Stack Overflow or Leonids issue #22.