|
2 | 2 |
|
3 | 3 | import android.animation.Animator;
|
4 | 4 | import android.animation.AnimatorListenerAdapter;
|
5 |
| -import android.animation.ObjectAnimator; |
6 | 5 | import android.animation.ValueAnimator;
|
7 | 6 | import android.content.Context;
|
8 | 7 | import android.content.res.TypedArray;
|
@@ -76,8 +75,6 @@ public class BounceBallView extends View {
|
76 | 75 | private Interpolator physicInterpolator; //物理效果插值器
|
77 | 76 | private ValueAnimator[] translateAnim; // 作用与小球位置变换
|
78 | 77 | private float[] translateFraction; //动画比例 [0,1]
|
79 |
| - private ObjectAnimator[] toAlphaAnim;//作用于画笔从不透明到透明 |
80 |
| - private ObjectAnimator[] fromAlphaAnim;//做用于画笔从透明到不透明 |
81 | 78 | MultiDecelerateAccelerateInterpolator interCreater;
|
82 | 79 | private Interpolator defaultInterpolator = new LinearInterpolator();
|
83 | 80 |
|
@@ -149,8 +146,6 @@ private void initData() {
|
149 | 146 |
|
150 | 147 | translateFraction = new float[ballCount];
|
151 | 148 | translateAnim = new ValueAnimator[ballCount];
|
152 |
| - toAlphaAnim = new ObjectAnimator[ballCount]; |
153 |
| - fromAlphaAnim = new ObjectAnimator[ballCount]; |
154 | 149 |
|
155 | 150 | }
|
156 | 151 |
|
@@ -440,6 +435,28 @@ private int getRandomColor() {
|
440 | 435 | (int) (255 * Math.random()));
|
441 | 436 | }
|
442 | 437 |
|
| 438 | + /** |
| 439 | + * 取消已有动画,释放资源 |
| 440 | + */ |
| 441 | + public void cancel(){ |
| 442 | + if(translateAnim != null){ |
| 443 | + for (int i = 0; i < translateAnim.length; i++) { |
| 444 | + if(translateAnim[i] != null){ |
| 445 | + translateAnim[i].cancel(); |
| 446 | + translateAnim[i] = null; |
| 447 | + } |
| 448 | + } |
| 449 | + translateAnim = null; |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + @Override |
| 454 | + protected void onDetachedFromWindow() { |
| 455 | +// Log.d("ccy","-----------------------------detached"); |
| 456 | + cancel(); |
| 457 | + super.onDetachedFromWindow(); |
| 458 | + } |
| 459 | + |
443 | 460 |
|
444 | 461 | /*
|
445 | 462 | --------------以下为动态配置、各种setter/getter
|
@@ -467,6 +484,7 @@ public void apply(){
|
467 | 484 | Log.w(TAG,"调用apply()之前没有调用过config()函数!");
|
468 | 485 | }
|
469 | 486 | isTransaction = false;
|
| 487 | + cancel(); |
470 | 488 |
|
471 | 489 | checkAttrs();
|
472 | 490 | initData();
|
|
0 commit comments