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

 找回密码
 立即注册
缓存时间16 现在时间16 缓存数据 “我不是一个含着金汤匙生的小孩, 我的家人,一直用他们自己的方式, 又普通又隆重地爱我” —张艺兴 ​​

“我不是一个含着金汤匙生的小孩, 我的家人,一直用他们自己的方式, 又普通又隆重地爱我” —张艺兴 ​​ -- 外婆

查看: 1772|回复: 2

Android基于绘制缓冲实现烟花效果

[复制链接]

  离线 

TA的专栏

  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:5
  • 最近打卡:2024-05-18 14:02:39
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
38
主题
28
精华
0
金钱
111
积分
70
注册时间
2023-10-4
最后登录
2025-3-10

发表于 2024-9-11 13:44:32 | 显示全部楼层 |阅读模式
目录


  • 前言
  • 新方案

    • 基于数学和Paint线宽渐变
    • 基于绘制缓冲
    • 最终方案

  • 详细设计

    • 实现烟花逻辑

      • 定义FireExploreView
      • 定义粒子
      • 管理粒子
      • 初始化粒子
      • 粒子绘制
      • 缓冲复用
      • Blend效果增强
      • 多线程绘制


  • 新问题
  • 性能优化
  • 总结

前言

三月以前,我也写过《Android 烟花效果》,这篇我相当于做了个基础框架,在此基础上扩展和填充,就能扩展出很多效果。不过,当时,我在这篇文章中着重强调了一件事
  1. 重点:构建闭合空间
复制代码
之所以强调这件事的原因是,只有闭合空间的图形才能填充颜色、图片纹理。我们知道,Canvas 绘制方法仅仅只有圆、弧、矩形、圆角矩形是可以闭合的,除此之外就是Path了。
想象一下,如果让你画一个三角形并填充上颜色,你可能的方法只有通过裁剪Path或者使用Path绘制才行,而Path也有性能问题。
另外,闭合空间的填充也是件不容易的事。
所以,那篇文章中的烟花效果,本质上还不够完美,因为一些特殊的填充效果还是很难实现。

新方案

1.webp

目前我觉得可行的方案有两种

基于数学和Paint线宽渐变
  1. 如:贝塞尔曲线函数 + strokeWidth渐渐增大 + Color 变化
复制代码
这种方式是利用贝塞尔曲线计算出路径(不用Path,根据数学公式描绘),然后再规定的时间内让Paint的strokeWidth随着贝塞尔曲线 * time的偏移而增大,就能绘制出效果不错的的烟花条。

基于绘制缓冲

首先,要知道什么是缓冲,缓冲其实就是通常意义上存储数据的对象,比如byte数组、ByteBuffer等,但如果再聚焦Android 平台,我们还有FBO、VBO等。当然,最容易被忽略的是Bitmap,Bitmap 其实也是FBO的一种,不过这里我称之为“可视化缓冲”。
如果追踪的具体的对象上,除了Bitmap之外,Layer也是缓冲。
为什么使用缓冲可以优化烟花效果呢?
我们先了解下缓冲的特性:

  • 占用空间较大,狭义上来说,这种数据不仅仅占用空间大,而且(虚拟)内存需要连续
  • 空间可复用性强,如享元模式的ByteBuffer、alpha离屏渲染buffer、inBitmap等
  • 会产生脏数据,比如上一次buffer中的数据,如果没有清理的话依然会保存
  • 数据可复用性强,脏数据并不一定“脏”,有时还能复用
我们最终利用的还是空间可复用性和数据可复用性,如果我们以每次都在上次的数据中绘制,那么,意味着可以绘制出更多效果,间接解决了闭合空间填充问题。
那么,本篇我们选哪种呢?

最终方案

本篇,我们就选择基于缓冲的方案了,因为总的来说,第一种方式可能需要很多次的绘制,相当考验CPU。而使用绘制缓冲的的话,我们还可以复用上次的数据,这就相当于将上一次的绘制画面保留,然后再一次绘制时,在之前的基础上进一步完善,这种显然是利用“空间换取时间”的做法。

