The method of realizing particle explosion effect on Android _android

Source: Internet
Author: User
Tags gety int size

In this paper, the method of realizing particle explosion effect by Android is described. Share to everyone for your reference. Specifically as follows:

1. Explosion.java Documents:

Package Net.obviam.particles.model;
Import Android.graphics.Canvas;
Import Android.graphics.Rect;
Import Android.util.Log;
  Public class Explosion {private static final String TAG = Explosion.class.getSimpleName ();
  public static final int state_alive = 0;
  At least 1 particle are alive public static final int state_dead = 1;
  All particles are dead private particle[] particles;
  Particles in the explosion private int x, y;
  The explosion ' s Origin private float gravity;
  The gravity of the explosion (+ upward,-down) is private float wind; Speed on horizontal private int size; Number of particles private int state; Whether it ' s still active or not public explosion (int particlenr, int x, int y) {log.d (TAG, explosion created a
    T "+ x +", "+ Y");
    This.state = state_alive;
    This.particles = new PARTICLE[PARTICLENR];
      for (int i = 0; i < this.particles.length i++) {particle p = new particle (x, y); THis.particles[i] = p;
  } this.size = Particlenr;
  Particle[] Getparticles () {return particles;
  } public void Setparticles (particle[] particles) {this.particles = particles;
  public int GetX () {return x;
  public void SetX (int x) {this.x = x;
  public int GetY () {return y;
  The public void sety (int y) {this.y = y;
  public float getgravity () {return gravity;
  public void setgravity (float gravity) {this.gravity = gravity;
  public float Getwind () {return wind;
  public void Setwind (float wind) {this.wind = wind;
  public int GetSize () {return size;
  public void setSize (int size) {this.size = size;
  public int getState () {return state;
  public void SetState (int state) {this.state = state;
  }//Helper methods-------------------------public boolean isAlive () {return this.state = = state_alive; public Boolean isdead () {return thIs.state = = State_dead;
      public void Update () {if (this.state!= state_dead) {Boolean isdead = true; for (int i = 0; i < this.particles.length i++) {if (this.particles[i].isalive ()) {This.particles[i
          ].update ();
        Isdead = false; 
    } if (isdead) this.state = State_dead;
      } public void Update (Rect container) {if (this.state!= state_dead) {Boolean isdead = true; for (int i = 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; } public void Draw (Canvas Canvas) {for (int i = 0; i < this.particles.length; i++) {if this.particles
      [I].isalive ()] {this.particles[i].draw (canvas);

 }
    }
  }
}

2. Particle.java files are as follows:

Package Net.obviam.particles.model;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;  public class Particle {public static final int state_alive = 0;   Particle is alive public static final int state_dead = 1; Particle is dead public static final int default_lifetime = 200;  Play with this public static final int max_dimension = 5;  The maximum width or height public static final int max_speed = 10;     Maximum speed (per update) private int state;    Particle is alive or dead private float widht;    Width of the particle private float height;     The height of the particle private float x, y;   Horizontal and vertical position private double XV, YV;      Vertical and horizontal velocity private int age;    Current age of the particle private int lifetime;     Particle dies when it reaches this value private int color; The color of the PartiCLE Private Paint Paint;
  Internal use to avoid instantiation public int getState () {return state;
  public void SetState (int state) {this.state = state;
  public float getwidht () {return widht;
  public void setwidht (float widht) {this.widht = WIDHT;
  public float getheight () {return height;
  public void SetHeight (float height) {this.height = height;
  public float GetX () {return x;
  public void SetX (float x) {this.x = x;
  public float GetY () {return y;
  public void sety (float y) {this.y = y;
  Public double Getxv () {return XV;
  public void Setxv (double xv) {THIS.XV = XV;
  Public double Getyv () {return yv;
  The public void Setyv (double yv) {this.yv = YV;
  public int getage () {return age;
  public void Setage (int age) {this.age = age;
  public int Getlifetime () {return lifetime;
 } public void Setlifetime (int lifetime) {   This.lifetime = lifetime;
  public int GetColor () {return color;
  public void SetColor (int color) {this.color = color;
  }//Helper methods-------------------------public boolean isAlive () {return this.state = = state_alive;
  public Boolean isdead () {return this.state = = State_dead;
    public particle (int x, int y) {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);
      Smoothing out the diagonal speed 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 = new Paint (this.color); }/** * Resets the PArticle * @param x * @param y */public void reset (float x, float y) {this.state = particle.state_alive;
    this.x = x;
    This.y = y;
  this.age = 0;
  //Return a integer that ranges from min inclusive to max inclusive.
  static int rndint (int min, int max) {return (int) (min + math.random () * (Max-min + 1));
  The static double Rnddbl (double min, double max) {return min + (max-min) * Math.random ();
      public void Update () {if (this.state!= state_dead) {this.x = = THIS.XV;
      This.y + = This.yv;
      Extract Alpha int a = This.color >>> 24;               A-= 2; Fade by 5 if (a <= 0) {//If reached transparency kill the particle this.state = State_dea
      D    else {This.color = (This.color & 0x00ffffff) + (a << 24);
        Set the new Alpha This.paint.setAlpha (a);           this.age++; Increase the age of the particle//THIS.WIDHT *= 1.05;
      This.height *= 1.05;
      } if (This.age >= this.lifetime) {//reached the end if it life this.state = State_dead; }//http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math///32bit//var color:uint = 0xff33
6699;
var a:uint = color >>> 24;
var r:uint = color >>> & 0xFF;
var g:uint = Color >>> 8 & 0xFF;
       
    var b:uint = color & 0xFF;  } public void Update (Rect container) {//Update with Collision if (This.isalive ()) {if (this.x <= Container.left | |
      This.x >= container.right-this.widht) {THIS.XV *=-1;
      }//Bottom is 0!!!
      if (this.y <= container.top | | this.y >= container.bottom-this.height) {this.yv *=-1;
  } update ();
    } public void Draw (Canvas Canvas) {//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);

 }
}

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.