欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现粒子爆炸效果的方法

程序员文章站 2023-10-28 20:27:52
本文实例讲述了android实现粒子爆炸效果的方法。分享给大家供大家参考。具体如下: 1. explosion.java文件: package net.obvi...

本文实例讲述了android实现粒子爆炸效果的方法。分享给大家供大家参考。具体如下:

1. explosion.java文件:

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 is 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)
  private float wind;
  // speed of wind 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 at " + 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;
  }
  public 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;
  }
  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文件如下:

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;    // 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;
  }
  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 an integer that ranges from min inclusive to max inclusive.
  static int rndint(int min, int max) {
    return (int) (min + math.random() * (max - min + 1));
  }
  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_dead;
      } 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 its life
        this.state = state_dead;
      }
      // http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
      //32bit
//     var color:uint = 0xff336699;
//     var a:uint = color >>> 24;
//     var r:uint = color >>> 16 & 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 480 and top 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);
  }
}

希望本文所述对大家的android程序设计有所帮助。