详细设计

本篇使用了绘制缓冲,原则上使用Bitmap是可以的,但是在使用的过程中发现,Bitmap在xformode绘制时性能还是很差,显然提升流畅度是必要原则。那么,你可能想到利用线程异步绘制,是的,我也打算这么做,但是想到使用线程渲染,那为什么不使用TextureView、SurfaceView或者GLSurfaceView呢?于是,我就没有再使用Bitmap的想法了。
但是,基于做以往的经验,我选了个兼容性最好性能最差的TextureView,其实我这里本打算选GLSurfaceView的,因为其性能和兼容性都是居中水平,不过涉及到顶点、纹理的一套东西,打算后续在音视频专栏写这类文章,因此本篇就选TexureView了。
  1. 简单说下SurfaceView的问题,性能最好,但其不适合在滑动的页面调用,因为有些设备会出现画面漂移和缩放的问题,另外不支持clipchildren等,理论上也是适合本篇的,但是如果app回到后台,其Surface会自动销毁,因此,控制线程的逻辑就会有些复杂。
复制代码
在这里我们看下TextureView源码,其创建的SurfaceTexture并不是单缓冲模式,但是又有设置缓冲bufferSize大小的操作,此外TextLayer负责提供缓冲,因此,这里至少是双缓冲。
  1.     mLayer = mAttachInfo.mThreadedRenderer.createTextureLayer();
  2.     boolean createNewSurface = (mSurface == null);
  3.     if (createNewSurface) {
  4.         // Create a new SurfaceTexture for the layer.
  5.         mSurface = new SurfaceTexture(false); //非单缓冲
  6.         nCreateNativeWindow(mSurface);
  7.     }
  8.     mLayer.setSurfaceTexture(mSurface);
  9.     mSurface.setDefaultBufferSize(getWidth(), getHeight());
  10.     mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);

  11.     if (mListener != null && createNewSurface) {
  12.         mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
  13.     }
  14.     mLayer.setLayerPaint(mLayerPaint);
  15. }
复制代码
下面是我们的详细流程。

实现烟花逻辑

下面是我们本篇的实现流程。

定义FireExploreView

我们本篇基于TextureView实现绘制逻辑,而TextureView必须要开启硬件加速,其次我们要实现TextureView.SurfaceTextureListener,用于监听SurfaceTexture的创建和销毁。理论上,TextureView的SurfaceTexture可以复用的,其次,如果onSurfaceTextureDestroyed返回false,那么SurfaceTexture的销毁是由你自己控制的,TextureView不会主动销毁。
  1. @Override
  2. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

  3.     return false;
  4. }
复制代码
另外,我们要知道,默认情况下TextureView使用的是TextureLayer,绘制完成之后,需要在RenderThread上使用gl去合成,这也是性能较差的主要原因。尤其是低配设备,使用TextureView也做不到性能优化,最终还是得使用SurfaceView或者GLTextureView或者GLSurfaceView,当然我比较推荐GL系列,主要是离屏渲染可以避免MediaCodec切换Surface引发黑屏和卡住的问题。
当然,这里我们肯定也要使用到线程和Surface了,相关代码如下
  1. @Override
  2. public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
  3.     drawThread = new Thread(this);
  4.     this.surfaceTexture = surfaceTexture;
  5.     this.surface = new Surface(this.surfaceTexture);
  6.     this.isRunning = true;
  7.     this.drawThread.start();
  8. }

  9. @Override
  10. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  11.     isRunning = false;
  12.     if (drawThread != null) {
  13.         try {
  14.             drawThread.interrupt();
  15.         }catch (Throwable e){
  16.             e.printStackTrace();
  17.         }
  18.     }
  19.     drawThread = null;
  20.     //不让TextureView 销毁SurfaceTexture,这里返回false
  21.     return false;
  22. }
复制代码
定义粒子

