Android 顯示view的粒子爆炸/綻放效果

照例先上圖

Bloom
Bloom
Bloom

這個(gè)庫(kù)做了什么?

它可以以粒子的形式顯示任何view的粒子動(dòng)畫(huà)效果,你可以下載DEMO來(lái)查看效果

功能

  • 支持任何view
  • 可靈活配置粒子大小和形狀
  • 可靈活配置粒子動(dòng)畫(huà)

形狀

在該庫(kù)中,支持三種粒子形狀

圓形
Circle
矩形
Rect
星型
Star

代碼中,你首先需要設(shè)置形狀分發(fā)器

Bloom.with('activity')
    .setParticleRadius(15)
    .setShapeDistributor(new CircleShapeDistributor())
    //or setShapeDistributor(new RectShapeDistributor())
    //or setShapeDistributor(new StarShapeDistributor())
    .boom(view);
什么是形狀分發(fā)器?

ParticleShapeDistributor基礎(chǔ)類:

public abstract class ParticleShapeDistributor {
    public abstract ParticleShape getShape(BloomParticle particle);
}

允許你為每個(gè)粒子設(shè)置對(duì)應(yīng)的形狀

關(guān)于自定義形狀分發(fā)器和形狀

怎么設(shè)置一個(gè)自定義形狀分發(fā)器或者形狀?

自定義分發(fā)器

繼承類 ParticleShapeDistributor 然后給每個(gè)粒子返回 形狀 .

自定義形狀

我們來(lái)看一下 形狀的基類:

public abstract class ParticleShape {
    private float mRadius;
    private float mCenterX;
    private float mCenterY;
    private Path mPath;

    public ParticleShape(float centerX, float centerY, float radius){
        mCenterX = centerX;
        mCenterY = centerY;
        mRadius  = radius;
        mPath = new Path();
    }

    public float getRadius() {
        return mRadius;
    }

    public float getCenterX() {
        return mCenterX;
    }

    public float getCenterY() {
        return mCenterY;
    }
    
    public abstract void generateShape(Path path);

    public Path getShapePath(){
        return mPath;
    }
}

當(dāng)你需要實(shí)現(xiàn)自定義粒子形狀時(shí),繼承這個(gè)類,然后實(shí)現(xiàn)generateShape方法,你需要注意這里提供的參數(shù)是粒子的中心點(diǎn)坐標(biāo)(x,y),以及它的最大半徑


最后,讓我們實(shí)現(xiàn)三種形狀的隨機(jī)效果:

代碼:

Bloom.with(this)
    .setParticleRadius(5)
    .setShapeDistributor(new ParticleShapeDistributor() {
        @Override
        public ParticleShape getShape(BloomParticle particle) {
            switch (mRandom.nextInt(3)){
                case 0:
                    return new ParticleCircleShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
                case 1:
                    return new ParticleRectShape(2, 2, particle.getInitialX(), particle.getInitialY(), particle.getRadius());//設(shè)置圓角效果
                case 2:
                    return new ParticleStarShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
            }
            return new ParticleCircleShape(particle.getInitialX(), particle.getInitialY(), particle.getRadius());
        }
    })
    .setEffector(new BloomEffector.Builder()
                 .setDuration(800)
                 .setAnchor(view.getWidth() / 2, view.getHeight() / 2)
                 .build())
    .boom(view);

截圖:


Random

效果器

BloomEffector 是該庫(kù)最重要的類 , 您可以通過(guò)以下方式構(gòu)建效果器:

new BloomEffector.Builder()
    .setDuration(800)
    .setAnchor(view.getWidth() / 2, view.getHeight() / 2)
    .build()

我們來(lái)看看效果器可以使用的所有方法:

