Android自定义View实现带数字的进度条实例代码

时间:2022-08-23 23:44:50

第一步、效果展示

图1、蓝色的进度条

Android自定义View实现带数字的进度条实例代码

图2、红色的进度条

Android自定义View实现带数字的进度条实例代码

图3、多条颜色不同的进度条

Android自定义View实现带数字的进度条实例代码

图4、多条颜色不同的进度条

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

第二步、自定义progressbar实现带数字的进度条

0、项目结构

Android自定义View实现带数字的进度条实例代码

如上图所示:library项目为自定义的带数字的进度条numberprogressbar的具体实现,demo项目为示例项目以工程依赖的方式引用library项目,然后使用自定义的带数字的进度条numberprogressbar来做展示

Android自定义View实现带数字的进度条实例代码

如上图所示:自定义的带数字的进度条的library项目的结构图

Android自定义View实现带数字的进度条实例代码

如上图所示:demo项目的结构图

1、绘制步骤分析

如上面几幅图形所示。这个进度条的可以分为以下三部分:

Android自定义View实现带数字的进度条实例代码

reacherd area :表示当前进度值之前文本的进度条(长方形)

text area :表示当前进度值文本

unreacherd area :当前进度值文本之后的进度条(长方形)

按照上面的分析,我们要实现带数字的进度条,只需要按照以下三个步骤绘制即可实现:

1、绘制reacherd area(当前进度值之前文本的进度条)

2、绘制text area(当前进度值文本)

3、绘制unreacherd area(当前进度值文本之后的进度条) 即可。

2、自定义属性

由于我们发现以上三个部分的颜色、字体大小、进度条的最大值、表示进度条的长方形的高度等属性都可以改变,从而展现出不同的界面效果。

因此我们将这些属性都做自定义属性。这样我们就能够做到像android官方提供的那些组件一样用xml来定义它的属性了。

1、定义自己的属性配置文件:attr.xml

在res/values文件下定义一个attrs.xml文件,res/values/attrs.xml定义代码如下所示:

?
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
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="numberprogressbar">
<!--进度条的当前进度值-->
<attr name="progress_current" format="integer"/>
<!--进度条的最大进度值-->
<attr name="progress_max" format="integer"/>
<!--当前进度值文本之后的进度条颜色-->
<attr name="progress_unreached_color" format="color"/>
<!--当前进度值文本之前的进度条颜色-->
<attr name="progress_reached_color" format="color"/>
<!-- 当前进度值文本之前的进度条的高度-->
<attr name="progress_reached_bar_height" format="dimension"/>
<!--当前进度值文本之后的进度条的高度-->
<attr name="progress_unreached_bar_height" format="dimension"/>
<!--当前进度值文本的字体大小-->
<attr name="progress_text_size" format="dimension"/>
<!--当前进度值文本的颜色-->
<attr name="progress_text_color" format="color"/>
<!--当前进度值之前文本的间距-->
<attr name="progress_text_offset" format="dimension"/>
<!--当前进度值文本是否可见-->
<attr name="progress_text_visibility" format="enum">
<enum name="visible" value="0"/>
<enum name="invisible" value="1"/>
</attr>
</declare-styleable>
<declare-styleable name="themes">
<attr name="numberprogressbarstyle" format="reference"/>
</declare-styleable>
</resources>

2、定义主题配置文件:styles.xml

在res/values文件下定义一个styles.xml文件,里面定义一些基本的主题选项,以备用户可以选择使用。res/values/styles.xml定义代码如下所示:

?
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
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="numberprogressbar_default">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#3498db</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#3498db</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_passing_green">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#70a800</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#70a800</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_beauty_red">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ff3d7f</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ff3d7f</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_warning_red">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#e74c3c</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#e74c3c</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_relax_blue">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#6dbcdb</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#6dbcdb</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_grace_yellow">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ffc73b</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ffc73b</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_funny_orange">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ff530d</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ff530d</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_twinkle_night">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ecf0f1</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ecf0f1</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
</resources>