无论任何时候,不要把粒子不当对象,一些开发者对粒子对象嗤之以鼻,这显然是不对的,不受管理的粒子凭什么听你的指挥。
当然,任何粒子的运动需要符合运动学方程,而二维平面的运动是可以拆分为X轴和Y轴单方向的运动的。
  1. static final float gravity = 0.0f;
  2. static final float fraction = 0.88f;
  3. static final float speed = 50f; //最大速度

  4.    
  5. static class Particle {
  6.     private float opacity;  //透明度
  7.     private float dy; // y 轴速度
  8.     private float dx; // x 轴速度
  9.     private int color; //此颜色
  10.     private float radius; //半径
  11.     private float y; // y坐标
  12.     private float x; // x坐标

  13.     Particle(float x, float y, float r, int color, float speedX, float speedY) {
  14.         this.x = x;
  15.         this.y = y;
  16.         this.radius = r;
  17.         this.color = color;
  18.         this.dx = speedX;
  19.         this.dy = speedY;
  20.         this.opacity = 1f;
  21.     }

  22.     void draw(Canvas canvas, Paint paint) {
  23.         int save = canvas.save();
  24.         paint.setAlpha((int) (this.opacity * 255));
  25.         paint.setColor(this.color);
  26.         canvas.drawCircle(this.x, this.y, this.radius, paint);
  27.         canvas.restoreToCount(save);
  28.     }

  29.     void update() {
  30.         this.dy += gravity;
  31.         //加上重力因子,那么就会出现粒子重力现象,这里我们不使用时间了,这样简单点

  32.         this.dx *= fraction;  // fraction 是小于1的,用于降低速度
  33.         this.dy *= fraction;  // fraction 是小于1的,用于降低速度

  34.         this.x += this.dx;
  35.         this.y += this.dy;

  36.         this.opacity -= 0.03; //透明度递减
  37.     }
  38. }
复制代码
上面是粒子以及更新方法、绘制逻辑。

管理粒子

我们使用List管理粒子
  1. static final int maxParticleCount = 300;
  2. List<Particle> particles = new ArrayList<>(maxParticleCount);
复制代码
初始化粒子

粒子的初始化是非常重要的,初始化位置的正确与否会影响粒子的整体效果,显然,这里我们需要注意。
  1. float angleIncrement = (float) ((Math.PI * 2) / maxParticleCount); //平分 360度
  2. float[] hsl = new float[3];

  3. for (int i = 0; i < maxParticleCount; i++) {
  4.     hsl[0] = (float) (Math.random() * 360);
  5.     hsl[1] = 0.5f;
  6.     hsl[2] = 0.5f;
  7.     int hslToColor = HSLToColor(hsl);

  8.     Particle p = new Particle(x, y,
  9.             2.5f,
  10.             hslToColor,
  11.             (float) (Math.cos(angleIncrement * i) * Math.random() * speed),
  12.             (float) (Math.sin(angleIncrement * i) * Math.random() * speed)
  13.     );
  14.     particles.add(p);
  15. }
