zoukankan      html  css  js  c++  java
  • Android粒子效果

    ActivityMain.java

     

    1. public class DrawThread extends Thread {  
    2.     ParticleView pv;  
    3.     SurfaceHolder suraceHolder;  
    4.     boolean isRunning;  
    5.     int sleepSpan = 15;  
    6.     long start = System.nanoTime();  
    7.     int count = 0;  
    8.     public DrawThread(ParticleView pv, SurfaceHolder suraceHolder) {  
    9.         this.isRunning = true;  
    10.         this.pv = pv;  
    11.         this.suraceHolder = suraceHolder;  
    12.     }  
    13.   
    14.     @Override  
    15.     public void run() {  
    16.         Canvas canvas = null;  
    17.         while(isRunning){  
    18.             try{  
    19.                 canvas = suraceHolder.lockCanvas();  
    20.                 synchronized(suraceHolder){  
    21.                     pv.doDraw(canvas);  
    22.                 }  
    23.             }catch(Exception e){  
    24.                 e.printStackTrace();  
    25.             }finally{  
    26.                 if(canvas!=null){  
    27.                     suraceHolder.unlockCanvasAndPost(canvas);  
    28.                 }  
    29.             }  
    30.             this.count++;  
    31.             if(count == 20){  
    32.                 count = 0;  
    33.                 long tempStamp = System.nanoTime(); //获取当前时间  
    34.                 long span = tempStamp - start;      //获取时间间隔  
    35.                 start = tempStamp;                  //为start重新赋值  
    36.                 double fps = Math.round(100000000000.0/span*20)/100.0;//计算帧速率  
    37.                 pv.fps = "FPS:"+fps;//将计算出的帧速率设置到BallView的相应字符串对象中  
    38.                   
    39.             }  
    40.             try{  
    41.                 Thread.sleep(sleepSpan);//线程休眠一段时间  
    42.             }  
    43.             catch(Exception e){  
    44.                 e.printStackTrace();//捕获并打印异常  
    45.             }  
    46.         }  
    47.     }  
    48.   
    49. }  

    DrawThread.java

     

    1. public class ActivityMain extends Activity {  
    2.     /** Called when the activity is first created. */  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         requestWindowFeature(Window.FEATURE_NO_TITLE);      //不显示标题  
    7.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
    8.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    9.         ParticleView lz = new ParticleView(this);  
    10.         setContentView(lz);  
    11.     }  
    12. }  

    Particle.java

     

    1. public class Particle {  
    2.     int color;          //粒子颜色  
    3.     int r;              //粒子半径  
    4.     double ver_v;       //垂直速度  
    5.     double hor_v;       //水平速度  
    6.     int startX;         //初始X坐标  
    7.     int startY;         //初始Y坐标  
    8.     int x;              //实时X坐标  
    9.     int y;              //实时Y坐标  
    10.     double startTime;   //起始时间  
    11.       
    12.     public Particle(int color, int r, double ver_v, double hor_v, int x, int y, double startTime) {  
    13.         super();  
    14.         this.color = color;  
    15.         this.r = r;  
    16.         this.ver_v = ver_v;  
    17.         this.hor_v = hor_v;  
    18.         this.startX = x;  
    19.         this.startY = y;  
    20.         this.x = x;  
    21.         this.y = y;  
    22.         this.startTime = startTime;  
    23.     }  
    24. }  

    ParticleSet.java

     

    1. public class ParticleSet {  
    2.     ArrayList<Particle> particleSet;  
    3.     public ParticleSet(){  
    4.         particleSet = new ArrayList<Particle>();  
    5.     }  
    6.       
    7.     public void addParticle(int count,double startTime){  
    8.         for(int i=0;i<count;i++){  
    9.             int color = this.getColor(i);  
    10.             int r = 1;  
    11.             double ver_v = -30 + 10*(Math.random());  
    12.             double hor_v = 10 - 20*(Math.random());  
    13.             int x = 160;  
    14.             int y = (int)(100 - 10*(Math.random()));  
    15.             Particle particle = new Particle(color, r, ver_v, hor_v, x, y, startTime);  
    16.             particleSet.add(particle);  
    17.         }  
    18.     }  
    19.     public int getColor(int i){  
    20.         int color = Color.RED;  
    21.         switch(i%4){  
    22.             case 0:  
    23.                 color = Color.RED;  
    24.                 break;  
    25.             case 1:  
    26.                 color = Color.BLUE;  
    27.                 break;  
    28.             case 2:  
    29.                 color = Color.YELLOW;  
    30.                 break;  
    31.             case 3:  
    32.                 color = Color.GRAY;  
    33.                 break;  
    34.         }  
    35.         return color;  
    36.     }  
    37. }  

    ParticleThread.java

     

    1. public class ParticleThread extends Thread{  
    2.     boolean isRunning;  
    3.     ParticleView father;  
    4.     int sleepSpan = 80;  
    5.     double time = 0;  
    6.     double span = 0.15;  
    7.       
    8.     public ParticleThread(ParticleView father) {  
    9.         this.isRunning = true;  
    10.         this.father = father;  
    11.     }  
    12.   
    13.     @Override  
    14.     public void run() {  
    15.         while(isRunning){  
    16.             father.ps.addParticle(5, time);  
    17.             ArrayList<Particle> tempSet = father.ps.particleSet;  
    18.             int count = tempSet.size();  
    19.             for(int i=0;i<count;i++){  
    20.                 Particle particle = tempSet.get(i);  
    21.                 double timeSpan = time - particle.startTime;  
    22.                 //计算X坐标  
    23.                 int tempx = (int)(particle.startX+particle.hor_v*timeSpan);  
    24.                 //计算Y坐标  
    25.                 int tempy = (int)(particle.startY+particle.ver_v*timeSpan+4.9*timeSpan*timeSpan);  
    26.                 //超过屏幕下边  
    27.                 if(tempy>300){  
    28.                     tempSet.remove(particle);  
    29.                     count = tempSet.size();  
    30.                 }  
    31.                 particle.x = tempx;  
    32.                 particle.y = tempy;  
    33.             }  
    34.             time += span;  
    35.             try{  
    36.                 Thread.sleep(sleepSpan);  
    37.             }catch(Exception ex){  
    38.                 ex.printStackTrace();  
    39.             }  
    40.         }  
    41.         super.run();  
    42.     }  
    43. }  

     

    ParticleView.java

     

    1. public class ParticleView extends SurfaceView implements SurfaceHolder.Callback{  
    2.     public static final int DIE_OUT_LINE = 300;  
    3.     DrawThread dt;  
    4.     ParticleSet ps;  
    5.     ParticleThread pt;  
    6.     String fps="FPS:N/A";  
    7.   
    8.   
    9.     public ParticleView(Context context) {  
    10.         super(context);  
    11.         this.getHolder().addCallback(this);  
    12.         dt = new DrawThread(this,getHolder());  
    13.         ps = new ParticleSet();  
    14.         pt = new ParticleThread(this);  
    15.     }  
    16.   
    17.     @Override  
    18.     public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {  
    19.         // TODO Auto-generated method stub  
    20.           
    21.     }  
    22.   
    23.     @Override  
    24.     public void surfaceCreated(SurfaceHolder holder) {  
    25.         // TODO Auto-generated method stub  
    26.         if(!dt.isAlive()){  
    27.             dt.start();  
    28.         }  
    29.         if(!pt.isAlive()){  
    30.             pt.start();  
    31.         }  
    32.     }  
    33.   
    34.     @Override  
    35.     public void surfaceDestroyed(SurfaceHolder holder) {  
    36.         // TODO Auto-generated method stub  
    37.         dt.isRunning = false;  
    38.         dt = null;  
    39.     }  
    40.   
    41.     public void doDraw(Canvas canvas) {  
    42.         // TODO Auto-generated method stub  
    43.         canvas.drawColor(Color.BLACK);  
    44.         ArrayList<Particle> particleSet = ps.particleSet;  
    45.         Paint paint = new Paint();  
    46.         //绘制粒子  
    47.         for(int i=0;i<particleSet.size();i++){  
    48.             Particle p = particleSet.get(i);  
    49.             paint.setColor(p.color);  
    50.             int tempX = p.x;  
    51.             int tempY = p.y;  
    52.             int tempR = p.r;  
    53.             RectF oval = new RectF(tempX,tempY,tempX+2*tempR, tempY+2*tempR);  
    54.             canvas.drawOval(oval, paint);  
    55.         }  
    56.         paint.setColor(Color.WHITE);  
    57.         paint.setTextSize(18);              //字体大小  
    58.         paint.setAntiAlias(true);           //设置抗锯齿  
    59.         canvas.drawText(fps, 1515, paint);  
    60.     }  
    61. }  

    运行结果

     

  • 相关阅读:
    ubuntu 14.04 下试用Sublime Text 3
    闲来无事,温习一下快速排序法
    学艺不精,又被shell的管道给坑了
    ssh登录失败处理步骤
    linux文件权限整理
    使用ssh远程执行命令批量导出数据库到本地
    leetcode-easy-design-384 Shuffle an Array
    leetcode-easy-dynamic-198 House Robber-NO
    leetcode-easy-dynamic-53 Maximum Subarray
    leetcode-easy-dynamic-121 Best Time to Buy and Sell Stock
  • 原文地址:https://www.cnblogs.com/firecode/p/2858238.html
Copyright © 2011-2022 走看看