3、自定义view实现带数字的进度条

?
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package com.daimajia.numberprogressbar;
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rectf;
import android.os.bundle;
import android.os.parcelable;
import android.util.attributeset;
import android.view.view;
import static com.daimajia.numberprogressbar.numberprogressbar.progresstextvisibility.invisible;
import static com.daimajia.numberprogressbar.numberprogressbar.progresstextvisibility.visible;
/**
* created by daimajia on 14-4-30.
*/
public class numberprogressbar extends view {
/**
* 进度值最大值
*/
private int mmaxprogress = 100;
/**
* current progress, can not exceed the max progress.
* 当前进度值,不能超过进度值最大值
*/
private int mcurrentprogress = 0;
/**
* the progress area bar color.
* 当前进度值文本之前的进度条颜色
*/
private int mreachedbarcolor;
/**
* the bar unreached area color.
* 当前进度值文本之后的进度条颜色
*/
private int munreachedbarcolor;
/**
* the progress text color.
* 当前进度值文本的颜色
*/
private int mtextcolor;
/**
* the progress text size.
* 当前进度值文本的字体大小
*/
private float mtextsize;
/**
* the height of the reached area.
* 当前进度值文本之前的进度条的高度
*/
private float mreachedbarheight;
/**
* the height of the unreached area.
* 当前进度值文本之后的进度条的高度
*/
private float munreachedbarheight;
/**
* the suffix of the number.
* 当前进度值的百分比后缀
*/
private string msuffix = "%";
/**
* the prefix.
* 当前进度值的百分比前缀
*/
private string mprefix = "";
//当前进度值文本的默认颜色
private final int default_text_color = color.rgb(66, 145, 241);
//当前进度值文本的字体大小
private final float default_text_size;
//当前进度值之前的默认进度条颜色
private final int default_reached_color = color.rgb(66, 145, 241);
//当前进度值之后的默认进度条颜色
private final int default_unreached_color = color.rgb(204, 204, 204);
//当前进度值之前文本的默认间距
private final float default_progress_text_offset;
//当前进度值文本之前的进度条的默认高度
private final float default_reached_bar_height;
//当前进度值文本之后的进度条的默认高度
private final float default_unreached_bar_height;
/**
* for save and restore instance of progressbar.
*/
private static final string instance_state = "saved_instance";
private static final string instance_text_color = "text_color";
private static final string instance_text_size = "text_size";
private static final string instance_reached_bar_height = "reached_bar_height";
private static final string instance_reached_bar_color = "reached_bar_color";
private static final string instance_unreached_bar_height = "unreached_bar_height";
private static final string instance_unreached_bar_color = "unreached_bar_color";
private static final string instance_max = "max";
private static final string instance_progress = "progress";
private static final string instance_suffix = "suffix";
private static final string instance_prefix = "prefix";
private static final string instance_text_visibility = "text_visibility";
//默认显示当前进度值文本 0为显示,1为不显示
private static final int progress_text_visible = 0;
/**
* the width of the text that to be drawn.
* 要绘制的当前进度值的文本的宽度
*/
private float mdrawtextwidth;
/**
* the drawn text start.
* 要绘制的当前进度值的文本的起始位置
*/
private float mdrawtextstart;
/**
* the drawn text end.
* 要绘制的当前进度值的文本的结束位置
*/
private float mdrawtextend;
/**
* the text that to be drawn in ondraw().
* 要绘制的当前进度值的文本
*/
private string mcurrentdrawtext;
/**
* the paint of the reached area.
* 绘制当前进度值文本之前的进度条的画笔
*/
private paint mreachedbarpaint;
/**
* the paint of the unreached area.
* 绘制当前进度值文本之后的进度条的画笔
*/
private paint munreachedbarpaint;
/**
* the paint of the progress text.
* 绘制当前进度值文本的的画笔
*/
private paint mtextpaint;
/**
* unreached bar area to draw rect.
* 当前进度值文本之后的进度条(长方形)
*/
private rectf munreachedrectf = new rectf(0, 0, 0, 0);
/**
* reached bar area rect.
* 当前进度值之前文本的进度条(长方形)
*/
private rectf mreachedrectf = new rectf(0, 0, 0, 0);
/**
* the progress text offset.
* 当前进度值之前文本的间距
*/
private float moffset;
/**
* determine if need to draw unreached area.
* 是否绘制当前进度值之后的进度条
*/
private boolean mdrawunreachedbar = true;
/**
* 是否绘制当前进度值之前的进度条
*/
private boolean mdrawreachedbar = true;
/**
* 是否绘制当前进度值文本
*/
private boolean mifdrawtext = true;
/**
* listener
*/
private onprogressbarlistener mlistener;
public enum progresstextvisibility {
visible, invisible
}
public numberprogressbar(context context) {
this(context, null);
}
public numberprogressbar(context context, attributeset attrs) {
this(context, attrs, r.attr.numberprogressbarstyle);
}
public numberprogressbar(context context, attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
default_reached_bar_height = dp2px(1.5f);
default_unreached_bar_height = dp2px(1.0f);
default_text_size = sp2px(10);
default_progress_text_offset = dp2px(3.0f);
//获取自定义属性
final typedarray attributes = context.gettheme().obtainstyledattributes(attrs, r.styleable.numberprogressbar,
defstyleattr, 0);
mreachedbarcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_reached_color, default_reached_color);
munreachedbarcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_unreached_color, default_unreached_color);
mtextcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_text_color, default_text_color);
mtextsize = attributes.getdimension(r.styleable.numberprogressbar_progress_text_size, default_text_size);
mreachedbarheight = attributes.getdimension(r.styleable.numberprogressbar_progress_reached_bar_height, default_reached_bar_height);
munreachedbarheight = attributes.getdimension(r.styleable.numberprogressbar_progress_unreached_bar_height, default_unreached_bar_height);
moffset = attributes.getdimension(r.styleable.numberprogressbar_progress_text_offset, default_progress_text_offset);
int textvisible = attributes.getint(r.styleable.numberprogressbar_progress_text_visibility, progress_text_visible);
if (textvisible != progress_text_visible) {
mifdrawtext = false;
}
setprogress(attributes.getint(r.styleable.numberprogressbar_progress_current, 0));
setmax(attributes.getint(r.styleable.numberprogressbar_progress_max, 100));
//回收 typedarray,用于后续调用时可复用之。回收到typedarraypool池中,以备后用
attributes.recycle();
initializepainters();
}
@override
protected int getsuggestedminimumwidth() {
return (int) mtextsize;
}
@override
protected int getsuggestedminimumheight() {
return math.max((int) mtextsize, math.max((int) mreachedbarheight, (int) munreachedbarheight));
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
/**
* measurespec参数的值为int型,分为高32位和低16为,
* 高32位保存的是specmode,低16位表示specsize,
*
* specmode分三种:
1、measurespec.unspecified,父视图不对子视图施加任何限制,子视图可以得到任意想要的大小;
2、measurespec.exactly,父视图希望子视图的大小是specsize中指定的大小;
3、measurespec.at_most,子视图的大小最多是specsize中的大小。
*/
setmeasureddimension(measure(widthmeasurespec, true), measure(heightmeasurespec, false));
}
private int measure(int measurespec, boolean iswidth) {
int result;
int mode = measurespec.getmode(measurespec);
int size = measurespec.getsize(measurespec);
int padding = iswidth ? getpaddingleft() + getpaddingright() : getpaddingtop() + getpaddingbottom();
/**
父决定子的确切大小,子被限定在给定的边界里,忽略本身想要的大小。
(当设置width或height为match_parent时,模式为exactly,因为子view会占据剩余容器的空间,所以它大小是确定的)
*/
if (mode == measurespec.exactly) {
result = size;
} else {
result = iswidth ? getsuggestedminimumwidth() : getsuggestedminimumheight();
result += padding;
/**
*子最大可以达到的指定大小
* (当设置为wrap_content时,模式为at_most, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸)
*/
if (mode == measurespec.at_most) {
if (iswidth) {
result = math.max(result, size);
} else {
result = math.min(result, size);
}
}
}
return result;
}
@override
protected void ondraw(canvas canvas) {
//如果要绘制当前进度值文本
if (mifdrawtext) {
calculatedrawrectf();
}else {
calculatedrawrectfwithoutprogresstext();
}
//如果要绘制当前进度值之前的进度条
if (mdrawreachedbar) {
canvas.drawrect(mreachedrectf, mreachedbarpaint);
}
//如果要绘制当前进度值之后的进度条
if (mdrawunreachedbar) {
canvas.drawrect(munreachedrectf, munreachedbarpaint);
}
//绘制当前进度值文本
if (mifdrawtext)
canvas.drawtext(mcurrentdrawtext, mdrawtextstart, mdrawtextend, mtextpaint);
}
/**
* 初始化画笔
*/
private void initializepainters() {
mreachedbarpaint = new paint(paint.anti_alias_flag);
mreachedbarpaint.setcolor(mreachedbarcolor);
munreachedbarpaint = new paint(paint.anti_alias_flag);
munreachedbarpaint.setcolor(munreachedbarcolor);
mtextpaint = new paint(paint.anti_alias_flag);
mtextpaint.setcolor(mtextcolor);
mtextpaint.settextsize(mtextsize);
}
/**
* 计算不要绘制当前进度值文本时 图形的各个属性
*/
private void calculatedrawrectfwithoutprogresstext() {
//当前进度值不画
//当前进度值之前的进度条(长方形)的属性
mreachedrectf.left = getpaddingleft();
mreachedrectf.top = getheight() / 2.0f - mreachedbarheight / 2.0f;
mreachedrectf.right =
(getwidth() - getpaddingleft() - getpaddingright()) / (getmax() * 1.0f) * getprogress()
+ getpaddingleft();
mreachedrectf.bottom = getheight() / 2.0f + mreachedbarheight / 2.0f;
//当前进度值之后的进度条(长方形)的属性
munreachedrectf.left = mreachedrectf.right;
munreachedrectf.right = getwidth() - getpaddingright();
munreachedrectf.top = getheight() / 2.0f + -munreachedbarheight / 2.0f;
munreachedrectf.bottom = getheight() / 2.0f + munreachedbarheight / 2.0f;
}
/**
* 计算要绘制当前进度值文本时 图形的各个属性
*/
private void calculatedrawrectf() {
//要绘制的当前进度值的文本
mcurrentdrawtext = string.format("%d", getprogress() * 100 / getmax());
mcurrentdrawtext = mprefix + mcurrentdrawtext + msuffix;
//要绘制的当前进度值的文本的宽度
mdrawtextwidth = mtextpaint.measuretext(mcurrentdrawtext);
//如果当前进度值为0,则不绘制当前进度值之前的进度条
if (getprogress() == 0) {
mdrawreachedbar = false;
mdrawtextstart = getpaddingleft();
}
//否则绘制当前进度值文本之前的进度条
else {
mdrawreachedbar = true;
//当前进度值文本之前的进度条(长方形)的属性
mreachedrectf.left = getpaddingleft();
mreachedrectf.top = getheight() / 2.0f - mreachedbarheight / 2.0f;
mreachedrectf.right= (getwidth() - getpaddingleft() - getpaddingright()) / (getmax() * 1.0f) * getprogress()
- moffset + getpaddingleft();
mreachedrectf.bottom = getheight() / 2.0f + mreachedbarheight / 2.0f;
//当前进度值的文本的起始位置
mdrawtextstart = (mreachedrectf.right + moffset);
}
//当前进度值的文本的结束位置
mdrawtextend = (int) ((getheight() / 2.0f) - ((mtextpaint.descent() + mtextpaint.ascent()) / 2.0f));
//如果画不下当前进度值的文本了,就重新计算下当前进度值的文本的起始位置和当前进度值之前的进度条(长方形)的右边
if ((mdrawtextstart + mdrawtextwidth) >= getwidth() - getpaddingright()) {
mdrawtextstart = getwidth() - getpaddingright() - mdrawtextwidth;
mreachedrectf.right = mdrawtextstart - moffset;
}
//当前进度值文本之后的进度条的起始位置
float unreachedbarstart = mdrawtextstart + mdrawtextwidth + moffset;
//如果画不下进度值文本之后的进度条了,就不画进度值之后的进度条
if (unreachedbarstart >= getwidth() - getpaddingright()) {
mdrawunreachedbar = false;
} else {
mdrawunreachedbar = true;
//当前进度值文本之后的进度条(长方形)的属性
munreachedrectf.left = unreachedbarstart;
munreachedrectf.right = getwidth() - getpaddingright();
munreachedrectf.top = getheight() / 2.0f + -munreachedbarheight / 2.0f;
munreachedrectf.bottom = getheight() / 2.0f + munreachedbarheight / 2.0f;
}
}
/**
* get progress text color.
* 获取当前进度值文本的颜色
* @return progress text color.
*/
public int gettextcolor() {
return mtextcolor;
}
/**
* get progress text size.
* 获取当前进度值文本的字体大小
* @return progress text size.
*/
public float getprogresstextsize() {
return mtextsize;
}
/**
* 获取当前进度值文本之后的进度条颜色
*/
public int getunreachedbarcolor() {
return munreachedbarcolor;
}
/**
* 获取当前进度值文本之前的进度条颜色
*/
public int getreachedbarcolor() {
return mreachedbarcolor;
}
/**
* 获取进度条的当前进度值
*/
public int getprogress() {
return mcurrentprogress;
}
/**
* 获取进度条的最大值
*/
public int getmax() {
return mmaxprogress;
}
/**
* 获取当前进度值文本之前的进度条的高度
*/
public float getreachedbarheight() {
return mreachedbarheight;
}
/**
* 获取当前进度值文本之后的进度条的高度
*/
public float getunreachedbarheight() {
return munreachedbarheight;
}
/**
* 设置当前进度值文本的字体大小
* @param textsize 当前进度值文本的字体大小
*/
public void setprogresstextsize(float textsize) {
this.mtextsize = textsize;
mtextpaint.settextsize(mtextsize);
invalidate();
}
/**
* 设置当前进度值文本的颜色
* @param textcolor 当前进度值文本的颜色
*/
public void setprogresstextcolor(int textcolor) {
this.mtextcolor = textcolor;
mtextpaint.setcolor(mtextcolor);
invalidate();
}
/**
* 设置当前进度值文本之后的进度条颜色
* @param barcolor 当前进度值文本之后的进度条颜色
*/
public void setunreachedbarcolor(int barcolor) {
this.munreachedbarcolor = barcolor;
munreachedbarpaint.setcolor(munreachedbarcolor);
invalidate();
}
/**
* 设置当前进度值文本之前的进度条颜色
* @param progresscolor 当前进度值文本之前的进度条颜色
*/
public void setreachedbarcolor(int progresscolor) {
this.mreachedbarcolor = progresscolor;
mreachedbarpaint.setcolor(mreachedbarcolor);
invalidate();
}
/**
* 设置当前进度值文本之前的进度条的高度
* @param height 当前进度值文本之前的进度条的高度
*/
public void setreachedbarheight(float height) {
mreachedbarheight = height;
}
/**
* 设置当前进度值文本之后的进度条的高度
* @param height 当前进度值文本之后的进度条的高度
*/
public void setunreachedbarheight(float height) {
munreachedbarheight = height;
}
/**
* 设置进度值的最大值
* @param maxprogress 进度值的最大值
*/
public void setmax(int maxprogress) {
if (maxprogress > 0) {
this.mmaxprogress = maxprogress;
invalidate();
}
}
/**
* 设置当前进度值文本的后缀
* @param suffix 当前进度值文本的后缀
*/
public void setsuffix(string suffix) {
if (suffix == null) {
msuffix = "";
} else {
msuffix = suffix;
}
}
/**
* 获取当前进度值文本的后缀
*/
public string getsuffix() {
return msuffix;
}
/**
* 设置当前进度值文本的前缀
* @param prefix 当前进度值文本的前缀
*/
public void setprefix(string prefix) {
if (prefix == null)
mprefix = "";
else {
mprefix = prefix;
}
}
/**
* 获取当前进度值文本的前缀
*/
public string getprefix() {
return mprefix;
}
/**
* 设置进度条的当前进度值增加
* @param by 增加多少
*/
public void incrementprogressby(int by) {
if (by > 0) {
setprogress(getprogress() + by);
}
if (mlistener != null) {
//回调onprogresschange()方法来处理进度值变化后的事件
mlistener.onprogresschange(getprogress(), getmax());
}
}
/**
* 设置当前进度值
*
* @param progress 当前进度值
*/
public void setprogress(int progress) {
if (progress <= getmax() && progress >= 0) {
this.mcurrentprogress = progress;
invalidate();
}
}
@override
protected parcelable onsaveinstancestate() {
final bundle bundle = new bundle();
bundle.putparcelable(instance_state, super.onsaveinstancestate());
bundle.putint(instance_text_color, gettextcolor());
bundle.putfloat(instance_text_size, getprogresstextsize());
bundle.putfloat(instance_reached_bar_height, getreachedbarheight());
bundle.putfloat(instance_unreached_bar_height, getunreachedbarheight());
bundle.putint(instance_reached_bar_color, getreachedbarcolor());
bundle.putint(instance_unreached_bar_color, getunreachedbarcolor());
bundle.putint(instance_max, getmax());
bundle.putint(instance_progress, getprogress());
bundle.putstring(instance_suffix, getsuffix());
bundle.putstring(instance_prefix, getprefix());
bundle.putboolean(instance_text_visibility, getprogresstextvisibility());
return bundle;
}
@override
protected void onrestoreinstancestate(parcelable state) {
if (state instanceof bundle) {
final bundle bundle = (bundle) state;
mtextcolor = bundle.getint(instance_text_color);
mtextsize = bundle.getfloat(instance_text_size);
mreachedbarheight = bundle.getfloat(instance_reached_bar_height);
munreachedbarheight = bundle.getfloat(instance_unreached_bar_height);
mreachedbarcolor = bundle.getint(instance_reached_bar_color);
munreachedbarcolor = bundle.getint(instance_unreached_bar_color);
initializepainters();
setmax(bundle.getint(instance_max));
setprogress(bundle.getint(instance_progress));
setprefix(bundle.getstring(instance_prefix));
setsuffix(bundle.getstring(instance_suffix));
setprogresstextvisibility(bundle.getboolean(instance_text_visibility) ? visible : invisible);
super.onrestoreinstancestate(bundle.getparcelable(instance_state));
return;
}
super.onrestoreinstancestate(state);
}
/**
* dp转px
*/
public float dp2px(float dp) {
final float scale = getresources().getdisplaymetrics().density;
return dp * scale + 0.5f;
}
/**
* sp转px
*/
public float sp2px(float sp) {
final float scale = getresources().getdisplaymetrics().scaleddensity;
return sp * scale;
}
/**
* 设置是否绘制当前进度值文本
*/
public void setprogresstextvisibility(progresstextvisibility visibility) {
mifdrawtext = visibility == visible;
invalidate();
}
/**
* 获取是否绘制当前进度值文本
*/
public boolean getprogresstextvisibility() {
return mifdrawtext;
}
/**
* 设置进度值变化时的监听器
*/
public void setonprogressbarlistener(onprogressbarlistener listener) {
mlistener = listener;
}
}

