当前位置:   article > 正文

Android 粒子喷泉动效

Android 粒子喷泉动效

一、前言:

在学习open gl es实现动效的时候,打算回顾了一下用普通的2D坐标系实现粒子效果和 open gl 3d 坐标系的区别,以及难易程度,因此本篇以Canvas 2D坐标系实现了一个简单的demo。

粒子动效原理:

粒子动效本质上是一种知道起点和各个坐标轴方向速度的无规则运动,这种动效的实现的算法确实有规则的。

我们以物理学公式为例,本质上是一种匀加速矢量方程,至于为什么忽快忽慢,也是从该类方程延伸出来的新算法。

x = startX  +  (Vx*t + 1/2*aX*t * t)

y = startY + (Vy * t + 1/2*aY*t * t)

t: 时间  ,vY,vX 各个方向的速度,aX,aY各个方向的加速度

当然,用向量解释就是向量A到向量B各个分量的运动学公式。

粒子动效的特点:

  1. 具备起点位置

  2. 需要计算出速度和运动角度,当然,难点也是速度的计算和定义。

  3. 符合运动学方程,但与现实规律有区别,因为在手机中使用的单位和重力加速度都是有一定区别的。

二、代码实现

2.1 构建粒子对象,在open gl中由于没有对象化的概念,绘制时通过数组的偏移实现,当然后果是代码可读性差一些。

  1. public class Particle {
  2. private float speedZ = 0;
  3. private float x;
  4. private float y;
  5. private float speedX;
  6. private float speedY;
  7. int color;
  8. long startTime;
  9. private float radius = 10;
  10. public Particle(float x, float y, float speedX, float speedY, int color,float speedZ,long clockTime) {
  11. this.x = x;
  12. this.y = y;
  13. this.speedX = speedX;
  14. this.speedY = speedY;
  15. this.speedZ = speedZ;
  16. this.color = color;
  17. this.startTime = clockTime;
  18. }
  19. public void draw(Canvas canvas, long clockTime, Paint paint) {
  20. long costTime = (clockTime - startTime)/2;
  21. float gravityY = costTime * costTime / 3000f; //重力加速度
  22. float dx = costTime * speedX;
  23. float dy = costTime * speedY + gravityY;
  24. float v = costTime / 500f;
  25. float ty = y + dy; // vt + t*t/2*g
  26. float tx = x + dx;
  27. int paintColor = paint.getColor();
  28. if(v > 1f && speedZ != 1) {
  29. //非z轴正半轴的降低透明度
  30. int argb = argb((int) (Color.alpha(color) /v), Color.red(color), Color.green(color), Color.blue(color));
  31. paint.setColor(argb);
  32. }else {
  33. paint.setColor(color);
  34. }
  35. float tRadius = radius;
  36. //这只Blend叠加效果,这个api版本较高 paint.setBlendMode(BlendMode.DIFFERENCE);
  37. canvas.drawCircle(tx,ty,tRadius,paint);
  38. paint.setColor(paintColor);
  39. if(ty > radius){
  40. reset(clockTime);
  41. }
  42. }
  43. private void reset(long clockTime) {
  44. startTime = clockTime;
  45. }
  46. public static int argb(
  47. @IntRange(from = 0, to = 255) int alpha,
  48. @IntRange(from = 0, to = 255) int red,
  49. @IntRange(from = 0, to = 255) int green,
  50. @IntRange(from = 0, to = 255) int blue) {
  51. return (alpha << 24) | (red << 16) | (green << 8) | blue;
  52. }
  53. }

 

