服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现粒子爆炸效果的方法

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

2021-03-26 15:15红薯 Android

这篇文章主要介绍了Android实现粒子爆炸效果的方法,实例分析了Android动画特效的实现技巧,需要的朋友可以参考下

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

1. Explosion.java文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9592022-03-02
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810042021-05-03
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7432021-03-11
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4652021-01-03
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4622021-03-09
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8332021-03-31
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6182021-03-27