设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间11 现在时间11 缓存数据 "如果你喜欢一个人, 一定要告诉她 不是为了要她报答, 而是让她在以后黑暗的日子里, 否定自己的时候, 想起世界上还有人这么喜欢她 她并非一无是处。"

"如果你喜欢一个人, 一定要告诉她 不是为了要她报答, 而是让她在以后黑暗的日子里, 否定自己的时候, 想起世界上还有人这么喜欢她 她并非一无是处。" -- 感谢你曾来过

查看: 1054|回复: 4

Android实现粒子中心扩散动画效果

[复制链接]

  离线 

TA的专栏

  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:19
  • 最近打卡:2024-04-03 04:13:10
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
32
主题
22
精华
0
金钱
106
积分
56
注册时间
2023-9-29
最后登录
2025-5-31

发表于 2024-9-11 06:00:09 | 显示全部楼层 |阅读模式
目录


  • 前言
  • 实现步骤

    • 粒子对象定义
    • 粒子更新
    • 粒子绘制方法
    • 粒子回收
    • View逻辑
    • 绘制逻辑
    • 更新粒子

  • 效果调节
  • 总结
  • 本篇代码

前言

粒子动画效果相比其他动画来说是非常复杂了的,主要涉及三个方面,粒子初始化、粒子位移、粒子回收等问题,其中特别是粒子位移是最复杂的,涉及到的数学逻辑非常多,主要是各种三角函数、物理学公式等。
本篇将实现两种动画效果,代码基本相同,只是旋转速度不一样,因此,本篇其实可以看作一篇模板文章,具体效果可以通过调节参数生成各种动画
第一种动画
1.webp

第二种动画
2.webp


实现步骤

其实和以往的粒子效果一样,粒子需要被管理起来,因此我们需要有容器、也需要粒子对象

粒子对象定义

下面是创建粒子对象的逻辑,基本属性在注释中了
  1. static class Circle {
  2.     int maxLength;  //最大运行距离
  3.     float speed; //外扩速度
  4.     float rotate; // 角速度
  5.     private float degree; //起始角度
  6.     private int y; //y坐标
  7.     private int x; //x坐标
  8.     private int color; //颜色
  9.     private float radius; //小圆半径
  10.     private float drawRadius; //绘制时的小圆半径
  11.    
  12.    
  13.   public Circle(int color, int maxLength, float radius, float degree) {
  14.     this.color = color;
  15.     this.radius = radius;
  16.     this.maxLength = maxLength;
  17.     this.degree = degree;
  18.     this.x = (int) (radius * Math.cos(degree));
  19.     this.y = (int) (radius * Math.sin(degree));
  20.     this.rotate = 0.35f;  //触角效果
  21.     this.speed = 0.2f;
  22. }
  23. }
复制代码
粒子更新

在任何动画中,粒子运动必须具备时间属性,任何符合物理学的位移运动,速度和时间的关系是位移计算的方法。下面,我们继续给Circle类添加更新方法。
这里一个重要的知识点是

  • Math.hypot(x, y) :平方根计算
  • Math.atan2(y, x): 斜率计算,注意,此角度具备方向
  1. public boolean update(long timeline) {
  2.     float length = (float) Math.hypot(x, y);  //计算当前移动的距离(距离中心点)
  3.     float center = length + this.speed * timeline; //计算即将到达的距离
  4.     float ratio = center / maxLength;  //计算与最远距离的比值

  5.     this.drawRadius = (1f - ratio) * radius;  //距离越远,圆的半径越小

  6.     double degree = Math.atan2(y, x) + rotate;  //即将旋转的角度

  7.     this.x = (int) (center * Math.cos(degree)); //新的x
  8.     this.y = (int) (center * Math.sin(degree)); //新的y

  9.     if (drawRadius <= 0) {
  10.         return false; //如果半径为0时,意味着圆看不见了,因此要坐下标记
  11.     }
  12.     return true;
  13. }
复制代码
粒子绘制方法

绘制自身其实很简单,只需要简单的调用Canvas相关逻辑即可
  1. public void draw(Canvas canvas, TextPaint paint) {
  2.     paint.setColor(color);
  3.     canvas.drawCircle(x, y, drawRadius, paint);
  4. }
