婚庆网站开发的意义,东莞专业设计网站,淘宝接网站开发的活,今天开始做女神免费网站一、前言#xff1a;
在学习open gl es实现动效的时候#xff0c;打算回顾了一下用普通的2D坐标系实现粒子效果和 open gl 3d 坐标系的区别#xff0c;以及难易程度#xff0c;因此本篇以Canvas 2D坐标系实现了一个简单的demo。 粒子动效原理#xff1a;
粒子动效本质上…一、前言
在学习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各个分量的运动学公式。
粒子动效的特点 具备起点位置 需要计算出速度和运动角度当然难点也是速度的计算和定义。 符合运动学方程但与现实规律有区别因为在手机中使用的单位和重力加速度都是有一定区别的。
二、代码实现
2.1 构建粒子对象在open gl中由于没有对象化的概念绘制时通过数组的偏移实现当然后果是代码可读性差一些。
public class Particle {private float speedZ 0;private float x;private float y;private float speedX;private float speedY;int color;long startTime;private float radius 10;public Particle(float x, float y, float speedX, float speedY, int color,float speedZ,long clockTime) {this.x x;this.y y;this.speedX speedX;this.speedY speedY;this.speedZ speedZ;this.color color;this.startTime clockTime;}public void draw(Canvas canvas, long clockTime, Paint paint) {long costTime (clockTime - startTime)/2;float gravityY costTime * costTime / 3000f; //重力加速度float dx costTime * speedX;float dy costTime * speedY gravityY;float v costTime / 500f;float ty y dy; // vt t*t/2*gfloat tx x dx;int paintColor paint.getColor();if(v 1f speedZ ! 1) {//非z轴正半轴的降低透明度int argb argb((int) (Color.alpha(color) /v), Color.red(color), Color.green(color), Color.blue(color));paint.setColor(argb);}else {paint.setColor(color);}float tRadius radius;//这只Blend叠加效果这个api版本较高 paint.setBlendMode(BlendMode.DIFFERENCE); canvas.drawCircle(tx,ty,tRadius,paint);paint.setColor(paintColor);if(ty radius){reset(clockTime);}}private void reset(long clockTime) {startTime clockTime;}public static int argb(IntRange(from 0, to 255) int alpha,IntRange(from 0, to 255) int red,IntRange(from 0, to 255) int green,IntRange(from 0, to 255) int blue) {return (alpha 24) | (red 16) | (green 8) | blue;}
} 2.2 构建粒子系统
public class CanvasParticleSystem {private Particle[] particles;private int maxParticleCount 500;private Random random new Random();private final float angle 30f; //x轴的活动范围private int index 0;private float radius 60; //x轴和y轴不能超过的边界public void addParticle(float centerX,float centerY,float maxWidth,float maxHeight,long clockTime){if(particles null){particles new Particle[maxParticleCount];}if(index particles.length) {return;}float degree (float) Math.toRadians((270 - angle) 2f * angle * random.nextFloat());float dx (float) (radius * Math.cos(degree)) * 2f; //计算初目标位置x的随机点float dy -(float) ((maxHeight * 1f / 2 - radius * 2f) * random.nextFloat()) - maxHeight / 2f;//计算目标y的随机点float dt 1000; //时间按1s计算// dx speedx * dt centerX;// dy speedy * dt centerY;float sx (dx - centerX) / dt; // x轴方向的速度float sy (dy - centerY) / dt; //y轴方向的速度int num (int) (random.nextFloat() * 100);float sz 0;if(num % 5 0) {sz random.nextBoolean() ? -1 : 1;}int argb argb(random.nextFloat(), random.nextFloat(), random.nextFloat());// argb argb(210, 110, 80);Particle p new Particle(centerX,centerY,sx,sy, argb,sz,clockTime);particles[index] p;}public void drawFrame(Canvas canvas, Paint paint,long clockTime) {for (int i 0; i particles.length;i) {Particle particle particles[i];if(particle null) continue;particle.draw(canvas,clockTime,paint);}}public int argb( float red, float green, float blue) {return ((int) (1 * 255.0f 0.5f) 24) |((int) (red * 255.0f 0.5f) 16) |((int) (green * 255.0f 0.5f) 8) |(int) (blue * 255.0f 0.5f);}
}2.3 粒子View实现
public class PracticeView extends View {Paint paint;CanvasParticleSystem particleSystem;private long clockTime 0L; //自定义时钟防止粒子堆积long startTimeout 0; public PracticeView(Context context) {super(context);init();}public PracticeView(Context context, Nullable AttributeSet attrs) {super(context, attrs);init();}public PracticeView(Context context, Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}public void init(){paint new Paint();paint.setAntiAlias(true);paint.setDither(false);paint.setStrokeWidth(2f);particleSystem new CanvasParticleSystem();}Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width getWidth();int height getHeight();if (width 10 || height 10) {return;}int save canvas.save();canvas.translate(width/2,height);fillParticles(5,width, height);particleSystem.drawFrame(canvas,paint,getClockTime());canvas.restoreToCount(save);clockTime 32;postInvalidateDelayed(16);}private void fillParticles(int size,int width, int height) {if(SystemClock.uptimeMillis() - startTimeout 60) {for (int i 0; i size; i) {particleSystem.addParticle(0, 0, width, height,getClockTime());}startTimeout SystemClock.uptimeMillis();}}private long getClockTime() {return clockTime;}
}三、总结
总体上使用Canvas 绘制高帧率的粒子动效其对比open gl肯定有很多差距甚至有一些天然缺陷比如Z轴的处理。当然易用性肯定是Canvas 2D的优势了。 文章转载自: http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.wjndl.cn.gov.cn.wjndl.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn