先上图
其实比较简单,其实就是canvas绘制图形,步骤就不赘述了,直接上代码
public class Particle {
//火焰粒子坐标
private float x;
private float y;
public float xv = 1;//x轴加速度
public float yv = 1;//y轴加速度
public int alpha;
public Particle(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
//500-200为火焰的y轴左边范围,所以根据这个计算透明度
alpha = (int) ((y-200)*255/300);
}
}
粒子系统类
public class ParticleSystem {
private List<Particle> particles = new ArrayList<>();
private int border_width = 20;
private Paint paint = new Paint();
private Random random;
public ParticleSystem(int particlesCount) {
paint.setColor(Color.RED);
random = new Random();
for (int i = 0; i < particlesCount; i++) {
Particle particle = new Particle(100 + 100 * random.nextFloat(), 500);
particle.xv = 0.5f - 1 * random.nextFloat();
particles.add(particle);
}
}
public void onDraw(Canvas canvas) {
for (int i = 0; i < particles.size(); i++) {
Particle particle = particles.get(i);
paint.setAlpha(particle.alpha);
canvas.drawRect(particle.getX(), particle.getY(), particle.getX() + border_width, particle.getY() + border_width, paint);
}
}
/**
* 粒子集合更新方法
*
* @param wind wind是风,0时代表无风,粒子不偏移
*/
public void update(float wind) {
for (int i = 0; i < particles.size(); i++) {
Particle particle = particles.get(i);
if (particle.xv > 0) {
//当初始加速度为正,就一直为正
particle.xv += 0.01f + wind;
} else {
//当初始加速度为负,就一直为正
particle.xv += -0.01f + wind;
}
particle.yv = particle.yv + 0.1f;
particle.setX(particle.getX() + particle.xv);
particle.setY(particle.getY() - particle.yv);
}
List<Particle> list = new ArrayList<>();
list.addAll(particles);
for (Particle particle : list) {
if (particle.getY() < 200) {
particles.remove(particle);
}
}
for (int i = 0; i < 5; i++) {
Particle particle = new Particle(100 + 100 * random.nextFloat(), 500);
particle.xv = 0.5f - 1 * random.nextFloat();
particles.add(particle);
}
}
}
public class MyView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder holder;
private MyThread thread;
public boolean running = true;
private ParticleSystem particleSystem;
private Paint paint;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
holder = getHolder();
holder.addCallback(this);
thread = new MyThread();
particleSystem = new ParticleSystem(50);
paint = new Paint();
paint.setColor(Color.BLACK);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
running = true;
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
running = false;
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(true){
if(running){
drawUI();
}
}
}
}
private void drawUI(){
Canvas canvas = holder.lockCanvas();
if(canvas==null){
return;
}
canvas.drawRect(0,0,getWidth(),getHeight(),paint);
particleSystem.onDraw(canvas);
particleSystem.update(new Random().nextFloat()*0.18f);
holder.unlockCanvasAndPost(canvas);
}
}