复制代码
粒子回收

为了减少内存申请频率,我们对跑出边界的粒子进行重置
  1. public void reset() {
  2.     this.x = (int) (radius * Math.cos(degree));
  3.     this.y = (int) (radius * Math.sin(degree));
  4. }
复制代码
View逻辑

以上是完整的粒子对象逻辑,接下来我们实现一个View,用来管理和绘制粒子。
  1. int maxCircleRadius = 20;  //粒子初始半径
  2. List<Circle> circleList = new ArrayList<>(); //容器
  3. int maxCircleNum = 300; //最大数量
复制代码
绘制逻辑

首先是初始化,我们这里设置了3种粒子,因此间隔角度是120度,而我们每次增加三种,防止出现混乱的问题。
  1.    final float rotateDegree = (float) Math.toRadians(120f); //间隔角度
  2.     if (circleList.size() < maxCircleNum) {
  3.     //每次增加三种
  4.         circleList.add(new Circle(Color.RED, (int) maxRadius, maxCircleRadius, 0 * rotateDegree));
  5.         circleList.add(new Circle(Color.GREEN, (int) maxRadius, maxCircleRadius, 1 * rotateDegree));
  6.         circleList.add(new Circle(Color.CYAN, (int) maxRadius, maxCircleRadius, 2 * rotateDegree));
  7.     }
复制代码
下面是每个粒子的绘制逻辑
  1. for (int i = 0; i < circleList.size(); i++) {
  2.     Circle circle = circleList.get(i);
  3.     circle.draw(canvas, mPaint); //绘制方法
  4. }
复制代码
更新粒子

下面有个重要的逻辑,其实前面也提到过,就是重置跑出边界的粒子
  1. for (int i = 0; i < circleList.size(); i++) {
  2.     Circle circle = circleList.get(i);
  3.     if(!circle.update(16)){
  4.         circle.reset(); //如果不能更新,则进行重置
  5.     }
  6. }
  7. postInvalidate(); //刷新绘制逻辑
复制代码
以上就是整体核心逻辑

效果调节

我们开头的两种效果其实是同一个View实现的,这其中一个重要的点就是速度调整,文章开头是调整出的两种效果,当然染还可以调整出其他效果 第一种
  1. this.rotate = 0.2f;
  2. this.speed = 0.2f; //外扩效果
复制代码
3.webp

第二种
  1. this.rotate = 0.35f;  //触角效果
  2. this.speed = 0.2f;
复制代码
4.webp

第三种
  1. this.rotate = 0.8f;
  2. this.speed = 0.1f;
复制代码
5.webp

当然,还有更多,篇幅原因就不深入了。

总结

本篇到这里就结束了,其实我们的核心代码并不多,但是简单的逻辑就能衍生出很多动画效果。其实,学习粒子动画是非常有意思的事,很多时候,你在实现某些效果的途中,就能突然开发出一种新的动画效果。

本篇代码