复制代码
不过,在这里我们还需要注意的是,这里我们使用HLS,这是一种色彩空间,和RGB不一样的是,他有Hue(色调)、饱和度、亮度为基准,因此,有利于亮色的表示,因此适合获取强调亮度的色彩。
与rgb的转换逻辑如下
  1. public static int HSLToColor(@NonNull float[] hsl) {
  2.     final float h = hsl[0];
  3.     final float s = hsl[1];
  4.     final float l = hsl[2];

  5.     final float c = (1f - Math.abs(2 * l - 1f)) * s;
  6.     final float m = l - 0.5f * c;
  7.     final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

  8.     final int hueSegment = (int) h / 60;

  9.     int r = 0, g = 0, b = 0;

  10.     switch (hueSegment) {
  11.         case 0:
  12.             r = Math.round(255 * (c + m));
  13.             g = Math.round(255 * (x + m));
  14.             b = Math.round(255 * m);
  15.             break;
  16.         case 1:
  17.             r = Math.round(255 * (x + m));
  18.             g = Math.round(255 * (c + m));
  19.             b = Math.round(255 * m);
  20.             break;
  21.         case 2:
  22.             r = Math.round(255 * m);
  23.             g = Math.round(255 * (c + m));
  24.             b = Math.round(255 * (x + m));
  25.             break;
  26.         case 3:
  27.             r = Math.round(255 * m);
  28.             g = Math.round(255 * (x + m));
  29.             b = Math.round(255 * (c + m));
  30.             break;
  31.         case 4:
  32.             r = Math.round(255 * (x + m));
  33.             g = Math.round(255 * m);
  34.             b = Math.round(255 * (c + m));
  35.             break;
  36.         case 5:
  37.         case 6:
  38.             r = Math.round(255 * (c + m));
  39.             g = Math.round(255 * m);
  40.             b = Math.round(255 * (x + m));
  41.             break;
  42.     }

  43.     r = constrain(r, 0, 255);
  44.     g = constrain(g, 0, 255);
  45.     b = constrain(b, 0, 255);

  46.     return Color.rgb(r, g, b);
  47. }
  48. private static int constrain(int amount, int low, int high) {
  49.     return amount < low ? low : Math.min(amount, high);
  50. }
复制代码
粒子绘制

绘制当然简单了,方法实现不是很复杂,调用如下逻辑即可,当然,opacity<=0 的粒子我们并没有移除,原因是因为remove 时, 可能引发ArrayList内存重整,这个是相当消耗性能的,因此,还不如遍历效率高。
  1. protected void drawParticles(Canvas canvas) {
  2.     canvas.drawColor(0x10000000); //为了让烟花减弱效果,每次加深绘制
  3.     for (int i = 0; i < particles.size(); i++) {
  4.         Particle particle = particles.get(i);
  5.         if (particle.opacity > 0) {
  6.             particle.draw(canvas, mPaint);
  7.             particle.update();
  8.         }
  9.     }
  10. }
复制代码
缓冲复用

那么,以上就是完整的绘制逻辑了,至于Surface调用逻辑呢,其实也很简单。
不过这里要注意的是,只有接受到command=true的时候,我们才清理画布,不然,我们要保留缓冲区中的数据。我们知道,一般View在onDraw的时候,RenderNode给你的Canvas都是清理过的,而这里,我们每次通过lockCanvas拿到的Canvas是带有上次缓冲数据的。
  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  2.     canvas = surface.lockHardwareCanvas();
  3. } else {
  4.     canvas = surface.lockCanvas(null);
  5. }

  6. if(isCommand){

  7.     canvas.drawColor(0x99000000, PorterDuff.Mode.CLEAR); //清理画布
  8.     explode(getWidth() / 2f, getHeight() / 2f);  //粒子初始化
  9.     isCommand = false;
  10. }
  11. //绘制粒子
  12. drawParticles(canvas);

  13. surface.unlockCanvasAndPost(canvas);
复制代码
2.webp

显然,我们能得到两条经验:

  • lockCanvas获取到的Canvas是带有上次绘制数据的
  • 利用缓冲绘制不仅强调结果,而且还强调过程,一般的Canvas绘制仅仅强调结果

Blend效果增强

实际上面的效果还有点差,就是尖端亮度太低,为此,我们可以使用Blend进行增强,我们设置BlendMode为PLUS,另外上面我们的重力是0,现在我们调整一下gravity=0.25f。
  1. PaintCompat.setBlendMode(mPaint, BlendModeCompat.PLUS);
复制代码
效果
3.webp


多线程绘制

总的来说,TextureView可以在一些情况下显著提升性能,当然,前提是你的主线程流畅。
这里的逻辑就是TextureView的用法了,我们就不继续深入了,本篇末尾提供源码。

新问题

