Android实现粒子爆炸效果的方法
本文实例讲述了Android实现粒子爆炸效果的方法。分享给大家供大家参考。具体如下:
1.Explosion.java文件:
packagenet.obviam.particles.model; importandroid.graphics.Canvas; importandroid.graphics.Rect; importandroid.util.Log; publicclassExplosion{ privatestaticfinalStringTAG=Explosion.class.getSimpleName(); publicstaticfinalintSTATE_ALIVE=0; //atleast1particleisalive publicstaticfinalintSTATE_DEAD=1; //allparticlesaredead privateParticle[]particles; //particlesintheexplosion privateintx,y; //theexplosion'sorigin privatefloatgravity; //thegravityoftheexplosion(+upward,-down) privatefloatwind; //speedofwindonhorizontal privateintsize;//numberofparticles privateintstate;//whetherit'sstillactiveornot publicExplosion(intparticleNr,intx,inty){ Log.d(TAG,"Explosioncreatedat"+x+","+y); this.state=STATE_ALIVE; this.particles=newParticle[particleNr]; for(inti=0;i<this.particles.length;i++){ Particlep=newParticle(x,y); this.particles[i]=p; } this.size=particleNr; } publicParticle[]getParticles(){ returnparticles; } publicvoidsetParticles(Particle[]particles){ this.particles=particles; } publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } publicfloatgetGravity(){ returngravity; } publicvoidsetGravity(floatgravity){ this.gravity=gravity; } publicfloatgetWind(){ returnwind; } publicvoidsetWind(floatwind){ this.wind=wind; } publicintgetSize(){ returnsize; } publicvoidsetSize(intsize){ this.size=size; } publicintgetState(){ returnstate; } publicvoidsetState(intstate){ this.state=state; } //helpermethods------------------------- publicbooleanisAlive(){ returnthis.state==STATE_ALIVE; } publicbooleanisDead(){ returnthis.state==STATE_DEAD; } publicvoidupdate(){ if(this.state!=STATE_DEAD){ booleanisDead=true; for(inti=0;i<this.particles.length;i++){ if(this.particles[i].isAlive()){ this.particles[i].update(); isDead=false; } } if(isDead) this.state=STATE_DEAD; } } publicvoidupdate(Rectcontainer){ if(this.state!=STATE_DEAD){ booleanisDead=true; for(inti=0;i<this.particles.length;i++){ if(this.particles[i].isAlive()){ this.particles[i].update(container); //this.particles[i].update(); isDead=false; } } if(isDead) this.state=STATE_DEAD; } } publicvoiddraw(Canvascanvas){ for(inti=0;i<this.particles.length;i++){ if(this.particles[i].isAlive()){ this.particles[i].draw(canvas); } } } }
2.Particle.java文件如下:
packagenet.obviam.particles.model; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Rect; publicclassParticle{ publicstaticfinalintSTATE_ALIVE=0;//particleisalive publicstaticfinalintSTATE_DEAD=1;//particleisdead publicstaticfinalintDEFAULT_LIFETIME=200;//playwiththis publicstaticfinalintMAX_DIMENSION=5;//themaximumwidthorheight publicstaticfinalintMAX_SPEED=10;//maximumspeed(perupdate) privateintstate;//particleisaliveordead privatefloatwidht;//widthoftheparticle privatefloatheight;//heightoftheparticle privatefloatx,y;//horizontalandverticalposition privatedoublexv,yv;//verticalandhorizontalvelocity privateintage;//currentageoftheparticle privateintlifetime;//particledieswhenitreachesthisvalue privateintcolor;//thecoloroftheparticle privatePaintpaint;//internalusetoavoidinstantiation publicintgetState(){ returnstate; } publicvoidsetState(intstate){ this.state=state; } publicfloatgetWidht(){ returnwidht; } publicvoidsetWidht(floatwidht){ this.widht=widht; } publicfloatgetHeight(){ returnheight; } publicvoidsetHeight(floatheight){ this.height=height; } publicfloatgetX(){ returnx; } publicvoidsetX(floatx){ this.x=x; } publicfloatgetY(){ returny; } publicvoidsetY(floaty){ this.y=y; } publicdoublegetXv(){ returnxv; } publicvoidsetXv(doublexv){ this.xv=xv; } publicdoublegetYv(){ returnyv; } publicvoidsetYv(doubleyv){ this.yv=yv; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicintgetLifetime(){ returnlifetime; } publicvoidsetLifetime(intlifetime){ this.lifetime=lifetime; } publicintgetColor(){ returncolor; } publicvoidsetColor(intcolor){ this.color=color; } //helpermethods------------------------- publicbooleanisAlive(){ returnthis.state==STATE_ALIVE; } publicbooleanisDead(){ returnthis.state==STATE_DEAD; } publicParticle(intx,inty){ this.x=x; this.y=y; this.state=Particle.STATE_ALIVE; this.widht=rndInt(1,MAX_DIMENSION); this.height=this.widht; //this.height=rnd(1,MAX_DIMENSION); this.lifetime=DEFAULT_LIFETIME; this.age=0; this.xv=(rndDbl(0,MAX_SPEED*2)-MAX_SPEED); this.yv=(rndDbl(0,MAX_SPEED*2)-MAX_SPEED); //smoothingoutthediagonalspeed if(xv*xv+yv*yv>MAX_SPEED*MAX_SPEED){ xv*=0.7; yv*=0.7; } this.color=Color.argb(255,rndInt(0,255),rndInt(0,255),rndInt(0,255)); this.paint=newPaint(this.color); } /** *Resetstheparticle *@paramx *@paramy */ publicvoidreset(floatx,floaty){ this.state=Particle.STATE_ALIVE; this.x=x; this.y=y; this.age=0; } //Returnanintegerthatrangesfrommininclusivetomaxinclusive. staticintrndInt(intmin,intmax){ return(int)(min+Math.random()*(max-min+1)); } staticdoublerndDbl(doublemin,doublemax){ returnmin+(max-min)*Math.random(); } publicvoidupdate(){ if(this.state!=STATE_DEAD){ this.x+=this.xv; this.y+=this.yv; //extractalpha inta=this.color>>>24; a-=2;//fadeby5 if(a<=0){//ifreachedtransparencykilltheparticle this.state=STATE_DEAD; }else{ this.color=(this.color&0x00ffffff)+(a<<24);//setthenewalpha this.paint.setAlpha(a); this.age++;//increasetheageoftheparticle //this.widht*=1.05; //this.height*=1.05; } if(this.age>=this.lifetime){//reachedtheendifitslife this.state=STATE_DEAD; } //http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/ //32bit //varcolor:uint=0xff336699; //vara:uint=color>>>24; //varr:uint=color>>>16&0xFF; //varg:uint=color>>>8&0xFF; //varb:uint=color&0xFF; } } publicvoidupdate(Rectcontainer){ //updatewithcollision if(this.isAlive()){ if(this.x<=container.left||this.x>=container.right-this.widht){ this.xv*=-1; } //Bottomis480andtopis0!!! if(this.y<=container.top||this.y>=container.bottom-this.height){ this.yv*=-1; } } update(); } publicvoiddraw(Canvascanvas){ //paint.setARGB(255,128,255,50); paint.setColor(this.color); canvas.drawRect(this.x,this.y,this.x+this.widht,this.y+this.height,paint); //canvas.drawCircle(x,y,widht,paint); } }
希望本文所述对大家的Android程序设计有所帮助。