下面是本篇内容的完整逻辑,基本就在100行左右。
  1. public class CircleParticleView extends View {
  2.     private TextPaint mPaint;
  3.     private DisplayMetrics mDM;

  4.     public CircleParticleView(Context context) {
  5.         this(context, null);
  6.     }
  7.     public CircleParticleView(Context context, AttributeSet attrs) {
  8.         super(context, attrs);
  9.         mDM = getResources().getDisplayMetrics();
  10.         initPaint();
  11.     }

  12.     private void initPaint() {
  13.         //否则提供给外部纹理绘制
  14.         mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
  15.         mPaint.setAntiAlias(true);
  16.         mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  17.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  18.         PaintCompat.setBlendMode(mPaint, BlendModeCompat.PLUS);

  19.     }

  20.     @Override
  21.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  22.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  23.         int widthSize = MeasureSpec.getSize(widthMeasureSpec);

  24.         if (widthMode != MeasureSpec.EXACTLY) {
  25.             widthSize = mDM.widthPixels / 2;
  26.         }
  27.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  28.         int heightSize = MeasureSpec.getSize(heightMeasureSpec);

  29.         if (heightMode != MeasureSpec.EXACTLY) {
  30.             heightSize = widthSize / 2;
  31.         }
  32.         setMeasuredDimension(widthSize, heightSize);

  33.     }

  34.     int maxCircleRadius = 20;
  35.     List<Circle> circleList = new ArrayList<>();
  36.     int maxCircleNum = 300;
  37.     long time = 0;

  38.     @Override
  39.     protected void onDraw(Canvas canvas) {
  40.         super.onDraw(canvas);

  41.         int width = getWidth();
  42.         int height = getHeight();
  43.         float maxRadius = Math.min(width, height) / 2f;


  44.         int save = canvas.save();
  45.         canvas.translate(width / 2f, height / 2f);

  46.         final float rotateDegree = (float) Math.toRadians(120f);
  47.         if (circleList.size() < maxCircleNum) {
  48.             circleList.add(new Circle(Color.RED, (int) maxRadius, maxCircleRadius, 0 * rotateDegree));
  49.             circleList.add(new Circle(Color.GREEN, (int) maxRadius, maxCircleRadius, 1 * rotateDegree));
  50.             circleList.add(new Circle(Color.CYAN, (int) maxRadius, maxCircleRadius, 2 * rotateDegree));
  51.         }

  52.         mPaint.setStyle(Paint.Style.FILL);
  53.         for (int i = 0; i < circleList.size(); i++) {
  54.             Circle circle = circleList.get(i);
  55.             circle.draw(canvas, mPaint);
  56.         }
  57.         canvas.restoreToCount(save);

  58.         for (int i = 0; i < circleList.size(); i++) {
  59.             Circle circle = circleList.get(i);
  60.             if (!circle.update(16)) {
  61.                 circle.reset();
  62.             }
  63.         }

  64.         postInvalidate();
  65.         time += 16;
  66.     }

  67.     static class Circle {
  68.         int maxLength;  //最大运行距离
  69.         float speed; //外扩速度
  70.         float rotate; // 角速度
  71.         private float degree; //起始角度
  72.         private int y; //y坐标
  73.         private int x; //x坐标
  74.         private int color; //颜色
  75.         private float radius; //小圆半径
  76.         private float drawRadius; //绘制时的小圆半径

  77.         public Circle(int color, int maxLength, float radius, float degree) {
  78.             this.color = color;
  79.             this.radius = radius;
  80.             this.maxLength = maxLength;
  81.             this.degree = degree;
  82.             this.x = (int) (radius * Math.cos(degree));
  83.             this.y = (int) (radius * Math.sin(degree));
  84.             this.rotate = 0.35f;  //触角效果
  85.             this.speed = 0.2f;
  86.         }


  87.         public boolean update(long timeline) {
  88.             float length = (float) Math.hypot(x, y);
  89.             float center = length + this.speed * timeline; //距离增加
  90.             float ratio = center / maxLength;

  91.             this.drawRadius = (1f - ratio) * radius;

  92.             double degree = Math.atan2(y, x) + rotate;  //角度增加

  93.             this.x = (int) (center * Math.cos(degree));
  94.             this.y = (int) (center * Math.sin(degree));

  95.             if (drawRadius <= 0) {
  96.                 return false;
  97.             }
  98.             return true;
  99.         }

  100.         public void draw(Canvas canvas, TextPaint paint) {
  101.             paint.setColor(color);
  102.             canvas.drawCircle(x, y, drawRadius, paint);
  103.         }
  104.         public void reset() {
  105.             this.x = (int) (radius * Math.cos(degree));
  106.             this.y = (int) (radius * Math.sin(degree));
  107.         }
  108.     }

  109. }
复制代码
以上就是Android实现粒子中心扩散动画效果的详细内容,更多关于Android粒子中心扩散的资料请关注晓枫资讯其它相关文章!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
15
积分
10
注册时间
2022-12-27
最后登录
2022-12-27

发表于 2025-1-23 12:48:17 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
18
积分
16
注册时间
2022-12-27
最后登录
2022-12-27

发表于 2025-2-14 09:31:35 | 显示全部楼层
感谢楼主,顶。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-26
最后登录
2022-12-26

发表于 2025-2-17 00:47:15 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
18
积分
16
注册时间
2022-12-28
最后登录
2022-12-28

发表于 2025-4-11 06:55:55 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼
4楼
5楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表