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

 更新时间:2015年06月16日 17:07:55   作者:红薯  
这篇文章主要介绍了Android实现粒子爆炸效果的方法,实例分析了Android动画特效的实现技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Android Studio签名打包的两种方式(图文教程)

    Android Studio签名打包的两种方式(图文教程)

    这篇文章主要介绍了Android Studio签名打包的两种方式(图文教程),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Android 炫舞漫天飞雪效果图

    Android 炫舞漫天飞雪效果图

    这篇文章主要介绍了Android 炫舞漫天飞雪效果图的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android中协调滚动布局的实现代码

    Android中协调滚动布局的实现代码

    这篇文章主要介绍了Android中协调滚动常用的布局实现,类似这样的协调滚动布局,当底部列表滑动的时候,顶部的布局做响应的动作,我们都可以通过 AppBarLayout 和 MotionLayout 来实现,本文通过实例代码介绍的非常详细,需要的朋友参考下吧
    2022-06-06
  • Android Studio连接MySql实现登录注册(附源代码)

    Android Studio连接MySql实现登录注册(附源代码)

    登录注册是常用的一个功能,正好今天用android studio 做一个类似于这样的登录软件,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Android 优化Handler防止内存泄露

    Android 优化Handler防止内存泄露

    这篇文章主要介绍了Android 优化Handler防止内存泄露的相关资料,这里提供实例帮助大家理解掌握这样的内容,需要的朋友可以参考下
    2017-09-09
  • Android APP与媒体存储服务的交互

    Android APP与媒体存储服务的交互

    本文介绍如何在 Android 中,开发者的 APP 如何使用媒体存储服务(包含MediaScanner、MediaProvider以及媒体信息解析等部分),包括如何把 APP 新增或修改的文件更新到媒体数据库、如何在多媒体应用中隐藏 APP 产生的文件、如何监听媒体数据库的变化等等。
    2013-10-10
  • Android编程自定义对话框(Dialog)位置及大小的方法

    Android编程自定义对话框(Dialog)位置及大小的方法

    这篇文章主要介绍了Android编程自定义对话框(Dialog)位置及大小的方法,涉及Android对话框的定义、功能、属性及布局相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • Android使用URLConnection提交请求的实现

    Android使用URLConnection提交请求的实现

    这篇文章主要为大家详细介绍了Android使用URLConnection提交请求的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android仿网易云音乐播放界面

    Android仿网易云音乐播放界面

    这篇文章主要为大家详细介绍了Android仿网易云音乐播放界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android程序开发之动态设置ImageView的亮度

    Android程序开发之动态设置ImageView的亮度

    这篇文章主要介绍了Android程序开发之动态设置ImageView的亮度 的相关资料,需要的朋友可以参考下
    2016-01-01

最新评论