Android在canvas中實現高效能的煙花/粒子特效

weixin_33806914發表於2018-12-27

新年到了,本文將展示通過自定義view繪製煙花效果的案例,同時介紹一種優化canvas繪製時的效能的方法.

效果展示

2772606-5ec77b12b30e1f8a.gif
1.gif

每點選一下螢幕會產生一枚煙花,煙花飛到最上空會炸裂成60~100個碎片,
同屏可能有上千個粒子在不停更新它的位置.

專案地址

github

程式碼分析

  1. 首先專案結構
└─fireworks
        ColorfullView.java -自定義的view
        Firework.java -對煙花建立模型
        MainActivity.java
        Particle.java -粒子
        PContext.java -工具類
        PVector.java  -向量類
  1. 使用GestureDetector處理使用者輸入,每點選一次在當前位置建立一枚煙花
        mGesture = new GestureDetectorCompat(getContext(), new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {
                fireworks.add(new Firework(e.getX(), e.getY()));
                ViewCompat.postInvalidateOnAnimation(ColorfullView.this);
                return true;
            }
        });
  1. FireWork類包含兩個屬性
    ArrayList<Particle> particles;    // 炸開後的粒子
    Particle firework;                // 炸開前的粒子
  1. Particle類具有隨機的顏色、大小、速度以接近真實
  2. 每繪製一幀會呼叫FireWork的run方法,該方法中會呼叫當前粒子的update和display方法
    void run(Canvas canvas, Paint paint) {
        if (firework != null) {//炸裂前
            firework.applyForce(gravity);
            firework.update();
            firework.display(canvas, paint);

            if (firework.explode()) {
                int fragments = (int) random(60, 100);//碎片個數
                for (int i = 0; i < fragments; i++) {
                    particles.add(new Particle(firework.location, hu));    // Add "num" amount of particles to the arraylist
                }
                firework = null;
                firstExplode = true;
            }
        } else {//炸裂後
            if (firstExplode) {//螢幕一閃
                firstExplode = false;
                canvas.drawColor(Color.HSVToColor(new float[]{hu, 0.6f, 0.6f}));
            }

            for (int i = particles.size() - 1; i >= 0; i--) {
                Particle p = particles.get(i);
                p.applyForce(gravity);
                p.update();
                p.display(canvas, paint);
                if (p.isDead()) {
                    particles.remove(i);
                }
            }
        }

    }

效能優化

這時候功能基本實現了,剩下的就是將每一個煙花繪製在canvas上,通常我們會這樣寫

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        for (int i = fireworks.size() - 1; i >= 0; i--) {
            Firework f = fireworks.get(i);
            f.run(canvas, mParticlePaint);
            if (f.done()) {
                fireworks.remove(i);
            }
        }
        //canvas.drawBitmap(canvasBitmap,0, 0, mBitmapPaint);
        if (!fireworks.isEmpty()) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

然而你會發現效能很糟糕,幀數隨著粒子數的增加直線下降直到個位數,優化如下

    @Override
    protected void onDraw(Canvas canvas) {
        if (canvasBitmap == null || canvasBitmap.isRecycled()) {
            canvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            bitmapCanvas = new Canvas(canvasBitmap);
            mBitmapShader = new BitmapShader(canvasBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mBitmapPaint.setShader(mBitmapShader);
            mBitmapPaint.setDither(false);
        }
        bitmapCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        for (int i = fireworks.size() - 1; i >= 0; i--) {
            Firework f = fireworks.get(i);
            f.run(bitmapCanvas, mParticlePaint);
            if (f.done()) {
                fireworks.remove(i);
            }
        }
        canvas.drawPaint(mBitmapPaint);
        if (!fireworks.isEmpty()) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

Thanks

some codes were from Daniel Shiffman

附贈

2772606-c19d6cef4326e5cc.gif
6.gif

相關文章