@@ -44,7 +44,10 @@ public class SmallBang extends View {
4444 private float DOT_BIG_RADIUS = 8 ;
4545 private float DOT_SMALL_RADIUS = 5 ;
4646 private int [] mExpandInset = new int [2 ];
47- private SmallBangListener listener ;
47+ private SmallBangListener mListener ;
48+ private int centerY ;
49+ private int centerX ;
50+
4851 // 将下面的view变小
4952 // 画圆半径从小到大,同时颜色渐变 (P1)
5053 // 当半径到达 MAX_RADIUS, 开始画空心圆,空闲圆半径变大,画笔宽度从MAX_RADIUS变小
@@ -71,6 +74,14 @@ public SmallBang(Context context, AttributeSet attrs, int defStyleAttr, int defS
7174 init (attrs , defStyleAttr );
7275 }
7376
77+ public static SmallBang attach2Window (Activity activity ) {
78+ ViewGroup rootView = (ViewGroup ) activity .findViewById (Window .ID_ANDROID_CONTENT );
79+ SmallBang smallBang = new SmallBang (activity );
80+ rootView .addView (smallBang , new ViewGroup .LayoutParams (
81+ ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
82+ return smallBang ;
83+ }
84+
7485 private void init (AttributeSet attrs , int defStyleAttr ) {
7586 circlePaint = new Paint (Paint .ANTI_ALIAS_FLAG );
7687 circlePaint .setStyle (Paint .Style .FILL );
@@ -79,46 +90,89 @@ private void init(AttributeSet attrs, int defStyleAttr) {
7990
8091 /**
8192 * listener for animation change time
93+ *
8294 * @param listener
8395 */
84- public void setListener (SmallBangListener listener ){
85- this .listener = listener ;
96+ public void setmListener (SmallBangListener listener ) {
97+ this .mListener = listener ;
8698 }
8799
88100 /**
89101 * set different colors for last dots
102+ *
90103 * @param newColors
91104 */
92- public void setColors (int [] newColors ){
93- this .colors = Arrays .copyOf (newColors ,newColors .length );
105+ public void setColors (int [] newColors ) {
106+ this .colors = Arrays .copyOf (newColors , newColors .length );
94107 }
95108
96- public void bang (final View view ) {
109+ /**
110+ * set small dot number
111+ * @param dotNumber
112+ */
113+ public void setDotNumber (int dotNumber ){
114+ DOT_NUMBER = dotNumber ;
115+ }
116+
117+ public void bang (final View view , SmallBangListener listener ) {
118+ bang (view , -1 , listener );
119+ }
120+
121+ public void bang (final View view , float radius , SmallBangListener listener ) {
122+
123+ // set listener
124+ if (listener != null ) {
125+ setmListener (listener );
126+ this .mListener .onAnimationStart ();
127+ }
128+
129+ Rect r = new Rect ();
130+ view .getGlobalVisibleRect (r );
131+ int [] location = new int [2 ];
132+ getLocationOnScreen (location );
133+ r .offset (-location [0 ], -location [1 ]);
134+ r .inset (-mExpandInset [0 ], -mExpandInset [1 ]);
97135
98- if (listener !=null ) {
99- listener .onAnimationStart ();
136+ centerX = r .left + r .width () / 2 ;
137+ centerY = r .top + r .height () / 2 ;
138+
139+ if (radius != -1 ) {
140+ initRadius (radius );
141+ } else {
142+ initRadius (Math .max (r .width (),r .height ()));
100143 }
101144
102- int startDelay = 100 ;
103145 view .setScaleX (0.1f );
104146 view .setScaleY (0.1f );
105- ValueAnimator animator = ValueAnimator .ofFloat (0f , 1f ).setDuration ((long ) (ANIMATE_DURATION * 0.5f ));
147+ ValueAnimator animator = ValueAnimator .ofFloat (0f , 1f ).setDuration ((long ) (ANIMATE_DURATION * 0.5f ));
106148 animator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
107149 @ Override
108150 public void onAnimationUpdate (ValueAnimator animation ) {
109151 float animatedFraction = animation .getAnimatedFraction ();
110- view .setScaleX (0.1f + animatedFraction * 0.9f );
111- view .setScaleY (0.1f + animatedFraction * 0.9f );
152+ view .setScaleX (0.1f + animatedFraction * 0.9f );
153+ view .setScaleY (0.1f + animatedFraction * 0.9f );
112154 }
113155 });
114156 animator .setInterpolator (new OvershootInterpolator (2 ));
115157 animator .setStartDelay ((long ) (ANIMATE_DURATION * P3 ));
116158
117159 animator .start ();
118160 bang ();
161+
162+ }
163+
164+ private void initRadius (float max_circle_radius ) {
165+ MAX_CIRCLE_RADIUS = max_circle_radius ;
166+ MAX_RADIUS = MAX_CIRCLE_RADIUS * 1.1f ;
167+ DOT_BIG_RADIUS = MAX_CIRCLE_RADIUS * 0.07f ;
168+ DOT_SMALL_RADIUS = DOT_BIG_RADIUS * 0.5f ;
169+ }
170+
171+ public void bang (final View view ) {
172+ bang (view , null );
119173 }
120174
121- public void bang () {
175+ private void bang () {
122176 ValueAnimator valueAnimator = new ValueAnimator ().ofFloat (0 , 1 ).setDuration (ANIMATE_DURATION );
123177 valueAnimator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
124178 @ Override
@@ -131,8 +185,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
131185 valueAnimator .addListener (new AnimatorListenerAdapter () {
132186 @ Override
133187 public void onAnimationEnd (Animator animation ) {
134- if ( listener != null ) {
135- listener .onAnimationEnd ();
188+ if ( mListener != null ) {
189+ mListener .onAnimationEnd ();
136190 }
137191 }
138192 });
@@ -148,7 +202,6 @@ private void initDots() {
148202 dot .endColor = colors [random .nextInt (99999 ) % colors .length ];
149203 dotList .add (dot );
150204 }
151-
152205 }
153206
154207 @ Override
@@ -163,7 +216,7 @@ protected void onDraw(Canvas canvas) {
163216 int endColor = colors [1 ];
164217 circlePaint .setStyle (Paint .Style .FILL );
165218 circlePaint .setColor (evaluateColor (startColor , endColor , progress1 ));
166- canvas .drawCircle (getWidth () / 2 , getHeight () / 2 , MAX_CIRCLE_RADIUS * progress1 , circlePaint );
219+ canvas .drawCircle (centerX , centerY , MAX_CIRCLE_RADIUS * progress1 , circlePaint );
167220 } else if (progress > P1 ) {
168221
169222 if (progress > P1 && progress <= P3 ) {
@@ -176,7 +229,7 @@ protected void onDraw(Canvas canvas) {
176229 float strokeWidth = (MAX_CIRCLE_RADIUS ) * (1 - progress2 );
177230 circlePaint .setStrokeWidth (strokeWidth );
178231
179- canvas .drawCircle (getWidth () / 2 , getHeight () / 2 , (MAX_CIRCLE_RADIUS ) * progress2 + strokeWidth / 2 , circlePaint );
232+ canvas .drawCircle (centerX , centerY , (MAX_CIRCLE_RADIUS ) * progress2 + strokeWidth / 2 , circlePaint );
180233 }
181234 if (progress >= P2 ) {
182235 circlePaint .setStyle (Paint .Style .FILL );
@@ -188,33 +241,22 @@ protected void onDraw(Canvas canvas) {
188241 Dot dot = dotList .get (i );
189242 circlePaint .setColor (evaluateColor (dot .startColor , dot .endColor , progress3 ));
190243
191- float x = (float ) (r * Math .cos (i * 2 * Math .PI / DOT_NUMBER )) + getWidth () / 2 ;
192- float y = (float ) (r * Math .sin (i * 2 * Math .PI / DOT_NUMBER )) + getHeight () / 2 ;
244+ float x = (float ) (r * Math .cos (i * 2 * Math .PI / DOT_NUMBER )) + centerX ;
245+ float y = (float ) (r * Math .sin (i * 2 * Math .PI / DOT_NUMBER )) + centerY ;
193246 canvas .drawCircle (x , y , DOT_BIG_RADIUS * (1 - progress3 ), circlePaint );
194247
195248 Dot dot2 = dotList .get (i + 1 );
196249
197250 circlePaint .setColor (evaluateColor (dot2 .startColor , dot2 .endColor , progress3 ));
198- float x2 = (float ) (r * Math .cos (i * 2 * Math .PI / DOT_NUMBER + 0.2 )) + getWidth () / 2 ;
199- float y2 = (float ) (r * Math .sin (i * 2 * Math .PI / DOT_NUMBER + 0.2 )) + getHeight () / 2 ;
251+ float x2 = (float ) (r * Math .cos (i * 2 * Math .PI / DOT_NUMBER + 0.2 )) + centerX ;
252+ float y2 = (float ) (r * Math .sin (i * 2 * Math .PI / DOT_NUMBER + 0.2 )) + centerY ;
200253 canvas .drawCircle (x2 , y2 , DOT_SMALL_RADIUS * (1 - progress3 ), circlePaint );
201254
202255 }
203256 }
204257 }
205-
206-
207258 }
208259
209- public static SmallBang attach2Window (Activity activity ) {
210- ViewGroup rootView = (ViewGroup ) activity .findViewById (Window .ID_ANDROID_CONTENT );
211- SmallBang smallBang = new SmallBang (activity );
212- rootView .addView (smallBang , new ViewGroup .LayoutParams (
213- ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
214- return smallBang ;
215- }
216-
217-
218260 private int evaluateColor (int startValue , int endValue , float fraction ) {
219261 if (fraction <= 0 ) {
220262 return startValue ;
0 commit comments