Skip to content

Commit 3c707d4

Browse files
author
yangjian
committed
refactor
1 parent 5573058 commit 3c707d4

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

app/src/main/java/com/jian/explosion/animation/ExplosionAnimator.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,60 @@ class ExplosionAnimator extends ValueAnimator {
3737
private View mContainer;
3838

3939
public ExplosionAnimator(View view, Bitmap bitmap, Rect bound) {
40-
41-
mPaint = new Paint();
42-
mContainer = view;
43-
4440
setFloatValues(0.0f, 1.0f);
4541
setDuration(DEFAULT_DURATION);
4642

43+
mPaint = new Paint();
44+
mContainer = view;
4745
mParticles = generateParticles(bitmap, bound);
4846
}
4947

48+
// 生成粒子,按行按列生成全部粒子
5049
private ParticleModel[][] generateParticles(Bitmap bitmap, Rect bound) {
5150
int w = bound.width();
5251
int h = bound.height();
5352

54-
int partW_Count = w / ParticleModel.PART_WH; //横向个数
55-
int partH_Count = h / ParticleModel.PART_WH; //竖向个数
53+
// 横向粒子的个数
54+
int horizontalCount = w / ParticleModel.PART_WH;
55+
// 竖向粒子的个数
56+
int verticalCount = h / ParticleModel.PART_WH;
5657

57-
int bitmap_part_w = bitmap.getWidth() / partW_Count;
58-
int bitmap_part_h = bitmap.getHeight() / partH_Count;
58+
// 粒子宽度
59+
int bitmapPartWidth = bitmap.getWidth() / horizontalCount;
60+
// 粒子高度
61+
int bitmapPartHeight = bitmap.getHeight() / verticalCount;
5962

60-
ParticleModel[][] particles = new ParticleModel[partH_Count][partW_Count];
61-
Point point = null;
62-
for (int row = 0; row < partH_Count; row++) { //行
63-
for (int column = 0; column < partW_Count; column++) { //列
63+
ParticleModel[][] particles = new ParticleModel[verticalCount][horizontalCount];
64+
for (int row = 0; row < verticalCount; row++) {
65+
for (int column = 0; column < horizontalCount; column++) {
6466
//取得当前粒子所在位置的颜色
65-
int color = bitmap.getPixel(column * bitmap_part_w, row * bitmap_part_h);
66-
67-
point = new Point(column, row); //x是列,y是行
67+
int color = bitmap.getPixel(column * bitmapPartWidth, row * bitmapPartHeight);
6868

69-
particles[row][column] = ParticleModel.generateParticle(color, bound, point);
69+
Point point = new Point(column, row);
70+
particles[row][column] = new ParticleModel(color, bound, point);
7071
}
7172
}
72-
7373
return particles;
7474
}
7575

76+
// 由view调用,在View上绘制全部的粒子
7677
void draw(Canvas canvas) {
77-
if (!isStarted()) { //动画结束时停止
78+
// 动画结束时停止
79+
if (!isStarted()) {
7880
return;
7981
}
82+
// 遍历粒子,并绘制在View上
8083
for (ParticleModel[] particle : mParticles) {
8184
for (ParticleModel p : particle) {
8285
p.advance((Float) getAnimatedValue());
8386
mPaint.setColor(p.color);
84-
// mPaint.setAlpha((int) (255 * p.alpha)); //只是这样设置,透明色会显示为黑色
85-
mPaint.setAlpha((int) (Color.alpha(p.color) * p.alpha)); //这样透明颜色就不是黑色了
87+
// 错误的设置方式只是这样设置,透明色会显示为黑色
88+
// mPaint.setAlpha((int) (255 * p.alpha));
89+
// 正确的设置方式,这样透明颜色就不是黑色了
90+
mPaint.setAlpha((int) (Color.alpha(p.color) * p.alpha));
8691
canvas.drawCircle(p.cx, p.cy, p.radius, mPaint);
8792
}
8893
}
89-
9094
mContainer.invalidate();
9195
}
9296

app/src/main/java/com/jian/explosion/animation/ExplosionField.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ protected void onDraw(Canvas canvas) {
5151
}
5252

5353
/**
54-
* 爆破
55-
*
56-
* @param view 使得该view爆破
54+
* 执行爆破破碎动画
5755
*/
5856
public void explode(final View view, final AnimatorListenerAdapter listener) {
5957
Rect rect = new Rect();
@@ -62,6 +60,7 @@ public void explode(final View view, final AnimatorListenerAdapter listener) {
6260

6361
animator = new ExplosionAnimator(this, createBitmapFromView(view), rect);
6462

63+
// 接口回调
6564
animator.addListener(new Animator.AnimatorListener() {
6665
@Override
6766
public void onAnimationStart(Animator animation) {
@@ -95,17 +94,14 @@ public void onAnimationRepeat(Animator animation) {
9594
}
9695

9796
private Bitmap createBitmapFromView(View view) {
98-
/*
99-
* 为什么屏蔽以下代码段?
100-
* 如果ImageView直接得到位图,那么当它设置背景(backgroud)时,不会读取到背景颜色
101-
*/
97+
// 为什么屏蔽以下代码段?
98+
// 如果ImageView直接得到位图,那么当它设置背景(backgroud)时,不会读取到背景颜色
10299
// if (view instanceof ImageView) {
103100
// Drawable drawable = ((ImageView)view).getDrawable();
104101
// if (drawable != null && drawable instanceof BitmapDrawable) {
105102
// return ((BitmapDrawable) drawable).getBitmap();
106103
// }
107104
// }
108-
109105
//view.clearFocus(); //不同焦点状态显示的可能不同——(azz:不同就不同有什么关系?)
110106

111107
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
@@ -114,14 +110,15 @@ private Bitmap createBitmapFromView(View view) {
114110
synchronized (mCanvas) {
115111
mCanvas.setBitmap(bitmap);
116112
view.draw(mCanvas);
117-
mCanvas.setBitmap(null); //清除引用
113+
// 清除引用
114+
mCanvas.setBitmap(null);
118115
}
119116
}
120117
return bitmap;
121118
}
122119

123120
/**
124-
* 给Activity加上全屏覆盖的ExplosionField
121+
* 将创建的ExplosionField添加到Activity上
125122
*/
126123
private void attach2Activity(Activity activity) {
127124
ViewGroup rootView = activity.findViewById(Window.ID_ANDROID_CONTENT);
@@ -131,6 +128,9 @@ private void attach2Activity(Activity activity) {
131128
rootView.addView(this, lp);
132129
}
133130

131+
/**
132+
* 将ExplosionField从Activity上移除
133+
*/
134134
private void removeFromActivity(Activity activity) {
135135
ViewGroup rootView = activity.findViewById(Window.ID_ANDROID_CONTENT);
136136
rootView.removeView(this);

app/src/main/java/com/jian/explosion/animation/ParticleModel.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,36 @@
2727
* 时间:2017/12/26.
2828
*/
2929
class ParticleModel {
30-
static final int PART_WH = 8; //默认小球宽高
31-
32-
//原本的值(不可变)
33-
// float originCX;
34-
// float originCY;
35-
// float originRadius;
36-
37-
//实际的值(可变)
38-
float cx; //center x of circle
39-
float cy; //center y of circle
30+
// 默认小球宽高
31+
static final int PART_WH = 8;
32+
// 随机数,随机出位置和大小
33+
static Random random = new Random();
34+
//center x of circle
35+
float cx;
36+
//center y of circle
37+
float cy;
38+
// 半径
4039
float radius;
41-
40+
// 颜色
4241
int color;
42+
// 透明度
4343
float alpha;
44-
45-
static Random random = new Random();
46-
44+
// 整体边界
4745
Rect mBound;
4846

49-
static ParticleModel generateParticle(int color, Rect bound, Point point) {
47+
ParticleModel(int color, Rect bound, Point point) {
5048
int row = point.y; //行是高
5149
int column = point.x; //列是宽
5250

53-
ParticleModel particle = new ParticleModel();
54-
particle.mBound = bound;
55-
particle.color = color;
56-
particle.alpha = 1f;
57-
58-
particle.radius = PART_WH;
59-
particle.cx = bound.left + PART_WH * column;
60-
particle.cy = bound.top + PART_WH * row;
61-
62-
return particle;
51+
this.mBound = bound;
52+
this.color = color;
53+
this.alpha = 1f;
54+
this.radius = PART_WH;
55+
this.cx = bound.left + PART_WH * column;
56+
this.cy = bound.top + PART_WH * row;
6357
}
6458

59+
// 每一步动画都得重新计算出自己的状态值
6560
void advance(float factor) {
6661
cx = cx + factor * random.nextInt(mBound.width()) * (random.nextFloat() - 0.5f);
6762
cy = cy + factor * random.nextInt(mBound.height() / 2);

0 commit comments

Comments
 (0)