如以上代码所示:

在自定义numberprogressbar控件的构造方法中,去获取了全部设置好了的自定义属性值,如果没有设置则使用默认的自定义属性值。

然后先重写onmeasure(int widthmeasurespec, int heightmeasurespec)方法,来确定自定义numberprogressbar控件的大小。

接着重写ondraw()方法,进行绘制自定义的带数字的进度条。

第三步、将自定义带数字的进度条添加到布局文件中

在res/layout目录下定义一个activity_main.xml文件,res/layout/activity_main.xml定义代码如下所示:

?
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
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
android:paddingbottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.daimajia.numberprogressbar.example.mainactivity=">
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar1"
android:layout_width="wrap_content"
android:padding="20dp"
custom:progress_current="0"
style="@style/numberprogressbar_default"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar2"
android:layout_height="wrap_content"
android:padding="20dp"
custom:progress_current="20"
android:layout_width="match_parent"
style="@style/numberprogressbar_passing_green"
/>
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar3"
android:layout_margin="20dp"
style="@style/numberprogressbar_relax_blue"
custom:progress_current="30"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar4"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_grace_yellow"
custom:progress_current="40"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar5"
android:layout_width="wrap_content"
android:layout_margin="20dp"
custom:progress_current="50"
style="@style/numberprogressbar_warning_red"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar6"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_funny_orange"
custom:progress_current="60"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar7"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_beauty_red"
custom:progress_current="70"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar8"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_twinkle_night"
custom:progress_current="80"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar9"
android:layout_width="wrap_content"
android:layout_margin="20dp"
custom:progress_current="20"
custom:progress_max="100"
custom:progress_unreached_color="#ff530d"
custom:progress_reached_color="#6dbcdb"
custom:progress_text_size="10sp"
custom:progress_text_color="#ecf0f1"
custom:progress_reached_bar_height="1.5dp"
custom:progress_unreached_bar_height="0.75dp"
android:layout_height="wrap_content" />
</linearlayout>

