• 粒子效果的简单实现


    1.封装一个粒子对象类:

     1 /*************************
     2  *@author 徐宏明    E-Mail:android_xhm.126.com
     3  *                                        QQ:294985925
     4  *@version 创建时间            :2013-5-28 上午10:56:48
     5  *
     6  *@see
     7  *************************/
     8 package com.example.mytest;
     9 
    10 public class Particle {
    11     int p_radius;// 粒子的半径
    12     double v_s, h_s;// 粒子的垂直和水平速度
    13     int p_color;// 粒子的颜色
    14     int startX, startY;// 粒子开始的位置
    15     int contentX, contentY;// 粒子的实时位置
    16     double contenttime;// 粒子开始的时间
    17 
    18     public Particle(int r, double vs, double hs, int color, int contentX,
    19             int contentY,double time) {
    20         // TODO Auto-generated constructor stub
    21         this.p_radius = r;
    22         this.v_s = vs;
    23         this.h_s = hs;
    24         this.p_color = color;
    25         this.startX = contentX;
    26         this.startY = contentY;
    27         this.contentX = contentX;
    28         this.contentY = contentY;
    29         this.contenttime = time;
    30     }
    31 }

    2.创建粒子对象类:

    /*************************
     *@author 徐宏明    E-Mail:android_xhm.126.com
     *                                        QQ:294985925
     *@version 创建时间            :2013-5-28 上午11:08:48
     *
     *@see
     *************************/
    package com.example.mytest;
    
    import java.util.ArrayList;
    
    import android.graphics.Color;
    
    public class GetParticles {
        ArrayList<Particle> particles;
    
        public GetParticles() {
            // TODO Auto-generated constructor stub
            particles = new ArrayList<Particle>();
        }
    
        /*
         * 增加粒子数量
         * 
         * @param count 一次增加的个数
         * 
         * @param time 增加粒子的起始时间
         */
        public void addParticle(int count, double time) {
            for (int i = 0; i < count; i++) {
                int r = 1;// 粒子的半径
                int color = getColor(i);// 粒子的颜色
                double h_s = 5;
                double v_s = 3;// 垂直速度
                int x = 160;
                int y = (int) (100 - 10 * (Math.random())); // 随机产生粒子Y坐标
                particles.add(new Particle(r, v_s, h_s, color, x, y, time));
            }
        }
    
        // 为粒子设置颜色
        private 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.BLUE;
                break;
            case 3:
                color = Color.YELLOW;
                break;
            default:
                break;
            }
            return color;
        }
    }

    3.粒子坐标变化类:

     1 /*************************
     2  *@author 徐宏明    E-Mail:android_xhm.126.com
     3  *                                        QQ:294985925
     4  *@version 创建时间            :2013-5-28 上午11:30:28
     5  *
     6  *@see
     7  *************************/
     8 package com.example.mytest;
     9 
    10 /**
    11  * 计算当前粒子的位置,超出范围的粒子移除集合
    12  * 
    13  * @author 宏明
    14  * 
    15  */
    16 public class ParticleChangeThread extends Thread {
    17     // 获得粒子的类
    18     private GetParticles particles;
    19     // 线程控制标识
    20     public boolean isRun;
    21     // 粒子坐标变化进步量
    22     private double span = 0.15;
    23 
    24     public ParticleChangeThread(MySurface surface) {
    25         // TODO Auto-generated constructor stub
    26         particles = surface.getParticles;
    27         this.isRun = true;
    28     }
    29 
    30     @Override
    31     public void run() {
    32         // TODO Auto-generated method stub
    33         super.run();
    34         double time = 0;
    35         while (isRun) {
    36             // 每次循环添加5个粒子
    37             particles.addParticle(5, time);
    38             for (int i = 0; i < particles.particles.size(); i++) {
    39                 Particle p = particles.particles.get(i);
    40                 double timespan = time - p.contenttime; // 获得粒子X坐标
    41                 p.contentX = (int) (p.startX + timespan * p.h_s);
    42                 p.contentY = (int) (p.startY + timespan * p.v_s);
    43                 // 当粒子超过屏幕范围时,移除粒子
    44                 if (p.contentY > 800 || p.contentY < 0 || p.contentX > 400
    45                         || p.contentX < 0) {
    46                     particles.particles.remove(p);
    47                 }
    48             }
    49             time += span;
    50             try {
    51                 sleep(60);
    52             } catch (InterruptedException e) {
    53                 // TODO Auto-generated catch block
    54                 e.printStackTrace();
    55             }
    56         }
    57     }
    58 }

    4.画粒子线程:

    /*************************
     *@author 徐宏明    E-Mail:android_xhm.126.com
     *                                        QQ:294985925
     *@version 创建时间            :2013-5-28 下午4:23:18
     *
     *@see
     *************************/
    package com.example.mytest;
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.view.SurfaceHolder;
    
    public class DrawThread extends Thread {
        // 线程控制标识
        public boolean isRun;
        // surface控制对象
        private SurfaceHolder mHolder;
        // mysurface对象
        private MySurface mSurface;
    
        public DrawThread(SurfaceHolder holder, MySurface surface) {
            // TODO Auto-generated constructor stub
            mHolder = holder;
            mSurface = surface;
            this.isRun = true;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            Canvas c = null;
    
            while (isRun) {
                c = mHolder.lockCanvas();
                synchronized (mHolder) {
                    draw(c);
                }
                if (c != null) {
                    mHolder.unlockCanvasAndPost(c);
                }
                try {
                    sleep(0);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        // 画粒子
        private void draw(Canvas c) {
            c.drawColor(Color.WHITE);
            Paint t = new Paint();
            for (int i = 0; i < mSurface.particles.size(); i++) {
                Particle p = mSurface.particles.get(i);
                t.setColor(p.p_color);
                RectF oval = new RectF(p.contentX, p.contentY, p.contentX + 2
                        * p.p_radius, p.contentY + 2 * p.p_radius);
                c.drawOval(oval, t);
            }
        }
    }

    5MySurface类:

     1 /*************************
     2  *@author 徐宏明    E-Mail:android_xhm.126.com
     3  *                                        QQ:294985925
     4  *@version 创建时间            :2013-5-28 下午4:25:45
     5  *
     6  *@see
     7  *************************/
     8 package com.example.mytest;
     9 
    10 import java.util.ArrayList;
    11 
    12 import android.content.Context;
    13 import android.view.SurfaceHolder;
    14 import android.view.SurfaceView;
    15 
    16 public class MySurface extends SurfaceView implements SurfaceHolder.Callback {
    17     public ArrayList<Particle> particles;
    18     public GetParticles getParticles;
    19     private DrawThread draw;
    20     private ParticleChangeThread change;
    21 
    22     public MySurface(Context context) {
    23         super(context);
    24         // TODO Auto-generated constructor stub
    25         this.getHolder().addCallback(this);
    26         getParticles = new GetParticles();
    27         particles = getParticles.particles;
    28         draw = new DrawThread(getHolder(), this);
    29         change = new ParticleChangeThread(this);
    30     }
    31 
    32     @Override
    33     public void surfaceCreated(SurfaceHolder holder) {
    34         // TODO Auto-generated method stub
    35         System.out.println(draw.isAlive());
    36         if (!draw.isAlive()) {
    37             draw.start();
    38         }
    39         if (!change.isAlive()) {
    40             change.start();
    41         }
    42     }
    43 
    44     @Override
    45     public void surfaceChanged(SurfaceHolder holder, int format, int width,
    46             int height) {
    47         // TODO Auto-generated method stub
    48 
    49     }
    50 
    51     @Override
    52     public void surfaceDestroyed(SurfaceHolder holder) {
    53         // TODO Auto-generated method stub
    54         draw.isRun = false;
    55         draw = null;
    56         change.isRun = false;
    57         change = null;
    58     }
    59 
    60 }
  • 相关阅读:
    apply call bind方法的区别和含义
    html头部meta标签
    语义化标签
    “文件名和url路径名”命名要点以及大小写问题
    BMP GIF PNG JPG等图片格式的区别和适用情况
    前端页面的性能优化
    js阻止默认事件,如a标签跳转和事件冒泡
    散列碰撞问题的解决——开链法(拉链法)
    substring()方法
    对学生成绩进行散列
  • 原文地址:https://www.cnblogs.com/qinghuaideren/p/3106179.html
Copyright © 2020-2023  润新知