22
33import android .animation .ValueAnimator ;
44import android .annotation .TargetApi ;
5+ import android .app .Activity ;
56import android .content .Context ;
67import android .graphics .Canvas ;
78import android .graphics .Color ;
89import android .graphics .Paint ;
9- import android .graphics .PorterDuff ;
10- import android .graphics .PorterDuffXfermode ;
10+ import android .graphics .Rect ;
1111import android .os .Build ;
1212import android .util .AttributeSet ;
1313import android .util .Log ;
1414import android .view .View ;
15+ import android .view .ViewGroup ;
16+ import android .view .Window ;
17+ import android .view .animation .OvershootInterpolator ;
1518
1619import java .util .ArrayList ;
1720import java .util .List ;
2326 */
2427public class SmallBang extends View {
2528
26- int [] colors = {0xFFDF4288 , 0xFFCD8BF8 ,0XFF2B9DF2 ,0XFFA4EEB4 ,0XFFE097CA ,0XFFCAACC6 ,0XFFC5A5FC ,0XFFF5BC16 ,0XFFF2DFC8 ,0XFFE1BE8E ,0XFFC8C79D };
29+ int [] colors = {0xFFDF4288 , 0xFFCD8BF8 , 0XFF2B9DF2 , 0XFFA4EEB4 , 0XFFE097CA , 0XFFCAACC6 , 0XFFC5A5FC , 0XFFF5BC16 , 0XFFF2DFC8 , 0XFFE1BE8E , 0XFFC8C79D };
30+ List <Dot > dotList = new ArrayList <>();
2731 private long ANIMATE_DURATION = 1000 ;
2832 private float MAX_RADIUS = 150 ;
2933 private float MAX_CIRCLE_RADIUS = 100 ;
@@ -36,8 +40,7 @@ public class SmallBang extends View {
3640 private int DOT_NUMBER = 16 ;
3741 private float DOT_BIG_RADIUS = 8 ;
3842 private float DOT_SMALL_RADIUS = 5 ;
39-
40-
43+ private int [] mExpandInset = new int [2 ];
4144 // 将下面的view变小
4245 // 画圆半径从小到大,同时颜色渐变 (P1)
4346 // 当半径到达 MAX_RADIUS, 开始画空心圆,空闲圆半径变大,画笔宽度从MAX_RADIUS变小
@@ -73,6 +76,38 @@ private void init(AttributeSet attrs, int defStyleAttr) {
7376
7477 }
7578
79+ public void bang (final View view ) {
80+ Rect r = new Rect ();
81+ view .getGlobalVisibleRect (r );
82+ int [] location = new int [2 ];
83+ getLocationOnScreen (location );
84+ r .offset (-location [0 ], -location [1 ]);
85+ r .inset (-mExpandInset [0 ], -mExpandInset [1 ]);
86+ int startDelay = 100 ;
87+ view .setScaleX (0.1f );
88+ view .setScaleY (0.1f );
89+ ValueAnimator animator = ValueAnimator .ofFloat (0f , 1f ).setDuration ((long ) (ANIMATE_DURATION *0.5f ));
90+ animator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
91+
92+ // Random random = new Random();
93+
94+ @ Override
95+ public void onAnimationUpdate (ValueAnimator animation ) {
96+ // view.setTranslationX((random.nextFloat() - 0.5f) * view.getWidth() * 0.05f);
97+ // view.setTranslationY((random.nextFloat() - 0.5f) * view.getHeight() * 0.05f);
98+ float animatedFraction = animation .getAnimatedFraction ();
99+ view .setScaleX (0.1f + animatedFraction *0.9f );
100+ view .setScaleY (0.1f + animatedFraction *0.9f );
101+ }
102+ });
103+ animator .setInterpolator (new OvershootInterpolator (2 ));
104+ animator .setStartDelay ((long ) (ANIMATE_DURATION * P3 ));
105+ animator .start ();
106+ // view.animate().setDuration(150).setStartDelay(startDelay).scaleX(0f).scaleY(0f).alpha(0f).start();
107+ //explode(Utils.createBitmapFromView(view), r, startDelay, ExplosionAnimator.DEFAULT_DURATION);
108+ bang ();
109+ }
110+
76111 public void bang () {
77112 ValueAnimator valueAnimator = new ValueAnimator ().ofFloat (0 , 1 ).setDuration (ANIMATE_DURATION );
78113 valueAnimator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
@@ -86,11 +121,10 @@ public void onAnimationUpdate(ValueAnimator animation) {
86121 initDots ();
87122 }
88123
89- List <Dot > dotList = new ArrayList <>();
90- private void initDots (){
124+ private void initDots () {
91125
92126 Random random = new Random (System .currentTimeMillis ());
93- for (int i = 0 ; i < DOT_NUMBER * 2 ; i ++) {
127+ for (int i = 0 ; i < DOT_NUMBER * 2 ; i ++) {
94128 Dot dot = new Dot ();
95129 dot .startColor = colors [random .nextInt (99999 ) % colors .length ];
96130 dot .endColor = colors [random .nextInt (99999 ) % colors .length ];
@@ -104,17 +138,17 @@ protected void onDraw(Canvas canvas) {
104138
105139 Log .d ("SmallBang" , "draw" + progress );
106140
107- if (progress >= 0 && progress <= P1 ) {
141+ if (progress >= 0 && progress <= P1 ) {
108142 float progress1 = 1f / P1 * progress ;
109143 if (progress1 > 1 ) progress1 = 1 ;
110144 int startColor = colors [0 ];
111145 int endColor = colors [1 ];
112146 circlePaint .setStyle (Paint .Style .FILL );
113147 circlePaint .setColor (evaluateColor (startColor , endColor , progress1 ));
114148 canvas .drawCircle (getWidth () / 2 , getHeight () / 2 , MAX_CIRCLE_RADIUS * progress1 , circlePaint );
115- }else if (progress > P1 ){
149+ } else if (progress > P1 ) {
116150
117- if (progress > P1 && progress <= P3 ) {
151+ if (progress > P1 && progress <= P3 ) {
118152 float progress2 = (progress - P1 ) / (P3 - P1 );
119153 if (progress2 < 0 ) progress2 = 0 ;
120154 if (progress2 > 1 ) progress2 = 1 ;
@@ -150,14 +184,19 @@ protected void onDraw(Canvas canvas) {
150184 }
151185 }
152186 }
153- }
154187
155188
156- class Dot {
157- int startColor ;
158- int endColor ;
159189 }
160190
191+ public static SmallBang attach2Window (Activity activity ) {
192+ ViewGroup rootView = (ViewGroup ) activity .findViewById (Window .ID_ANDROID_CONTENT );
193+ SmallBang smallBang = new SmallBang (activity );
194+ rootView .addView (smallBang , new ViewGroup .LayoutParams (
195+ ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
196+ return smallBang ;
197+ }
198+
199+
161200 private int evaluateColor (int startValue , int endValue , float fraction ) {
162201 if (fraction <= 0 ) {
163202 return startValue ;
@@ -179,4 +218,9 @@ private int evaluateColor(int startValue, int endValue, float fraction) {
179218
180219 return ((startA + (int ) (fraction * (endA - startA ))) << 24 ) | ((startR + (int ) (fraction * (endR - startR ))) << 16 ) | ((startG + (int ) (fraction * (endG - startG ))) << 8 ) | ((startB + (int ) (fraction * (endB - startB ))));
181220 }
221+
222+ class Dot {
223+ int startColor ;
224+ int endColor ;
225+ }
182226}
0 commit comments