第四步、编写activity加载布局文件,显示自定义的带数字的进度条

mainactity的代码如下所示:

?
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
package com.daimajia.numberprogressbar.example;
import android.app.activity;
import android.graphics.color;
import android.os.bundle;
import android.widget.toast;
import com.daimajia.numberprogressbar.numberprogressbar;
import com.daimajia.numberprogressbar.onprogressbarlistener;
import java.util.timer;
import java.util.timertask;
public class mainactivity extends activity implements onprogressbarlistener {
private timer timer;
private numberprogressbar bnp;
private numberprogressbar bnp9;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
bnp = (numberprogressbar)findviewbyid(r.id.numberbar1);
bnp.setonprogressbarlistener(this);
bnp9 = (numberprogressbar)findviewbyid(r.id.numberbar9);
bnp9.setprefix("欧阳鹏:");
bnp9.setsuffix("% csdn");
bnp9.setprogresstextsize(20);
bnp9.setprogresstextcolor(color.yellow);
bnp9.setprogresstextvisibility(numberprogressbar.progresstextvisibility.visible);
bnp9.setunreachedbarcolor(color.red);
bnp9.setreachedbarheight(10);
bnp9.setreachedbarheight(5);
timer = new timer();
timer.schedule(new timertask() {
@override
public void run() {
runonuithread(new runnable() {
@override
public void run() {
bnp.incrementprogressby(1);
}
});
}
}, 1000, 100);
}
@override
protected void ondestroy() {
super.ondestroy();
timer.cancel();
}
@override
public void onprogresschange(int current, int max) {
if(current == max) {
toast.maketext(getapplicationcontext(), getstring(r.string.finish), toast.length_short).show();
bnp.setprogress(0);
}
}
}

显示出的效果图为:

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

看完介绍后,读者可以到以下地址去查看完整的项目代码

daimajia的github上该项目的原始地址

https://github.com/daimajia/numberprogressbar

这里还有另外一个numberprogresbar的例子,如下图所示

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

以上内容是小编给大家介绍的android自定义view实现带数字的进度条实例代码,希望对大家有所帮助!