Particles without onclick call Example
leglopy opened this issue · 4 comments
Hey,
I'm using this wonderful library but i'm blocked to animate particles without a touch on a button.
I have a Xml view with
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity"
>
<FrameLayout
android:id="@+id/background_hook"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
...
</RelativeLayout>
And an actvity with
@OnClick(R.id.button)
public void buttonTouched(View v){
doAnimation();
}
public void doAnimation(){
ParticleSystem ps = new ParticleSystem(this, 50, R.drawable.placeholder, 1000, R.id.background_hook);
ps.setSpeedRange(0.1f, 0.25f);
ps.setScaleRange(0.7f, 1.3f);
ps.setSpeedRange(0.1f, 0.25f);
ps.setAcceleration(0.0001f, 90);
ps.setRotationSpeedRange(90, 180);
ps.setFadeOut(200, new AccelerateInterpolator());
ps.emit(v, 100);
}
That code is okay. I have particles around the button.
But if i start the animation programatically without onclick event I have the animation in the top left corner --'
protected void onStart() {
super.onStart();
doAnimation();
}
It's the same with
protected void onStart() {
super.onStart();
button.performClick();
}
Thank you in advance for any help you can provide
This is duplication of #21, but at least your title and code explanation is much more detailed and clear. Actually it is a great report.
I'm doing a copy and paste from the other ticket:
It is due to how Android works.
For the particle system to work fine, the views must be measured when it is created. Inside onCreate the Views are not yet measured, just created. You need to set a ViewTreeObserver to know when the layout has been measured.
This is a standard question on Android, check out this link on Stack Overflow, it is exactly the same problem: http://stackoverflow.com/questions/3779173/determining-the-size-of-an-android-view-at-runtime
That being said, I'd leave this one open because I want to improve the code so this special case can be done automatically. I'm afraid that, in the meantime, you need to go with the ViewTreeObserver
Thanks for your help, that works with the ViewTreeObserver ;)
Hey, I was also having a problem with all particles showing up on top left corner regardless of anchor view positions on launch. The problem was the anchor view was not ready while starting the animation. So you can put the animation code inside View.post(Runnable) or View.postDelayed(Runnable) method. This fixed the issue of particales whithout onclick for me.
findViewById(R.id.center_anchor).post(new Runnable() {
@OverRide
public void run() {
new ParticleSystem(ModeSelectionActivity.this, 100, R.drawable.ic_gem_green, 30000)
.setSpeedRange(0.2f, 0.5f)
.oneShot(findViewById(R.id.center_anchor), 100);
}
});
If this helps out, Good Luck :)
Posting a runnable, even with a delay does not guarantee a fix. You need to start the emitter after the views have been measured. Posting a runnable is a gamble that works 99.9% of the time, but if the phone is slow for whatever reason, it will not work.
The only way to make sure it works is the tree observer.