Android動畫之煙花

boom.gif

思路

主要步驟就是上升和爆炸。都是用屬性動畫實現(xiàn)的。使用屬性動畫的時候厘贼,該屬性必須要有set和get方法。

 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(boomView, "flyFraction", 0, 1);
        objectAnimator.setDuration(700);

        objectAnimator.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(boomView, "boomFraction", 0, 1);
        objectAnimator1.setDuration(400);

        final AnimatorSet set = new AnimatorSet();
        set.playSequentially(objectAnimator, objectAnimator1);

        boomView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                set.start();

            }
        });

        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                //改變顏色
                color = Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255));
                boomView.setColor(color);
                DrawableCompat.setTint(imageView.getDrawable(),color);

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //循環(huán)播放
                set.start();
            }
        });

上升過程

上升過程用canvas畫一個矩形圣拄,從0-3/4高度嘴秸。

if(flyFraction!=1){
            int y = (int) ((1 - flyFraction * 3 / 4) * canvas.getHeight());
            mPaint.setColor(color);
            canvas.drawRect(canvas.getWidth() / 2 - 5, y, canvas.getWidth() / 2 + 5, y + 10, mPaint);
        }

爆炸過程

爆炸粒子的圍繞半徑逐漸變大。(使用極坐標可以很容易地在畫小圓點)

public class BoomUtil {

    private Paint mPaint;
    private int pointCount;//爆炸時的粒子數(shù)量
    private int radiusMax;//最大爆炸半徑
    private int pointRadius;//粒子半徑


    public BoomUtil(int pointCount, int radiusMax, int pointRadius) {
        this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.pointCount = pointCount;
        this.radiusMax = radiusMax;
        this.pointRadius = pointRadius;
    }

    public void drawBoom(Canvas canvas, int cx,int cy,float fraction,int color) {
        mPaint.setColor(color);
        mPaint.setAlpha((int) (255 * (1 - fraction)));
        int radius = (int) (radiusMax * fraction);
        for (int i = 0; i < pointCount; i++) {
            double angle = i * 2 * Math.PI / pointCount;
            float x = (float) Math.cos(angle) * radius+cx;
            float y = (float) Math.sin(angle) * radius+cy;
            canvas.drawCircle(x, y, pointRadius, mPaint);
        }
    }
}

BoomView

public class BoomView extends View {

    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float flyFraction = 0;
    private float boomFraction = 0;
    private BoomUtil boomUtil;
    private Random random;
    private int color;

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public BoomView(Context context) {
        super(context);
        initBoomUtil();
    }

    public BoomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initBoomUtil();
    }

    private void initBoomUtil() {
        boomUtil = new BoomUtil(12, 200, 12);

    }

    public float getBoomFraction() {
        return boomFraction;
    }

    public void setBoomFraction(float boomFraction) {
        this.boomFraction = boomFraction;
        invalidate();
    }

    public float getFlyFraction() {
        return flyFraction;
    }

    public void setFlyFraction(float flyFraction) {
        this.flyFraction = flyFraction;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(flyFraction!=1){
            int y = (int) ((1 - flyFraction * 3 / 4) * canvas.getHeight());
            mPaint.setColor(color);
            canvas.drawRect(canvas.getWidth() / 2 - 5, y, canvas.getWidth() / 2 + 5, y + 10, mPaint);
        }

        if (boomFraction != 0) {
            boomUtil.drawBoom(canvas, canvas.getWidth() / 2, canvas.getHeight() / 4, boomFraction, color);
        }
    }
}

總結(jié)

把一個復雜的過程分解為多個簡單的過程庇谆,思路瞬間清晰了岳掐。
github源碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市饭耳,隨后出現(xiàn)的幾起案子串述,更是在濱河造成了極大的恐慌,老刑警劉巖寞肖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纲酗,死亡現(xiàn)場離奇詭異衰腌,居然都是意外死亡,警方通過查閱死者的電腦和手機觅赊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門右蕊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人茉兰,你說我怎么就攤上這事尤泽。” “怎么了规脸?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵坯约,是天一觀的道長。 經(jīng)常有香客問我莫鸭,道長闹丐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任被因,我火速辦了婚禮卿拴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘梨与。我一直安慰自己堕花,他們只是感情好,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布粥鞋。 她就那樣靜靜地躺著缘挽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呻粹。 梳的紋絲不亂的頭發(fā)上壕曼,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機與錄音等浊,去河邊找鬼腮郊。 笑死,一個胖子當著我的面吹牛筹燕,可吹牛的內(nèi)容都是我干的轧飞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼庄萎,長吁一口氣:“原來是場噩夢啊……” “哼踪少!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起糠涛,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎兼犯,沒想到半個月后忍捡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體集漾,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年砸脊,在試婚紗的時候發(fā)現(xiàn)自己被綠了具篇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凌埂,死狀恐怖驱显,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瞳抓,我是刑警寧澤埃疫,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站孩哑,受9級特大地震影響栓霜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜横蜒,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一胳蛮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧丛晌,春花似錦仅炊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瓶竭,卻和暖如春督勺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斤贰。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工智哀, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荧恍。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓瓷叫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親送巡。 傳聞我的和親對象是個殘疾皇子摹菠,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容