Android小程序--模拟焰火粒子和瀑布粒子

这篇博客分享了如何在Android平台上实现焰火和瀑布的粒子效果。通过下载提供的particle.zip文件,读者可以获取相关代码资源,进一步了解和学习Android小程序中粒子系统的创建与应用。
摘要由CSDN通过智能技术生成
   需要注意的是,粒子系统和前面开发的物理小球之间有类似的地方,其都是通过数学方法和物理公式模拟客观世界中物体的运动轨迹。不同的是物理小球更强调个体运动,而焰火粒子系统更注重整体感觉,这点区别在代码中也能体现。
   1、开发粒子对象Particle类:

package xiao.fuyan.particle;

/**
 * Created by xiao on 2017/2/10.
 */
public class Particle {
    //粒子半径
    int r;
    //粒子颜色
    int color;
    //垂直速度
    double v_v;
    //水平速度
    double h_v;
    //初始坐标
    int startX;
    int startY;
    //实时坐标
    int x, y;
    //起始时间
    double startTime;

    //构造器
    public Particle(int color, int r, double v_v, double h_v, int x, int y, double startTime){
        //初始化各个变量
        this.color = color;
        this.r = r;
        this.v_v = v_v;
        this.h_v = h_v;
        this.startX = x;
        this.startY = y;
        this.x = x;
        this.y = y;
        this.startTime = startTime;
    }
}
   2、开发粒子集合ParticleSet类:
package xiao.fuyan.particle;

import android.graphics.Color;

import java.util.ArrayList;

/**
 * Created by xiao on 2017/2/10.
 */
public class ParticleSet {
    //用于存放Particle对象的集合
    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 = 3;
            //随机产生粒子竖直方向上的速度
            double tempv_v = -30 + 10 * (Math.random());
            //同上获取水平方向上的速度
            double temph_v = 10 - 20 * (Math.random());

            //粒子的X坐标是固定的
            int tempX = 160;
            //随机产生Y坐标,90 - 100之间
            int tempY = (int) (100 - 10 * (Math.random()));

            Particle particle = new Particle(tempColor, tempR, tempv_v, temph_v, tempX, tempY, startTime);
            //将创建好的Particle对象添加到列表中
            particleSet.add(particle);

            //下面代码是将焰火粒子变为瀑布粒子
//  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值