评论区有同学反馈,在真机上很卡,我试了一下,发现不是卡,而是TextureView 不是单缓冲,两次缓冲在没有CLEAR时会有交替闪烁问题。
因此,为了优化闪烁问题,我把可视化缓冲Bitmap重新加进来,使用之后在上是没有问题的,但是由于Android 6.0 之前的系统无法使用lockHardwareCanvas,卡顿是比较明显的。
为啥模拟器表现比较好,可能刷新率比较低。

性能优化

由于使用Bitmap作为缓冲,性能有所降低,我们这里进行如下优化

  • 减少绘制区域大小
  • 移除Surface 清理 canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
  • Android 6.0+版本使用硬件Canvas
缩小绘制区域收益明显,后续考虑先缩小后绘制,再利用Matrix放大。

总结

以上是本篇的内容,也是我们要掌握的技巧,很多时候,我们对Canvas的绘制,过于强调结果,结果设计了很多复杂的算法,其实,基于过程的绘制显然更加简单和优化。
到这里本篇就结束了,希望本篇对你有所帮助。
源码
  1. public class FireExploreView extends TextureView implements TextureView.SurfaceTextureListener, Runnable {
  2.     private TextPaint mPaint;
  3.     private SurfaceTexture surfaceTexture;
  4.     private Surface surface;
  5.     private BitmapCanvas mBitmapCanvas;
  6.     private boolean updateOnSizeChanged = false;
  7.     private volatile boolean isRunning = false;
  8.     private final Object lockSurface = new Object();

  9.     {
  10.         initPaint();
  11.     }
  12.     public FireExploreView(Context context) {
  13.         this(context, null);
  14.     }
  15.     public FireExploreView(Context context, @Nullable AttributeSet attrs) {
  16.         super(context, attrs);
  17.         setSurfaceTextureListener(this);
  18.     }

  19.     private void initPaint() {
  20.         //否则提供给外部纹理绘制
  21.         mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
  22.         mPaint.setAntiAlias(true);
  23.         mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  24.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  25.         mPaint.setStyle(Paint.Style.FILL);
  26.         PaintCompat.setBlendMode(mPaint, BlendModeCompat.PLUS);

  27.     }

  28.     static final float gravity = 0.21f;
  29.     static final float fraction = 0.88f;
  30.     static final int maxParticleCount = 300;
  31.     List<Particle> particles = new ArrayList<>(maxParticleCount);
  32.     float[] hsl = new float[3];

  33.     volatile boolean isCommand = false;
  34.     static final float speed = 60f;
  35.     Thread drawThread = null;

  36.     public void startExplore() {
  37.         isCommand = true;
  38.     }

  39.     //初始化粒子
  40.     void explode(float x, float y) {
  41.         float angleIncrement = (float) ((Math.PI * 2) / maxParticleCount);
  42.         for (int i = 0; i < maxParticleCount; i++) {
  43.             hsl[0] = (float) (Math.random() * 360);
  44.             hsl[1] = 0.5f;
  45.             hsl[2] = 0.5f;
  46.             int hslToColor = HSLToColor(hsl);

  47.             Particle p = null;
  48.             if (particles.size() > i) {
  49.                 p = particles.get(i);
  50.             }

  51.             if (p == null) {
  52.                 p = new Particle();
  53.                 particles.add(p);
  54.             }
  55.             p.init(x, y,
  56.                     4f,
  57.                     hslToColor,
  58.                     (float) (Math.cos(angleIncrement * i) * Math.random() * speed),
  59.                     (float) (Math.sin(angleIncrement * i) * Math.random() * speed)
  60.             );
  61.         }
  62.     }


  63.     protected void drawParticles(Canvas canvas) {
  64.         canvas.drawColor(0x10000000);
  65.         for (int i = 0; i < particles.size(); i++) {
  66.             Particle particle = particles.get(i);
  67.             if (particle.opacity > 0) {
  68.                 particle.draw(canvas, mPaint);
  69.                 particle.update();
  70.             }
  71.         }
  72.     }


  73.     static class Particle {
  74.         private float opacity;
  75.         private float dy;
  76.         private float dx;
  77.         private int color;
  78.         private float radius;
  79.         private float y;
  80.         private float x;

  81.         public void init(float x, float y, float r, int color, float speedX, float speedY) {
  82.             this.x = x;
  83.             this.y = y;
  84.             this.radius = r;
  85.             this.color = color;
  86.             this.dx = speedX;
  87.             this.dy = speedY;
  88.             this.opacity = 1f;
  89.         }
  90.         void draw(Canvas canvas, Paint paint) {
  91.             int save = canvas.save();
  92.             paint.setColor(argb((int) (this.opacity * 255),Color.red(this.color),Color.green(this.color),Color.blue(this.color)));
  93.             canvas.drawCircle(this.x, this.y, this.radius, paint);
  94.             canvas.restoreToCount(save);
  95.         }

  96.         void update() {
  97.             this.dy += gravity;

  98.             this.dx *= fraction;
  99.             this.dy *= fraction;

  100.             this.x += this.dx;
  101.             this.y += this.dy;

  102.             this.opacity -= 0.02;
  103.         }


  104.     }


  105.     Matrix matrix = new Matrix();
  106.     @Override
  107.     public void run() {
  108.         while (true) {
  109.             synchronized (this) {
  110.                 try {
  111.                     this.wait(16);
  112.                 } catch (InterruptedException e) {
  113.                     e.printStackTrace();
  114.                 }
  115.             }
  116.             if (!isRunning || Thread.currentThread().isInterrupted()) {
  117.                 synchronized (lockSurface) {
  118.                     if (surface != null && surface.isValid()) {
  119.                         surface.release();
  120.                     }
  121.                     surface = null;
  122.                 }
  123.                 break;
  124.             }

  125.             Canvas canvas = null;
  126.             synchronized (lockSurface) {
  127.                 if(mBitmapCanvas == null || updateOnSizeChanged) {
  128.                     updateOnSizeChanged = false;
  129.                     mBitmapCanvas = createBitmapCanvas(getWidth(),getHeight());
  130.                 }

  131.                 if(isCommand){
  132.                     mBitmapCanvas.bitmap.eraseColor(0x00000000);
  133.                     explode(mBitmapCanvas.getWidth() / 2f, mBitmapCanvas.getHeight() / 2f);
  134.                     isCommand = false;
  135.                 }
  136.                 //这里其实目前没有加锁的必要,考虑到如果有其他SurfaceTexture相关操作会加锁,这里先加锁吧
  137.                 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
  138.                      canvas = surface.lockHardwareCanvas();
  139.                 }else {
  140.                     canvas = surface.lockCanvas(null);
  141.                 }
  142.                 Bitmap bitmap = mBitmapCanvas.getBitmap();
  143.                 drawParticles(mBitmapCanvas);
  144.                 matrix.reset();
  145.                 matrix.setTranslate((getWidth() - bitmap.getWidth()) / 2f, (getHeight() - bitmap.getHeight()) / 2f);
  146.                 canvas.drawBitmap(mBitmapCanvas.getBitmap(), matrix, null);
  147.                 surface.unlockCanvasAndPost(canvas);
  148.             }
  149.         }

  150.     }

  151.     private BitmapCanvas createBitmapCanvas(int width,int height){
  152.         if(mBitmapCanvas != null){
  153.             mBitmapCanvas.recycle();
  154.         }
  155.         int size = Math.max(Math.min(width,height),1);
  156.         return new BitmapCanvas(Bitmap.createBitmap(size,size, Bitmap.Config.ARGB_8888));
  157.     }

  158.     static class BitmapCanvas extends Canvas{
  159.         Bitmap bitmap;

  160.         public BitmapCanvas(Bitmap bitmap) {
  161.             super(bitmap);
  162.             this.bitmap = bitmap;
  163.         }

  164.         public Bitmap getBitmap() {
  165.             return bitmap;
  166.         }

  167.         public void recycle() {
  168.             if(bitmap == null || bitmap.isRecycled()){
  169.                 return;
  170.             }
  171.             bitmap.recycle();
  172.         }
  173.     }

  174.     @Override
  175.     public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
  176.         this.drawThread = new Thread(this);
  177.         this.surfaceTexture = surfaceTexture;
  178.         this.surface = new Surface(this.surfaceTexture);
  179.         this.isRunning = true;
  180.         this.drawThread.start();
  181.     }

  182.     @Override
  183.     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  184.         updateOnSizeChanged = true;
  185.     }

  186.     @Override
  187.     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  188.         isRunning = false;
  189.         if (drawThread != null) {
  190.             try {
  191.                 drawThread.interrupt();
  192.             }catch (Throwable e){
  193.                 e.printStackTrace();
  194.             }
  195.         }
  196.         drawThread = null;
  197.         return false;
  198.     }

  199.     @Override
  200.     public void onSurfaceTextureUpdated(SurfaceTexture surface) {

  201.     }


  202.     @ColorInt
  203.     public static int HSLToColor(@NonNull float[] hsl) {
  204.         final float h = hsl[0];
  205.         final float s = hsl[1];
  206.         final float l = hsl[2];

  207.         final float c = (1f - Math.abs(2 * l - 1f)) * s;
  208.         final float m = l - 0.5f * c;
  209.         final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

  210.         final int hueSegment = (int) h / 60;

  211.         int r = 0, g = 0, b = 0;

  212.         switch (hueSegment) {
  213.             case 0:
  214.                 r = Math.round(255 * (c + m));
  215.                 g = Math.round(255 * (x + m));
  216.                 b = Math.round(255 * m);
  217.                 break;
  218.             case 1:
  219.                 r = Math.round(255 * (x + m));
  220.                 g = Math.round(255 * (c + m));
  221.                 b = Math.round(255 * m);
  222.                 break;
  223.             case 2:
  224.                 r = Math.round(255 * m);
  225.                 g = Math.round(255 * (c + m));
  226.                 b = Math.round(255 * (x + m));
  227.                 break;
  228.             case 3:
  229.                 r = Math.round(255 * m);
  230.                 g = Math.round(255 * (x + m));
  231.                 b = Math.round(255 * (c + m));
  232.                 break;
  233.             case 4:
  234.                 r = Math.round(255 * (x + m));
  235.                 g = Math.round(255 * m);
  236.                 b = Math.round(255 * (c + m));
  237.                 break;
  238.             case 5:
  239.             case 6:
  240.                 r = Math.round(255 * (c + m));
  241.                 g = Math.round(255 * m);
  242.                 b = Math.round(255 * (x + m));
  243.                 break;
  244.         }

  245.         r = constrain(r, 0, 255);
  246.         g = constrain(g, 0, 255);
  247.         b = constrain(b, 0, 255);

  248.         return Color.rgb(r, g, b);
  249.     }
  250.     private static int constrain(int amount, int low, int high) {
  251.         return amount < low ? low : Math.min(amount, high);
  252.     }

  253.     public static int argb(
  254.             @IntRange(from = 0, to = 255) int alpha,
  255.             @IntRange(from = 0, to = 255) int red,
  256.             @IntRange(from = 0, to = 255) int green,
  257.             @IntRange(from = 0, to = 255) int blue) {
  258.         return (alpha << 24) | (red << 16) | (green << 8) | blue;
  259.     }
  260.     public void release(){
  261.         synchronized (lockSurface) {
  262.             isRunning = false;
  263.             updateOnSizeChanged = false;
  264.             if (surface != null && surface.isValid()) {
  265.                 surface.release();
  266.             }
  267.             surface = null;
  268.         }
  269.     }
  270. }
复制代码
以上就是Android基于绘制缓冲实现烟花效果的详细内容,更多关于Android烟花效果的资料请关注晓枫资讯其它相关文章!

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

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

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

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

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

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

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

本版积分规则

1楼
2楼
3楼

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

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

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

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

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

Powered by Discuz! X3.5

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