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程序设计有所帮助。

(0)

相关推荐

  • android Gallery组件实现的iPhone图片滑动效果实例

    实现的效果图,可左右滑动: 一.先在将Gallery标签放入: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layo

  • Android中实现毛玻璃效果的3种方法

    最近在做一款叫叽叽的App(男银懂的),其中有一个功能需要对图片处理实现毛玻璃的特效 进过一番预研,找到了3中实现方案,其中各有优缺点: 1.如果系统的api在16以上,可以使用系统提供的方法直接处理图片 复制代码 代码如下: if (VERSION.SDK_INT > 16) {             Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); final RenderScript rs = RenderScr

  • Android实现动画效果详解

    目前Android平台提供了两类动画一类是Tween动画,第二类就是 Frame动画,具体内容介绍请看下文: 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似. 实现动画有两种方式:一种使用XML文件(文件放在res/anim),一种直接代码搞定  1.透明度控制动画效果alpha <!-- 透明度控制动画效果alpha 浮点型值: fromAlpha 动画起始时透明

  • android开发教程之实现listview下拉刷新和上拉刷新效果

    复制代码 代码如下: public class PullToLoadListView extends ListView implements OnScrollListener { private static final String TAG = PullToLoadListView.class.getSimpleName(); private static final int STATE_NON = 0; private static final int STATE_PULL_TO_REFRE

  • android实现自动滚动的Gallary控件效果

    本文实例讲述了android实现自动滚动的Gallary控件.分享给大家供大家参考.具体如下: import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; im

  • Android实现图片阴影效果的方法

    本文实例介绍了Android实现图片阴影效果,设置画布颜色,图像倾斜效果,图片阴影效果的方法,采用canvas.save(Canvas.MATRIX_SAVE_FLAG);来实现.由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果,在原有矩形基础上,画成圆角矩形,同时带有阴影层.读者可以根据自身需要对该程序代码进行个性化的修改以便更符合自身项目需求. 具体实现代码如下: package canvas.test; import android.app.Activi

  • android实现图片反转效果

    可能有些同学不明白,为啥要图片反转(不是旋转哦),我们在游戏开发中,为了节省图片资源(空间) 有可能会使用到图片反转,例如,一个人物图片,面向左,或右,如果不能实现图片反转的情况下,就需要两张图片了,废话少说,看效果上代码: 在上图中,实际两个人物使用的是一张图片,只是针对一张图片做了处理而已. 详细代码: public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback{ public Bit

  • Android实现Flip翻转动画效果

    本文实例讲述了Android实现Flip翻转动画效果的方法,分享给大家供大家学习借鉴. 具体实现代码如下: LinearLayout locationLL = (LinearLayout) findViewById(R.id.locationLL); LinearLayout baseLL = (LinearLayout) findViewById(R.id.baseLL); private void flipit() { Interpolator accelerator = new Accel

  • Android 轻松实现图片倒影效果实例代码

    主Activity 复制代码 代码如下: package com.mj.myweather;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.widget.ImageView;import com.mj.myweather.utils.ImageUtil; publi

  • Android ImageButton自定义按钮的按下效果的代码实现方法分享

    使用Button时为了让用户有"按下"的效果,有两种实现方式:1.在代码里面. 复制代码 代码如下: imageButton.setOnTouchListener(new OnTouchListener(){ @Override                          public boolean onTouch(View v, MotionEvent event) {                                  if(event.getAction()

  • Android实现跑马灯效果的方法

    本文实例讲述了Android实现跑马灯效果的方法.分享给大家供大家参考.具体如下: 运行效果截图如下: 直接在布局里写代码就好了: <TextView android:id="@+id/menu_desc" android:layout_width="300dip" android:layout_height="wrap_content" android:text="温馨提示:左右滑动更改菜单,点击进入" android

随机推荐