ActivityMain.java
- public class DrawThread extends Thread {
- ParticleView pv;
- SurfaceHolder suraceHolder;
- boolean isRunning;
- int sleepSpan = 15;
- long start = System.nanoTime();
- int count = 0;
- public DrawThread(ParticleView pv, SurfaceHolder suraceHolder) {
- this.isRunning = true;
- this.pv = pv;
- this.suraceHolder = suraceHolder;
- }
- @Override
- public void run() {
- Canvas canvas = null;
- while(isRunning){
- try{
- canvas = suraceHolder.lockCanvas();
- synchronized(suraceHolder){
- pv.doDraw(canvas);
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(canvas!=null){
- suraceHolder.unlockCanvasAndPost(canvas);
- }
- }
- this.count++;
- if(count == 20){
- count = 0;
- long tempStamp = System.nanoTime(); //获取当前时间
- long span = tempStamp - start; //获取时间间隔
- start = tempStamp; //为start重新赋值
- double fps = Math.round(100000000000.0/span*20)/100.0;//计算帧速率
- pv.fps = "FPS:"+fps;//将计算出的帧速率设置到BallView的相应字符串对象中
- }
- try{
- Thread.sleep(sleepSpan);//线程休眠一段时间
- }
- catch(Exception e){
- e.printStackTrace();//捕获并打印异常
- }
- }
- }
- }
DrawThread.java
- public class ActivityMain extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE); //不显示标题
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- ParticleView lz = new ParticleView(this);
- setContentView(lz);
- }
- }
Particle.java
- public class Particle {
- int color; //粒子颜色
- int r; //粒子半径
- double ver_v; //垂直速度
- double hor_v; //水平速度
- int startX; //初始X坐标
- int startY; //初始Y坐标
- int x; //实时X坐标
- int y; //实时Y坐标
- double startTime; //起始时间
- public Particle(int color, int r, double ver_v, double hor_v, int x, int y, double startTime) {
- super();
- this.color = color;
- this.r = r;
- this.ver_v = ver_v;
- this.hor_v = hor_v;
- this.startX = x;
- this.startY = y;
- this.x = x;
- this.y = y;
- this.startTime = startTime;
- }
- }
ParticleSet.java
- public class ParticleSet {
- ArrayList<Particle> particleSet;
- public ParticleSet(){
- particleSet = new ArrayList<Particle>();
- }
- public void addParticle(int count,double startTime){
- for(int i=0;i<count;i++){
- int color = this.getColor(i);
- int r = 1;
- double ver_v = -30 + 10*(Math.random());
- double hor_v = 10 - 20*(Math.random());
- int x = 160;
- int y = (int)(100 - 10*(Math.random()));
- Particle particle = new Particle(color, r, ver_v, hor_v, x, y, 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.BLUE;
- break;
- case 2:
- color = Color.YELLOW;
- break;
- case 3:
- color = Color.GRAY;
- break;
- }
- return color;
- }
- }
ParticleThread.java
- public class ParticleThread extends Thread{
- boolean isRunning;
- ParticleView father;
- int sleepSpan = 80;
- double time = 0;
- double span = 0.15;
- public ParticleThread(ParticleView father) {
- this.isRunning = true;
- this.father = father;
- }
- @Override
- public void run() {
- while(isRunning){
- father.ps.addParticle(5, time);
- 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.hor_v*timeSpan);
- //计算Y坐标
- int tempy = (int)(particle.startY+particle.ver_v*timeSpan+4.9*timeSpan*timeSpan);
- //超过屏幕下边
- if(tempy>300){
- tempSet.remove(particle);
- count = tempSet.size();
- }
- particle.x = tempx;
- particle.y = tempy;
- }
- time += span;
- try{
- Thread.sleep(sleepSpan);
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- super.run();
- }
- }
ParticleView.java
- public class ParticleView extends SurfaceView implements SurfaceHolder.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);
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- if(!dt.isAlive()){
- dt.start();
- }
- if(!pt.isAlive()){
- pt.start();
- }
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- dt.isRunning = false;
- dt = null;
- }
- public void doDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- 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 tempR = p.r;
- RectF oval = new RectF(tempX,tempY,tempX+2*tempR, tempY+2*tempR);
- canvas.drawOval(oval, paint);
- }
- paint.setColor(Color.WHITE);
- paint.setTextSize(18); //字体大小
- paint.setAntiAlias(true); //设置抗锯齿
- canvas.drawText(fps, 15, 15, paint);
- }
- }
运行结果