12
12
import android .view .ViewGroup ;
13
13
import android .view .animation .AccelerateInterpolator ;
14
14
import android .view .animation .DecelerateInterpolator ;
15
- import android .widget .FrameLayout ;
16
15
import android .widget .ImageView ;
16
+ import android .widget .LinearLayout ;
17
17
import android .widget .TextView ;
18
18
19
19
import com .mingle .shapeloading .R ;
20
20
import com .nineoldandroids .animation .Animator ;
21
21
import com .nineoldandroids .animation .AnimatorSet ;
22
22
import com .nineoldandroids .animation .ObjectAnimator ;
23
+ import com .nineoldandroids .view .ViewHelper ;
23
24
24
25
25
26
/**
26
27
* Created by zzz40500 on 15/4/6.
27
28
*/
28
- public class LoadingView extends FrameLayout {
29
+ public class LoadingView extends LinearLayout {
29
30
30
31
private static final int ANIMATION_DURATION = 500 ;
31
32
33
+ private static final float FACTOR = 1.2f ;
34
+
32
35
private static float mDistance = 200 ;
33
36
34
37
private ShapeLoadingView mShapeLoadingView ;
@@ -42,27 +45,23 @@ public class LoadingView extends FrameLayout {
42
45
private AnimatorSet mUpAnimatorSet ;
43
46
private AnimatorSet mDownAnimatorSet ;
44
47
48
+ private boolean mStopped = false ;
49
+
50
+ private int mDelay ;
51
+
45
52
public LoadingView (Context context ) {
46
53
super (context );
54
+ init (context , null );
47
55
}
48
56
49
57
public LoadingView (Context context , AttributeSet attrs ) {
50
- super (context , attrs , 0 );
58
+ super (context , attrs );
51
59
init (context , attrs );
52
60
53
61
}
54
62
55
- private void init (Context context , AttributeSet attrs ) {
56
-
57
- TypedArray typedArray = context
58
- .obtainStyledAttributes (attrs , R .styleable .LoadingView );
59
- mLoadText = typedArray .getString (R .styleable .LoadingView_loadingText );
60
- mTextAppearance = typedArray .getResourceId (R .styleable .LoadingView_loadingTextAppearance , -1 );
61
-
62
- typedArray .recycle ();
63
- }
64
-
65
63
64
+ @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
66
65
public LoadingView (Context context , AttributeSet attrs , int defStyleAttr ) {
67
66
super (context , attrs , defStyleAttr );
68
67
init (context , attrs );
@@ -74,46 +73,53 @@ public LoadingView(Context context, AttributeSet attrs, int defStyleAttr, int de
74
73
init (context , attrs );
75
74
}
76
75
77
- public int dip2px (float dipValue ) {
78
- final float scale = getContext ().getResources ().getDisplayMetrics ().density ;
76
+ private void init (Context context , AttributeSet attrs ) {
77
+
78
+ setOrientation (VERTICAL );
79
+ mDistance = dip2px (context , 54f );
80
+ LayoutInflater .from (context ).inflate (R .layout .load_view , this , true );
81
+ mShapeLoadingView = (ShapeLoadingView ) findViewById (R .id .shapeLoadingView );
82
+ mIndicationIm = (ImageView ) findViewById (R .id .indication );
83
+ mLoadTextView = (TextView ) findViewById (R .id .promptTV );
84
+ ViewHelper .setScaleX (mIndicationIm , 0.2f );
85
+
86
+ TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .LoadingView );
87
+ String loadText = typedArray .getString (R .styleable .LoadingView_loadingText );
88
+ int textAppearance = typedArray .getResourceId (R .styleable .LoadingView_loadingText , -1 );
89
+ mDelay = typedArray .getInteger (R .styleable .LoadingView_delay , 80 );
90
+ typedArray .recycle ();
91
+
92
+ if (textAppearance != -1 ) {
93
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
94
+ mLoadTextView .setTextAppearance (textAppearance );
95
+ } else {
96
+ mLoadTextView .setTextAppearance (getContext (), textAppearance );
97
+ }
98
+ }
99
+ setLoadingText (loadText );
100
+ }
101
+
102
+ private int dip2px (Context context , float dipValue ) {
103
+ final float scale = context .getResources ().getDisplayMetrics ().density ;
79
104
return (int ) (dipValue * scale + 0.5f );
80
105
}
81
106
82
107
83
108
@ Override
84
109
protected void onFinishInflate () {
85
110
super .onFinishInflate ();
86
-
87
- View view = LayoutInflater .from (getContext ()).inflate (R .layout .load_view , null );
88
-
89
- mDistance = dip2px (54f );
90
-
91
- LayoutParams layoutParams = new LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
92
-
93
- layoutParams .gravity = Gravity .CENTER ;
94
-
95
- mShapeLoadingView = (ShapeLoadingView ) view .findViewById (R .id .shapeLoadingView );
96
-
97
- mIndicationIm = (ImageView ) view .findViewById (R .id .indication );
98
- mLoadTextView = (TextView ) view .findViewById (R .id .promptTV );
99
-
100
- if (mTextAppearance != -1 ) {
101
- mLoadTextView .setTextAppearance (getContext (), mTextAppearance );
111
+ if (getVisibility () == VISIBLE ) {
112
+ startLoading (mDelay );
102
113
}
103
- setLoadingText (mLoadText );
104
-
105
- addView (view , layoutParams );
106
-
107
-
108
- startLoading (900 );
109
114
}
110
115
111
-
112
- private AnimatorSet mAnimatorSet = null ;
113
-
114
116
private Runnable mFreeFallRunnable = new Runnable () {
115
117
@ Override
116
118
public void run () {
119
+ ViewHelper .setRotation (mShapeLoadingView , 180f );
120
+ ViewHelper .setTranslationY (mShapeLoadingView , 0f );
121
+ ViewHelper .setScaleX (mIndicationIm , 0.2f );
122
+ mStopped = false ;
117
123
freeFall ();
118
124
}
119
125
};
@@ -137,12 +143,7 @@ protected void onDetachedFromWindow() {
137
143
}
138
144
139
145
private void stopLoading () {
140
- if (mAnimatorSet != null ) {
141
- if (mAnimatorSet .isRunning ()) {
142
- mAnimatorSet .cancel ();
143
- }
144
- mAnimatorSet = null ;
145
- }
146
+ mStopped = true ;
146
147
if (mUpAnimatorSet != null ) {
147
148
if (mUpAnimatorSet .isRunning ()) {
148
149
mUpAnimatorSet .cancel ();
@@ -151,6 +152,7 @@ private void stopLoading() {
151
152
for (Animator animator : mUpAnimatorSet .getChildAnimations ()) {
152
153
animator .removeAllListeners ();
153
154
}
155
+ mUpAnimatorSet = null ;
154
156
}
155
157
if (mDownAnimatorSet != null ) {
156
158
if (mDownAnimatorSet .isRunning ()) {
@@ -160,23 +162,34 @@ private void stopLoading() {
160
162
for (Animator animator : mDownAnimatorSet .getChildAnimations ()) {
161
163
animator .removeAllListeners ();
162
164
}
165
+ mDownAnimatorSet = null ;
163
166
}
164
167
this .removeCallbacks (mFreeFallRunnable );
165
168
}
166
169
167
170
@ Override
168
171
public void setVisibility (int visibility ) {
172
+ this .setVisibility (visibility , mDelay );
173
+ }
174
+
175
+ public void setVisibility (int visibility , int delay ) {
169
176
super .setVisibility (visibility );
170
177
if (visibility == View .VISIBLE ) {
171
- startLoading (200 );
178
+ startLoading (delay );
172
179
} else {
173
180
stopLoading ();
174
181
}
175
182
}
176
183
177
- public void setLoadingText (CharSequence loadingText ) {
184
+ public void setDelay (int delay ) {
185
+ mDelay = delay ;
186
+ }
178
187
188
+ public int getDelay () {
189
+ return mDelay ;
190
+ }
179
191
192
+ public void setLoadingText (CharSequence loadingText ) {
180
193
if (TextUtils .isEmpty (loadingText )) {
181
194
mLoadTextView .setVisibility (GONE );
182
195
} else {
@@ -186,20 +199,24 @@ public void setLoadingText(CharSequence loadingText) {
186
199
mLoadTextView .setText (loadingText );
187
200
}
188
201
202
+ public CharSequence getLoadingText (){
203
+ return mLoadTextView .getText ();
204
+ }
205
+
189
206
/**
190
207
* 上抛
191
208
*/
192
209
public void upThrow () {
193
210
194
211
if (mUpAnimatorSet == null ) {
195
212
ObjectAnimator objectAnimator = ObjectAnimator .ofFloat (mShapeLoadingView , "translationY" , mDistance , 0 );
196
- ObjectAnimator scaleIndication = ObjectAnimator .ofFloat (mIndicationIm , "scaleX" , 0.2f , 1 );
213
+ ObjectAnimator scaleIndication = ObjectAnimator .ofFloat (mIndicationIm , "scaleX" , 1f , 0.2f );
197
214
198
215
ObjectAnimator objectAnimator1 = null ;
199
216
switch (mShapeLoadingView .getShape ()) {
200
217
case SHAPE_RECT :
201
218
202
- objectAnimator1 = ObjectAnimator .ofFloat (mShapeLoadingView , "rotation" , 0 , - 120 );
219
+ objectAnimator1 = ObjectAnimator .ofFloat (mShapeLoadingView , "rotation" , 0 , 180 );
203
220
204
221
break ;
205
222
case SHAPE_CIRCLE :
@@ -213,14 +230,12 @@ public void upThrow() {
213
230
break ;
214
231
}
215
232
216
- objectAnimator .setDuration (ANIMATION_DURATION );
217
- objectAnimator1 .setDuration (ANIMATION_DURATION );
218
- objectAnimator .setInterpolator (new DecelerateInterpolator (factor ));
219
- objectAnimator1 .setInterpolator (new DecelerateInterpolator (factor ));
220
233
mUpAnimatorSet = new AnimatorSet ();
221
- mUpAnimatorSet .setDuration (ANIMATION_DURATION );
222
234
mUpAnimatorSet .playTogether (objectAnimator , objectAnimator1 , scaleIndication );
223
235
236
+ mUpAnimatorSet .setDuration (ANIMATION_DURATION );
237
+ mUpAnimatorSet .setInterpolator (new DecelerateInterpolator (FACTOR ));
238
+
224
239
mUpAnimatorSet .addListener (new Animator .AnimatorListener () {
225
240
@ Override
226
241
public void onAnimationStart (Animator animation ) {
@@ -229,7 +244,9 @@ public void onAnimationStart(Animator animation) {
229
244
230
245
@ Override
231
246
public void onAnimationEnd (Animator animation ) {
232
- freeFall ();
247
+ if (!mStopped ) {
248
+ freeFall ();
249
+ }
233
250
234
251
}
235
252
@@ -249,22 +266,19 @@ public void onAnimationRepeat(Animator animation) {
249
266
250
267
}
251
268
252
- public float factor = 1.2f ;
253
-
254
269
/**
255
270
* 下落
256
271
*/
257
272
public void freeFall () {
258
273
259
274
if (mDownAnimatorSet == null ) {
260
275
ObjectAnimator objectAnimator = ObjectAnimator .ofFloat (mShapeLoadingView , "translationY" , 0 , mDistance );
261
- ObjectAnimator scaleIndication = ObjectAnimator .ofFloat (mIndicationIm , "scaleX" , 1 , 0.2f );
276
+ ObjectAnimator scaleIndication = ObjectAnimator .ofFloat (mIndicationIm , "scaleX" , 0.2f , 1f );
262
277
263
- objectAnimator .setDuration (ANIMATION_DURATION );
264
- objectAnimator .setInterpolator (new AccelerateInterpolator (factor ));
265
278
mDownAnimatorSet = new AnimatorSet ();
266
- mDownAnimatorSet .setDuration (ANIMATION_DURATION );
267
279
mDownAnimatorSet .playTogether (objectAnimator , scaleIndication );
280
+ mDownAnimatorSet .setDuration (ANIMATION_DURATION );
281
+ mDownAnimatorSet .setInterpolator (new AccelerateInterpolator (FACTOR ));
268
282
mDownAnimatorSet .addListener (new Animator .AnimatorListener () {
269
283
@ Override
270
284
public void onAnimationStart (Animator animation ) {
@@ -273,9 +287,10 @@ public void onAnimationStart(Animator animation) {
273
287
274
288
@ Override
275
289
public void onAnimationEnd (Animator animation ) {
276
-
277
- mShapeLoadingView .changeShape ();
278
- upThrow ();
290
+ if (!mStopped ) {
291
+ mShapeLoadingView .changeShape ();
292
+ upThrow ();
293
+ }
279
294
}
280
295
281
296
@ Override
0 commit comments