zhuguifei
2025-12-31 a5a64b0bdc4e90e265cbff767c09351ea82f113a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.shlb.comb.view;
 
 
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
 
import com.shlb.comb.R;
import com.shlb.comb.view.particleview.LineEvaluator;
import com.shlb.comb.view.particleview.Particle;
 
import java.util.ArrayList;
import java.util.Collection;
 
 
/**
 * 作者: 巴掌 on 16/8/27 11:29
 * Github: https://github.com/JeasonWong
 */
public class ParticleView extends View {
 
    private final int STATUS_MOTIONLESS = 0;
    private final int STATUS_PARTICLE_GATHER = 1;
    private final int STATUS_TEXT_MOVING = 2;
 
    private final int ROW_NUM = 10;
    private final int COLUMN_NUM = 10;
 
    private final int DEFAULT_MAX_TEXT_SIZE = sp2px(80);
    private final int DEFAULT_MIN_TEXT_SIZE = sp2px(30);
 
    public final int DEFAULT_TEXT_ANIM_TIME = 1000;
    public final int DEFAULT_SPREAD_ANIM_TIME = 300;
    public final int DEFAULT_HOST_TEXT_ANIM_TIME = 800;
 
    private Paint mHostTextPaint;
    private Paint mParticleTextPaint;
    private Paint mCirclePaint;
    private Paint mHostBgPaint;
    private int mWidth, mHeight;
 
    private Particle[][] mParticles = new Particle[ROW_NUM][COLUMN_NUM];
    private Particle[][] mMinParticles = new Particle[ROW_NUM][COLUMN_NUM];
 
    //背景色
    private int mBgColor;
    //粒子色
    private int mParticleColor;
    //默认粒子文案大小
    private int mParticleTextSize = DEFAULT_MIN_TEXT_SIZE;
 
    private int mStatus = STATUS_MOTIONLESS;
 
    private ParticleAnimListener mParticleAnimListener;
 
    //粒子文案
    private String mParticleText;
    //主文案
    private String mHostText;
    //扩散宽度
    private float mSpreadWidth;
    //Host文字展现宽度
    private float mHostRectWidth;
    //粒子文案的x坐标
    private float mParticleTextX;
    //Host文字的x坐标
    private float mHostTextX;
 
    //Text anim time in milliseconds
    private int mTextAnimTime;
    //Spread anim time in milliseconds
    private int mSpreadAnimTime;
    //HostText anim time in milliseconds
    private int mHostTextAnimTime;
 
    private PointF mStartMaxP, mEndMaxP;
    private PointF mStartMinP, mEndMinP;
 