2.2 构建粒子系统

  1. public class CanvasParticleSystem {
  2. private Particle[] particles;
  3. private int maxParticleCount = 500;
  4. private Random random = new Random();
  5. private final float angle = 30f; //x轴的活动范围
  6. private int index = 0;
  7. private float radius = 60; //x轴和y轴不能超过的边界
  8. public void addParticle(float centerX,float centerY,float maxWidth,float maxHeight,long clockTime){
  9. if(particles == null){
  10. particles = new Particle[maxParticleCount];
  11. }
  12. if(index >= particles.length) {
  13. return;
  14. }
  15. float degree = (float) Math.toRadians((270 - angle) + 2f * angle * random.nextFloat());
  16. float dx = (float) (radius * Math.cos(degree)) * 2f; //计算初目标位置x的随机点
  17. float dy = -(float) ((maxHeight * 1f / 2 - radius * 2f) * random.nextFloat()) - maxHeight / 2f;
  18. //计算目标y的随机点
  19. float dt = 1000; //时间按1s计算
  20. // dx = speedx * dt + centerX;
  21. // dy = speedy * dt + centerY;
  22. float sx = (dx - centerX) / dt; // x轴方向的速度
  23. float sy = (dy - centerY) / dt; //y轴方向的速度
  24. int num = (int) (random.nextFloat() * 100);
  25. float sz = 0;
  26. if(num % 5 == 0) {
  27. sz = random.nextBoolean() ? -1 : 1;
  28. }
  29. int argb = argb(random.nextFloat(), random.nextFloat(), random.nextFloat());
  30. // argb = argb(210, 110, 80);
  31. Particle p = new Particle(centerX,centerY,sx,sy, argb,sz,clockTime);
  32. particles[index++] = p;
  33. }
  34. public void drawFrame(Canvas canvas, Paint paint,long clockTime) {
  35. for (int i = 0; i < particles.length;i++) {
  36. Particle particle = particles[i];
  37. if(particle == null) continue;
  38. particle.draw(canvas,clockTime,paint);
  39. }
  40. }
  41. public int argb( float red, float green, float blue) {
  42. return ((int) (1 * 255.0f + 0.5f) << 24) |
  43. ((int) (red * 255.0f + 0.5f) << 16) |
  44. ((int) (green * 255.0f + 0.5f) << 8) |
  45. (int) (blue * 255.0f + 0.5f);
  46. }
  47. }

2.3 粒子View实现

  1. public class PracticeView extends View {
  2. Paint paint;
  3. CanvasParticleSystem particleSystem;
  4. private long clockTime = 0L; //自定义时钟,防止粒子堆积
  5. long startTimeout = 0;
  6. public PracticeView(Context context) {
  7. super(context);
  8. init();
  9. }
  10. public PracticeView(Context context, @Nullable AttributeSet attrs) {
  11. super(context, attrs);
  12. init();
  13. }
  14. public PracticeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  15. super(context, attrs, defStyleAttr);
  16. init();
  17. }
  18. public void init(){
  19. paint = new Paint();
  20. paint.setAntiAlias(true);
  21. paint.setDither(false);
  22. paint.setStrokeWidth(2f);
  23. particleSystem = new CanvasParticleSystem();
  24. }
  25. @Override
  26. protected void onDraw(Canvas canvas) {
  27. super.onDraw(canvas);
  28. int width = getWidth();
  29. int height = getHeight();
  30. if (width <= 10 || height <= 10) {
  31. return;
  32. }
  33. int save = canvas.save();
  34. canvas.translate(width/2,height);
  35. fillParticles(5,width, height);
  36. particleSystem.drawFrame(canvas,paint,getClockTime());
  37. canvas.restoreToCount(save);
  38. clockTime += 32;
  39. postInvalidateDelayed(16);
  40. }
  41. private void fillParticles(int size,int width, int height) {
  42. if(SystemClock.uptimeMillis() - startTimeout > 60) {
  43. for (int i = 0; i < size; i++) {
  44. particleSystem.addParticle(0, 0, width, height,getClockTime());
  45. }
  46. startTimeout = SystemClock.uptimeMillis();
  47. }
  48. }
  49. private long getClockTime() {
  50. return clockTime;
  51. }
  52. }

三、总结

总体上使用Canvas 绘制高帧率的粒子动效,其对比open gl肯定有很多差距,甚至有一些天然缺陷比如Z轴的处理。当然,易用性肯定是Canvas 2D的优势了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/105103
推荐阅读
相关标签
  

闽ICP备14008679号