此效果类似放烟花的效果,粒子从一个以不同速度点向上喷出,达到最高点然后向下散落,和上一个小球的示例类似,两个线程进行控制,一个控制改变添加粒子和改变他们的轨迹,另一个线程不停地绘制屏幕元素。两者结合完成此效果。代码如下:
MainActivity:
package com.home.particle; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置不显示标题 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置全屏模式 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); ParticleView pv = new ParticleView(this); setContentView(pv); } }
ParticleView:
package com.home.particle; import java.util.ArrayList; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * 试图类 * * @author Administrator * */ public class ParticleView extends SurfaceView implements SurfaceHolder.Callback { public static final int DIE_OUT_LINE = 500;// 屏幕下边缘界限 DrawThread dt; ParticleSet ps; ParticleThread pt; String fps = "FPS:N/A";// 帧速率 public ParticleView(Context context) { super(context); this.getHolder().addCallback(this); dt = new DrawThread(this, getHolder()); ps = new ParticleSet(); pt = new ParticleThread(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { // 启动线程 if (!dt.isAlive()) { dt.start(); } if (!pt.isAlive()) { pt.start(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { dt.flag = false; dt = null; pt.flag = false; pt = null; } public void doDraw(Canvas canvas) { canvas.drawColor(Color.BLACK);// 清除屏幕 ArrayList<Particle> particleSet = ps.particleSet; Paint paint = new Paint(); for (int i = 0; i < particleSet.size(); i++) { Particle p = particleSet.get(i); paint.setColor(p.color); int tempX = p.x; int tempY = p.y; int tempRadius = p.r; RectF oval = new RectF(tempX, tempY, tempX + 2 * tempRadius, tempY + 2 * tempRadius); canvas.drawOval(oval, paint);// 绘制椭圆粒子 } paint.setColor(Color.WHITE); paint.setTextSize(18); paint.setAntiAlias(true); canvas.drawText(fps, 15, 15, paint);// 绘制帧速率 } }
DrawThread:
package com.home.particle; import android.graphics.Canvas; import android.view.SurfaceHolder; /** * 绘制屏幕的线程 * * @author Administrator * */ public class DrawThread extends Thread { ParticleView pv; SurfaceHolder holder; boolean flag; int sleepSpan = 15; long start = System.nanoTime(); int count = 0;// 记录帧数,该变量用于计算帧速率 public DrawThread(ParticleView pv, SurfaceHolder holder) { this.pv = pv; this.holder = holder; this.flag = true; } @Override public void run() { Canvas canvas = null; while (flag) { try { canvas = holder.lockCanvas(null); synchronized (holder) { pv.doDraw(canvas); } } catch (Exception e) { e.printStackTrace(); } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } this.count++; if (count == 20) { count = 0; long tempStamp = System.nanoTime(); long span = tempStamp - start; start = tempStamp; // 计算帧速率 double fps = Math.round(100000000000.0 / span * 20) / 100.0; pv.fps = "FPS:" + fps; } try { Thread.sleep(sleepSpan); } catch (InterruptedException e) { e.printStackTrace(); } } } }
ParticleThread:
package com.home.particle; import java.util.ArrayList; /** * 添加粒子和改变轨迹的线程 * * @author Administrator * */ public class ParticleThread extends Thread { boolean flag;// 线程执行的标志 ParticleView father;// ParticleView对象的引用 int sleepSpan = 80;// 线程休眠时间 double time = 0;// 物理引擎的时间轴 double span = 0.15;// 每次计算粒子位移的时间间隔 public ParticleThread(ParticleView father) { this.father = father; this.flag = true; } @Override public void run() { while (flag) { father.ps.add(5, time);// 每次添加5个粒子 // 获取粒子集合 ArrayList<Particle> tempSet = father.ps.particleSet; int count = tempSet.size(); // 遍历粒子集合修改其轨迹 for (int i = 0; i < count; i++) { Particle particle = tempSet.get(i); // 计算从程序开始到现在经过的时间 double timeSpan = time - particle.startTime; // 计算x坐标 int tempx = (int) (particle.startX + particle.horizontal_v * timeSpan); // 计算y坐标 int tempy = (int) (particle.startY + 4.9 * timeSpan * timeSpan + particle.vertical_v * timeSpan); if (tempy > ParticleView.DIE_OUT_LINE) {// 如果粒子超过屏幕下边缘 tempSet.remove(particle);// 移除 count = tempSet.size();// 重设置粒子个数 } // 修改粒子坐标 particle.x = tempx; particle.y = tempy; } time += span;// 将时间延长 try { Thread.sleep(sleepSpan); } catch (Exception e) { e.printStackTrace(); } } } }
Particle:
package com.home.particle; /** * 粒子实体类 * * @author Administrator * */ public class Particle { int color;// 颜色 int r;// 半径 double vertical_v;// 垂直速度 double horizontal_v;// 水平速度 int startX;// 初始x坐标 int startY;// 初始y坐标 int x;// 实时x坐标 int y;// 实时y坐标 double startTime;// 起始时间 public Particle(int color, int r, double vertical_v, double horizontal_v, int x, int y, double startTime) { this.color = color; this.r = r; this.vertical_v = vertical_v; this.horizontal_v = horizontal_v; this.startX = x; this.startY = y; this.x = x; this.y = y; this.startTime = startTime; } }
ParticleSet:
package com.home.particle; import java.util.ArrayList; import android.graphics.Color; /** * 负责管理和添加粒子对象 * * @author Administrator * */ public class ParticleSet { ArrayList<Particle> particleSet;// 存放粒子对象的集合 public ParticleSet() { particleSet = new ArrayList<Particle>(); } /** * 向粒子集合添加指定个数的粒子对象 * * @param count * @param startTime */ public void add(int count, double startTime) { for (int i = 0; i < count; i++) { int tempColor = this.getColor(i); int tempR = 1; // 随机产生粒子竖直方向的速度 double tempv_v = -30 + 10 * (Math.random()); // 随机产生粒子水平方向的速度 double tempv_h = 10 - 20 * (Math.random()); int tempX = 260; // 随机产生粒子Y坐标,90~100之间 int tempY = (int) (100 - 10 * (Math.random())); // 创建粒子对象 Particle particle = new Particle(tempColor, tempR, tempv_v, tempv_h, tempX, tempY, startTime); particleSet.add(particle);// 加入集合 } } /** * 根据索引得到不同的颜色 * * @param i * @return */ public int getColor(int i) { int color = Color.RED; switch (i % 4) { case 0: color = Color.RED; break; case 1: color = Color.GREEN; break; case 2: color = Color.YELLOW; break; case 3: color = Color.GRAY; break; } return color; } }