/
RoundLoadingView.java
454 lines (398 loc) · 13.8 KB
/
RoundLoadingView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package com.yazhi1992.yazhilib.widget.RoundView;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.yazhi1992.yazhilib.R;
import com.yazhi1992.yazhilib.utils.LibCalcUtil;
import com.yazhi1992.yazhilib.widget.ProgressWheel.ProgressWheel;
import java.util.Timer;
import java.util.TimerTask;
import static android.R.attr.state_enabled;
import static android.R.attr.state_pressed;
/**
* Created by zengyazhi on 17/6/1.
* <p>
* 带有倒计时功能、loading加载中、设置圆角/可用/点击颜色的textview
*/
// TODO: 2018/1/27 不设置pressTextColor,应该使用已设置的正常的color
public class RoundLoadingView extends RelativeLayout {
//test
private RoundViewDelegate mDelegate;
private TextView mTextView;
private ProgressWheel mProgressWheel;
//显示的文字
private String mNormalText;
private float mTextSize;
private int mTextColor;
private int mTextPressColor;
private int mTextDisableColor;
private Paint mTextPaint;
//loading控件 半径
private int mCircleRadiu;
//loading控件 颜色
private int mCircleColor;
//loading控件 宽度
private int mCircleWidth;
private Context mContext;
//未开启计时
private int mSecond = -1;
//计时器
private VideoTimerTask mVideoTimerTask;
private Timer mVideoTimer;
private Handler mHandler;
//倒计时字样
private String mTimeText1 = "";
private String mTimeText2 = " 秒后重获";
//默认的是否可用状态
private boolean mEnable;
public RoundLoadingView(Context context) {
this(context, null);
}
public RoundLoadingView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public int getCircleRadiu() {
return mCircleRadiu;
}
public void setCircleRadiu(int circleRadiu) {
mCircleRadiu = circleRadiu;
mProgressWheel.setCircleRadius(mCircleRadiu);
}
public int getCircleColor() {
return mCircleColor;
}
public void setCircleColor(int circleColor) {
mCircleColor = circleColor;
mProgressWheel.setBarColor(mCircleColor);
}
public int getCircleWidth() {
return mCircleWidth;
}
public void setCircleWidth(int circleWidth) {
mCircleWidth = circleWidth;
mProgressWheel.setBarWidth(mCircleWidth);
}
public RoundLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
mDelegate = new RoundViewDelegate(this, context, attrs);
TypedArray typedArray = null;
try {
typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundLoadingView, 0, defStyleAttr);
mNormalText = typedArray.getString(R.styleable.RoundLoadingView_rv_text);
mTextColor = typedArray.getColor(R.styleable.RoundLoadingView_rv_textColor, Color.parseColor("#333333"));
mTextSize = typedArray.getInteger(R.styleable.RoundLoadingView_rv_textSize, 15);
mTextPressColor = typedArray.getColor(R.styleable.RoundLoadingView_rv_textPressColor, Color.parseColor("#333333"));
mTextDisableColor = typedArray.getColor(R.styleable.RoundLoadingView_rv_textDisableColor, Color.parseColor("#333333"));
mCircleColor = typedArray.getColor(R.styleable.RoundLoadingView_rv_circleColor, Color.parseColor("#ffffff"));
mCircleWidth = typedArray.getDimensionPixelSize(R.styleable.RoundLoadingView_rv_circleWidth, (int) LibCalcUtil.dp2px(context, 2));
mCircleRadiu = typedArray.getDimensionPixelSize(R.styleable.RoundLoadingView_rv_circleRadius, (int) LibCalcUtil.dp2px(context, 16));
mEnable = typedArray.getBoolean(R.styleable.RoundLoadingView_rv_enable, true);
} finally {
if (typedArray != null) {
typedArray.recycle();
}
}
//添加textview
mTextView = new TextView(context);
if (mNormalText != null && !mNormalText.isEmpty()) {
mTextView.setText(mNormalText);
}
mTextView.setTextSize(mTextSize);
ColorStateList colorStateList = new ColorStateList(
new int[][]{new int[]{-state_pressed, state_enabled}, new int[]{state_pressed, state_enabled}, new int[]{-state_enabled}},
new int[]{mTextColor, mTextPressColor, mTextDisableColor});
mTextView.setTextColor(colorStateList);
mTextView.setGravity(Gravity.CENTER);
mTextView.setDuplicateParentStateEnabled(true);
//只显示当行
mTextView.setSingleLine(true);
LayoutParams layoutParams1 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams1.addRule(CENTER_IN_PARENT);
mTextView.setLayoutParams(layoutParams1);
addView(mTextView);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(mTextSize);
//添加加载控件
mProgressWheel = new ProgressWheel(mContext);
mProgressWheel.setVisibility(View.INVISIBLE);
LayoutParams layoutParams = new LayoutParams(2 * mCircleRadiu, 2 * mCircleRadiu);
layoutParams.addRule(CENTER_IN_PARENT);
mProgressWheel.setLayoutParams(layoutParams);
mProgressWheel.setBarWidth(mCircleWidth);
mProgressWheel.setCircleRadius(mCircleRadiu);
mProgressWheel.setBarColor(mCircleColor);
addView(mProgressWheel);
if (!mEnable) {
setEnabled(false);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//文字 radiu*2 padding
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// 计算出所有的childView的宽和高
measureChildren(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
int computeWidth = 0;
int computeHeight = 0;
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
computeWidth = Math.max(computeWidth, measuredWidth);
computeHeight = Math.max(computeHeight, measuredHeight);
}
computeWidth += (getPaddingLeft() + getPaddingRight());
switch (widthMode) {
case MeasureSpec.AT_MOST:
//wrap_content
//不超过父控件范围
widthSize = computeWidth > widthSize ? widthSize : computeWidth;
break;
case MeasureSpec.UNSPECIFIED:
//自由发挥
widthSize = computeWidth;
break;
case MeasureSpec.EXACTLY:
break;
default:
break;
}
computeHeight += (getPaddingTop() + getPaddingBottom());
switch (heightMode) {
case MeasureSpec.AT_MOST:
heightSize = computeHeight > heightSize ? heightSize : computeHeight;
break;
case MeasureSpec.UNSPECIFIED:
heightSize = computeHeight;
break;
case MeasureSpec.EXACTLY:
break;
default:
break;
}
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mDelegate.isCircleRound()) {
mDelegate.setCornerRadius(getHeight() / 2);
} else {
mDelegate.setBgSelector();
}
}
/**
* 设置加载状态
*
* @param loading true加载/false停止加载
*/
public void setLoading(boolean loading) {
if (mProgressWheel == null || loading == mProgressWheel.isSpinning() || mSecond != -1 || !isEnabled())
return;
if (loading) {
//开始选旋转
mTextView.setVisibility(View.INVISIBLE);
mProgressWheel.setVisibility(View.VISIBLE);
mProgressWheel.spin();
} else {
mTextView.setVisibility(View.VISIBLE);
mProgressWheel.setVisibility(View.INVISIBLE);
mProgressWheel.stopSpinning();
}
}
/**
* 设置显示文字
*
* @param str
*/
public void setText(String str) {
mNormalText = str;
if (mTextView != null) {
mTextView.setText(str);
}
}
public String getText() {
return mNormalText;
}
/**
* 单位sp
*
* @param size
*/
public void setTextSize(float size) {
mTextSize = size;
mTextView.setTextSize(size);
}
private void setTextColor() {
ColorStateList colorStateList = new ColorStateList(
new int[][]{new int[]{-state_pressed, state_enabled}, new int[]{state_pressed, state_enabled}, new int[]{-state_enabled}},
new int[]{mTextColor, mTextPressColor, mTextDisableColor});
mTextView.setTextColor(colorStateList);
}
public float getTextSize() {
return mTextSize;
}
public int getTextPressColor() {
return mTextPressColor;
}
public void setTextPressColor(int textPressColor) {
mTextPressColor = textPressColor;
setTextColor();
}
public int getTextDisableColor() {
return mTextDisableColor;
}
public void setTextDisableColor(int textDisableColor) {
mTextDisableColor = textDisableColor;
setTextColor();
}
public int getTextColor() {
return mTextColor;
}
public void setTextColor(int color) {
mTextColor = color;
setTextColor();
}
/**
* 是否正在加载
*
* @return
*/
public boolean isLoading() {
return mProgressWheel.isSpinning();
}
/**
* 开始倒计时
*
* @param time
*/
private void startTime(int time) {
if (time > 0) {
// 时间
mSecond = time;
mVideoTimer = new Timer(true);
mVideoTimerTask = new VideoTimerTask();
mVideoTimer.schedule(mVideoTimerTask, 1000, 1000);
}
}
/**
* 停止计时
*/
private void stopTime() {
if (null != mVideoTimer) {
mVideoTimer.cancel();
mVideoTimer = null;
mVideoTimerTask.cancel();
mVideoTimerTask = null;
mSecond = -1;
}
setEnabled(mEnable);
}
/**
* 开始倒计时,可以通过{@link #setTextWhenCountDown(String, String)}设置倒计时时显示的字样
*
* @param time 倒计时时间
*/
public void startTimer(int time) {
if (!isEnabled()) return;
stopTime();
setLoading(false);
mTextView.setText(mTimeText1 + time + mTimeText2);
setEnabled(false);
if (mHandler == null) {
mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(android.os.Message msg) {
switch (msg.what) {
case UPDAT_WALL_TIME_TIMER_TASK:
updateText();
break;
case END_TIMER_TASK:
mTextView.setText(mNormalText);
stopTime();
break;
default:
break;
}
return false;
}
});
}
startTime(time);
}
/**
* 记时器
*/
private class VideoTimerTask extends TimerTask {
public void run() {
--mSecond;
if (mSecond < 0) {
stopTime();
return;
}
mHandler.sendEmptyMessage(UPDAT_WALL_TIME_TIMER_TASK);
}
}
/**
* 设置倒计时时字样
*
* @param text1
* @param text2
*/
public void setTextWhenCountDown(String text1, String text2) {
mTimeText1 = text1;
mTimeText2 = text2;
}
/**
* 更新textview字样
*/
private void updateText() {
if (mSecond == 0) {
mHandler.sendEmptyMessage(END_TIMER_TASK);
return;
}
if (mTextView != null) {
mTextView.setText(mTimeText1 + mSecond + mTimeText2);
}
}
private static final int UPDAT_WALL_TIME_TIMER_TASK = 1;
private static final int END_TIMER_TASK = 2;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mProgressWheel == null || !mProgressWheel.isSpinning()) {
return super.onTouchEvent(event);
} else {
//加载时屏蔽点击事件
return true;
}
}
@Override
public void setEnabled(boolean enabled) {
//记录设置的状态
mEnable = enabled;
//如果还在倒计时则等待倒计时结束后设置状态
if (mSecond == -1) {
super.setEnabled(enabled);
}
}
public RoundViewDelegate getDelegate() {
return mDelegate;
}
}