package com.particle;
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) {
super();
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;
}
}
package com.particle;
import java.util.ArrayList;
import android.graphics.Color;
public class ParticleSet {
ArrayList<Particle> particleSet;
public ParticleSet() {
particleSet = new ArrayList<Particle>();
}
/**
* 向粒子集合中添加指定数量的粒子对象
*/
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 = 160;
int tempY = (int) (100 - 10 * (Math.random())); // 随机产生粒子Y坐标,90到100之间
Particle particle = new Particle(tempColor, tempR, tempv_v,
tempv_h, tempX, tempY, startTime);
particleSet.add(particle);
}
}
/**
* 获取指定索引的颜色
*/
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;
}
}
package com.particle;
import java.util.ArrayList;
public class ParticleThread extends Thread {
boolean flag;
ParticleView father;
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; // 获取粒子集合
for (int i = tempSet.size() - 1; i >= 0; i--) {
Particle particle = tempSet.get(i);
double timeSpan = time - particle.startTime; // 计算从程序开始到现在经过的时间
int tempX = (int) (particle.startX + particle.horizontal_v * timeSpan);
int tempY = (int) (particle.startY + 4.9 * timeSpan * timeSpan + particle.vertical_v * timeSpan);
if (tempY > ParticleView.DIE_OUT_LINE) { // 如果粒子超过屏幕下边沿
tempSet.remove(particle);
}
particle.x = tempX;
particle.y = tempY;
}
time += span;
try {
Thread.sleep(sleepSpan);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.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.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class ParticleView extends SurfaceView implements Callback {
public static final int DIE_OUT_LINE = 300;
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);
}
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);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
if (!dt.isAlive()) {
dt.start();
}
if (!pt.isAlive()) {
pt.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
dt.flag = false;
dt = null;
pt.flag = false;
pt = null;
}
}
package com.particle;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class DrawThread extends Thread {
ParticleView pv;
SurfaceHolder surfaceHolder;
boolean flag=false;
int sleepSpan=30;
long start =System.nanoTime(); //记录起始时间,该变量用于计算帧速率
int count=0 ; //记录帧数
public DrawThread(ParticleView pv,SurfaceHolder surfaceHolder) {
this.pv=pv;
this.surfaceHolder=surfaceHolder;
this.flag=true;
}
public void run() {
Canvas canvas=null;
while(flag) {
try {
canvas=surfaceHolder.lockCanvas(null); //获取BallView的画布
synchronized (surfaceHolder) {
pv.doDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(canvas!=null) {
surfaceHolder.unlockCanvasAndPost(canvas); // surfaceHolder解锁,并将画布传回
}
}
this.count++;
if(count==20) { //计满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();
}
}
}
}
package com.particle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
ParticleView pv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //设置不显示标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏
pv=new ParticleView(this);
setContentView(pv);
}
}
/**
* 向粒子集合中添加指定数量的粒子对象(瀑布粒子效果)
*/
public void add2(int count, double startTime) {
for (int i = 0; i < count; i++) {
int tempColor = this.getColor(i);
int tempR = 1; // 粒子半径
double tempv_v = 0; // 粒子竖直方向的速度为0
double tempv_h = 10 + 20 * (Math.random()); // 随机产生粒子水平方向的速度
int tempX = 50;
int tempY = (int) (50 - 10 * (Math.random())); // 随机产生粒子Y坐标,90到100之间
Particle particle = new Particle(tempColor, tempR, tempv_v,
tempv_h, tempX, tempY, startTime);
particleSet.add(particle);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有