    public ParticleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public ParticleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }
 
    private void initView(AttributeSet attrs) {
 
        TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.ParticleView);
        mHostText = null == typeArray.getString(R.styleable.ParticleView_pv_host_text) ? "" : typeArray.getString(R.styleable.ParticleView_pv_host_text);
        mParticleText = null == typeArray.getString(R.styleable.ParticleView_pv_particle_text) ? "" : typeArray.getString(R.styleable.ParticleView_pv_particle_text);
        mParticleTextSize = (int) typeArray.getDimension(R.styleable.ParticleView_pv_particle_text_size, DEFAULT_MIN_TEXT_SIZE);
        int hostTextSize = (int) typeArray.getDimension(R.styleable.ParticleView_pv_host_text_size, DEFAULT_MIN_TEXT_SIZE);
        mBgColor = typeArray.getColor(R.styleable.ParticleView_pv_background_color, 0xFF0867AB);
        mParticleColor = typeArray.getColor(R.styleable.ParticleView_pv_text_color, 0xFFCEF4FD);
        mTextAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_TEXT_ANIM_TIME);
        mSpreadAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_SPREAD_ANIM_TIME);
        mHostTextAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_HOST_TEXT_ANIM_TIME);
        typeArray.recycle();
 
        mHostTextPaint = new Paint();
        mHostTextPaint.setAntiAlias(true);
        mHostTextPaint.setTextSize(hostTextSize);
        mParticleTextPaint = new Paint();
        mParticleTextPaint.setAntiAlias(true);
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mHostBgPaint = new Paint();
        mHostBgPaint.setAntiAlias(true);
        mHostBgPaint.setTextSize(hostTextSize);
 
        mParticleTextPaint.setTextSize(mParticleTextSize);
        mCirclePaint.setTextSize(mParticleTextSize);
 
        mParticleTextPaint.setColor(mBgColor);
        mHostTextPaint.setColor(mBgColor);
        mCirclePaint.setColor(mParticleColor);
        mHostBgPaint.setColor(mParticleColor);
 
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
 
        mStartMinP = new PointF(mWidth / 2 - getTextWidth(mParticleText, mParticleTextPaint) / 2f - dip2px(4), mHeight / 2 + getTextHeight(mHostText, mHostTextPaint) / 2 - getTextHeight(mParticleText, mParticleTextPaint) / 0.7f);
        mEndMinP = new PointF(mWidth / 2 + getTextWidth(mParticleText, mParticleTextPaint) / 2f + dip2px(10), mHeight / 2 + getTextHeight(mHostText, mHostTextPaint) / 2);
 
        for (int i = 0; i < ROW_NUM; i++) {
            for (int j = 0; j < COLUMN_NUM; j++) {
                mMinParticles[i][j] = new Particle(mStartMinP.x + (mEndMinP.x - mStartMinP.x) / COLUMN_NUM * j, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / ROW_NUM * i, dip2px(0.8f));
            }
        }
 
        mStartMaxP = new PointF(mWidth / 2 - DEFAULT_MAX_TEXT_SIZE, mHeight / 2 - DEFAULT_MAX_TEXT_SIZE);
        mEndMaxP = new PointF(mWidth / 2 + DEFAULT_MAX_TEXT_SIZE, mHeight / 2 + DEFAULT_MAX_TEXT_SIZE);
 
        for (int i = 0; i < ROW_NUM; i++) {
            for (int j = 0; j < COLUMN_NUM; j++) {
                mParticles[i][j] = new Particle(mStartMaxP.x + (mEndMaxP.x - mStartMaxP.x) / COLUMN_NUM * j, mStartMaxP.y + (mEndMaxP.y - mStartMaxP.y) / ROW_NUM * i, getTextWidth(mHostText + mParticleText, mParticleTextPaint) / (COLUMN_NUM * 1.8f));
            }
        }
 
        Shader linearGradient = new LinearGradient(mWidth / 2 - getTextWidth(mParticleText, mCirclePaint) / 2f,
                mHeight / 2 - getTextHeight(mParticleText, mCirclePaint) / 2,
                mWidth / 2 - getTextWidth(mParticleText, mCirclePaint) / 2,
                mHeight / 2 + getTextHeight(mParticleText, mCirclePaint) / 2,
                new int[]{mParticleColor, Color.argb(120, getR(mParticleColor), getG(mParticleColor), getB(mParticleColor))}, null, Shader.TileMode.CLAMP);
        mCirclePaint.setShader(linearGradient);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        if (mStatus == STATUS_PARTICLE_GATHER) {
            for (int i = 0; i < ROW_NUM; i++) {
                for (int j = 0; j < COLUMN_NUM; j++) {
                    canvas.drawCircle(mParticles[i][j].x, mParticles[i][j].y, mParticles[i][j].radius, mCirclePaint);
                }
            }
        }
 
        if (mStatus == STATUS_TEXT_MOVING) {
            canvas.drawText(mHostText, mHostTextX, mHeight / 2 + getTextHeight(mHostText, mHostBgPaint) / 2, mHostBgPaint);
            canvas.drawRect(mHostTextX + mHostRectWidth, mHeight / 2 - getTextHeight(mHostText, mHostBgPaint) / 1.2f, mHostTextX + getTextWidth(mHostText, mHostTextPaint), mHeight / 2 + getTextHeight(mHostText, mHostBgPaint) / 1.2f, mHostTextPaint);
        }
 
        if (mStatus == STATUS_PARTICLE_GATHER) {
            canvas.drawRoundRect(new RectF(mWidth / 2 - mSpreadWidth, mStartMinP.y, mWidth / 2 + mSpreadWidth, mEndMinP.y), dip2px(2), dip2px(2), mHostBgPaint);
            canvas.drawText(mParticleText, mWidth / 2 - getTextWidth(mParticleText, mParticleTextPaint) / 2, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / 2 + getTextHeight(mParticleText, mParticleTextPaint) / 2, mParticleTextPaint);
        } else if (mStatus == STATUS_TEXT_MOVING) {
            canvas.drawRoundRect(new RectF(mParticleTextX - dip2px(4), mStartMinP.y, mParticleTextX + getTextWidth(mParticleText, mParticleTextPaint) + dip2px(4), mEndMinP.y), dip2px(2), dip2px(2), mHostBgPaint);
            canvas.drawText(mParticleText, mParticleTextX, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / 2 + getTextHeight(mParticleText, mParticleTextPaint) / 2, mParticleTextPaint);
        }
 
    }
 
    private void startParticleAnim() {
 
        mStatus = STATUS_PARTICLE_GATHER;
 
        Collection<Animator> animList = new ArrayList<>();
 
        ValueAnimator textAnim = ValueAnimator.ofInt(DEFAULT_MAX_TEXT_SIZE, mParticleTextSize);
        textAnim.setDuration((int) (mTextAnimTime * 0.8f));
        textAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int textSize = (int) valueAnimator.getAnimatedValue();
                mParticleTextPaint.setTextSize(textSize);
            }
        });
        animList.add(textAnim);
 
        for (int i = 0; i < ROW_NUM; i++) {
            for (int j = 0; j < COLUMN_NUM; j++) {
                final int tempI = i;
                final int tempJ = j;
                ValueAnimator animator = ValueAnimator.ofObject(new LineEvaluator(), mParticles[i][j], mMinParticles[i][j]);
                animator.setDuration(mTextAnimTime + ((int) (mTextAnimTime * 0.02f)) * i + ((int) (mTextAnimTime * 0.03f)) * j);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        mParticles[tempI][tempJ] = (Particle) animation.getAnimatedValue();
                        if (tempI == ROW_NUM - 1 && tempJ == COLUMN_NUM - 1) {
                            invalidate();
                        }
                    }
                });
                animList.add(animator);
            }
        }
 
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animList);
        set.start();
 
        set.addListener(new AnimListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startSpreadAnim();
            }
        });
 
    }
 
    private void startSpreadAnim() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, getTextWidth(mParticleText, mParticleTextPaint) / 2 + dip2px(4));
        animator.setDuration(mSpreadAnimTime);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mSpreadWidth = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.addListener(new AnimListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startHostTextAnim();
            }
        });
        animator.start();
    }
 
    private void startHostTextAnim() {
        mStatus = STATUS_TEXT_MOVING;
 
        Collection<Animator> animList = new ArrayList<>();
 
        ValueAnimator particleTextXAnim = ValueAnimator.ofFloat(mStartMinP.x + dip2px(4), mWidth / 2 - (getTextWidth(mHostText, mHostTextPaint) + getTextWidth(mParticleText, mParticleTextPaint)) / 2 + getTextWidth(mHostText, mHostTextPaint));
        particleTextXAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mParticleTextX = (float) animation.getAnimatedValue();
            }
        });
        animList.add(particleTextXAnim);
 
        ValueAnimator animator = ValueAnimator.ofFloat(0, getTextWidth(mHostText, mHostTextPaint));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mHostRectWidth = (float) animation.getAnimatedValue();
            }
        });
        animList.add(animator);
 
        ValueAnimator hostTextXAnim = ValueAnimator.ofFloat(mStartMinP.x, mWidth / 2 - (getTextWidth(mHostText, mHostTextPaint) + getTextWidth(mParticleText, mParticleTextPaint) + dip2px(20)) / 2);
        hostTextXAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mHostTextX = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animList.add(hostTextXAnim);
 
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animList);
        set.setDuration(mHostTextAnimTime);
        set.addListener(new AnimListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (null != mParticleAnimListener) {
                    mParticleAnimListener.onAnimationEnd();
                }
            }
        });
        set.start();
 
    }
 
    public void startAnim() {
        post(new Runnable() {
            @Override
            public void run() {
                startParticleAnim();
            }
        });
    }
 
    private abstract class AnimListener implements Animator.AnimatorListener {
        @Override
        public void onAnimationStart(Animator animation) {
 
        }
 
        @Override
        public void onAnimationCancel(Animator animation) {
 
        }
 
        @Override
        public void onAnimationRepeat(Animator animation) {
 
        }
    }
 
    public void setOnParticleAnimListener(ParticleAnimListener particleAnimListener) {
        mParticleAnimListener = particleAnimListener;
    }
 
    public interface ParticleAnimListener {
        void onAnimationEnd();
    }
 
    private int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
 
    private int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
 
    private float getTextHeight(String text, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.height() / 1.1f;
    }
 
    private float getTextWidth(String text, Paint paint) {
        return paint.measureText(text);
    }
 
    private int getR(int color) {
        int r = (color >> 16) & 0xFF;
        return r;
    }
 
    private int getG(int color) {
        int g = (color >> 8) & 0xFF;
        return g;
    }
 
    private int getB(int color) {
        int b = color & 0xFF;
        return b;
    }
}