方法 描述
setDuration(long duration) 設(shè)置bloom效果動(dòng)畫(huà)的長(zhǎng)度(以毫秒為單位)
setInterpolator(TimeInterpolator interpolator) 設(shè)置bloom效果動(dòng)畫(huà)的插值器
setAnchor(float anchorX, float anchorY) 設(shè)置所有粒子的錨點(diǎn)
setSpeedRange(float minSpeed, float maxSpeed) 設(shè)置粒子的速度范圍
setScaleRange(float minScale, float maxScale) 設(shè)置粒子的縮放范圍
setSkewRange(float minSkew, float maxSkew) 設(shè)置粒子的傾斜范圍
setRotationSpeedRange(float minRotationSpeedRange, float maxRotationSpeedRange) 設(shè)置粒子的旋轉(zhuǎn)速度范圍
setAcceleration(float acceleration, int accelerationAngle) 設(shè)置粒子加速度
setAccelerationRange(float minAcceleration, float maxAcceleration, int minAccelerationAngel, int maxAccelerationAngel) 設(shè)置粒子加速度
setFadeOut(long startTime, TimeInterpolator interpolator) 設(shè)置所有粒子的淡出效果
setFadeOut(long startTime) 設(shè)置所有粒子的淡出效果
BloomEffector build() 建立一個(gè)效果器

功能點(diǎn)描述

錨點(diǎn)

錨點(diǎn)可以控制所有粒子的初始運(yùn)動(dòng)方向, 原理是所有粒子都與這個(gè)錨點(diǎn)坐標(biāo)(x,y)計(jì)算角度,以獲得粒子的下一個(gè)運(yùn)動(dòng)角度

例如將錨點(diǎn)設(shè)置為視圖中心點(diǎn)的坐標(biāo),則所有粒子將與中心點(diǎn)的坐標(biāo)計(jì)算角度,即所有粒子將相對(duì)于中心點(diǎn)偏移,最終粒子動(dòng)畫(huà)效果如下:

Anchor
速度

控制粒子的移動(dòng)速度

粒子的速度將從你設(shè)置的速度范圍中取一個(gè)隨機(jī)值

公式如下:

float randomSpeed = mRandom.nextFloat()*(mMaxSpeed-mMinSpeed) + mMinSpeed;
縮放

控制粒子的縮放值

粒子的所防止將從你設(shè)置的縮放范圍中獲取隨機(jī)值

公式如下:

float scale = mRandom.nextFloat() * (mMaxScale - mMinScale) + mMinScale
傾斜

控制粒子的傾斜

粒子的傾斜將從你設(shè)置的偏斜范圍中獲取隨機(jī)值

公式如下:

float skew = mRandom.nextFloat() * (mMaxSkew - mMinSkew) + mMinSkew;
旋轉(zhuǎn)

控制粒子的旋轉(zhuǎn)加速度,如果未設(shè)置此值,則粒子不會(huì)旋轉(zhuǎn)

粒子的旋轉(zhuǎn)動(dòng)畫(huà)將從你設(shè)置的旋轉(zhuǎn)速度范圍中獲取隨機(jī)值

公式如下:

float rotationSpeed = mRandom.nextFloat()*(mMaxRotationSpeed-mMinRotationSpeed) + mMinRotationSpeed;
加速度

控制粒子加速度和加速度方向,加速度方向由你設(shè)定的加速度角度決定,計(jì)算方法如下:

float angelInRadsAcc = (float) (accelerationAngle*Math.PI / 180f)

//x軸加速度
accelerationX = (float) (value * Math.cos(angleInRadsAcc));
//y軸加速度
accelerationY = (float) (value * Math.sin(angleInRadsAcc));

加速度以像素/平方毫秒為單位:

float finalTranslateX = accelerationX*milliSecond*milliSecond;
float finalTranslateY = accelerationY*milliSecond*milliSecond;
淡出

控制粒子的淡出效果,事實(shí)是控制粒子的alpha值, 淡出開(kāi)始時(shí)間可以是[0-duration]

監(jiān)聽(tīng)器

你可以通過(guò)以下方法設(shè)置監(jiān)聽(tīng)器:

Bloom.with(this)
    .setParticleRadius(5)
    .setBloomListener(new BloomListener() {
        @Override
        public void onBegin() {
            //動(dòng)畫(huà)開(kāi)始
        }

        @Override
        public void onEnd() {
        //動(dòng)畫(huà)結(jié)束
        }
    })
    .boom(view);

如果對(duì)你的開(kāi)發(fā)起到幫助,請(qǐng)給作者一個(gè)star
Github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容