Leonids 粒子系统源码分析

来源:互联网 发布:好看的韩国迷你网络剧 编辑:程序博客网 时间:2024/05/17 09:14

https://github.com/plattysoft/Leonids

用第一例子oneshot为例子:

public void onClick(View arg0) {
new ParticleSystem(this, 2, R.drawable.star_pink, 3200)   
.setSpeedRange(0.1f, 0.25f) // 速度在这个两值之间,随机取。是角速度啊。
.oneShot(arg0, 2); // arg0为了获得,button的坐标。这个坐标为开始点。
}


所有点都画在ParticleField上了,这是一个view。

mActiveParticles 存放所有点的坐标信息,速度。。。


ValueAnimator是动画产生的原理,每隔一帧时间,会触发事件。这个事件定义成,改变所有的坐标值,重绘ParticleField。





private void onUpdate(long miliseconds) {
while (((mEmitingTime > 0 && miliseconds < mEmitingTime)|| mEmitingTime == -1) && // This point should emit
!mParticles.isEmpty() && // We have particles in the pool 
mActivatedParticles < mParticlesPerMilisecond*miliseconds) { // and we are under the number of particles that should be launched
// Activate a new particle
activateParticle(miliseconds);
Log.e(TAG,"activateParticle");
}
synchronized(mActiveParticles) {
for (int i = 0; i < mActiveParticles.size(); i++) {
Log.e(TAG,"mActiveParticles i:"+i);
boolean active = mActiveParticles.get(i).update(miliseconds);//更新坐标
if (!active) {
Particle p = mActiveParticles.remove(i);
i--; // Needed to keep the index at the right position
mParticles.add(p);


}
}
}
mDrawingView.postInvalidate();
}


private void startAnimator(Interpolator interpolator, long animnationTime) {   mAnimator = ValueAnimator.ofInt(0, (int) animnationTime);   mAnimator.setDuration(animnationTime);   mAnimator.addUpdateListener(new AnimatorUpdateListener() {           @Override           public void onAnimationUpdate(ValueAnimator animation) {               int miliseconds = (Integer) animation.getAnimatedValue();               onUpdate(miliseconds);           }       });   mAnimator.addListener(new AnimatorListener() {             @Override      public void onAnimationStart(Animator animation) {}      @Override      public void onAnimationRepeat(Animator animation) {}      @Override      public void onAnimationEnd(Animator animation) {         cleanupAnimation();      }      @Override      public void onAnimationCancel(Animator animation) {         cleanupAnimation();                   }       });   mAnimator.setInterpolator(interpolator);   mAnimator.start();}




第二个例子,加速度掉下来。要手工计算 x=v0t+1/2*at^2

mCurrentX = mInitialX+mSpeedX*realMiliseconds+mAccelerationX*realMiliseconds*realMiliseconds;



原创粉丝点击