android sdk已经提供有进度条组件progressdialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且progressdialog的可定制行也不太强,这时就需要我们自定义实现一个progressdialog。
通过看源码我们发现,progressdialog继承自alertdialog,有一个progressbar和两个textview组成的,通过对progressdialog的源码进行改进就可以实现一个自定义的progressdialog。
1、效果:
首先看一下自定义commonprogressdialog和原生progressdialog的对比:
2、代码:
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
|
common_progress_dialog.xml 布局文件: <framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <linearlayout android:layout_width= "798px" android:layout_height= "460px" android:orientation= "vertical" android:background= "@drawable/common_progress_dialog_background" > <textview android:id= "@+id/progress_message" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "44px" android:layout_margintop= "113px" android:layout_gravity= "center_horizontal" android:textcolor= "#ffffff" /> <progressbar android:id= "@+id/progress" style= "?android:attr/progressbarstylehorizontal" android:layout_width= "712px" android:layout_height= "30px" android:layout_margintop= "100px" android:layout_marginleft= "47px" android:layout_centerhorizontal= "true" android:progressdrawable= "@drawable/common_progressdialog_progressbar_background" /> <linearlayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:orientation= "horizontal" > <textview android:id= "@+id/progress_percent" android:layout_width= "80px" android:layout_height= "wrap_content" android:textsize= "30px" android:layout_marginleft= "280px" android:gravity= "center_horizontal" android:textcolor= "#ffffff" /> <textview android:id= "@+id/progress_number" android:layout_width= "250px" android:layout_height= "wrap_content" android:layout_marginleft= "120px" android:textsize= "30px" android:gravity= "center_horizontal" android:textcolor= "#ffffff" /> </linearlayout> </linearlayout> </framelayout> common_progressdialog_progressbar_background.xml progressbar进度条图片和背景图片设置 <?xml version= "1.0" encoding= "utf-8" ?> <layer-list xmlns:android= "http://schemas.android.com/apk/res/android" > <item android:id= "@android:id/background" android:drawable= "@drawable/common_progress_dialog_progressbar3" /> <item android:id= "@android:id/progress" android:drawable= "@drawable/common_progress_dialog_progressbar2" /> </layer-list> commonprogressdialog.java类: package com.johnny.testactivity; import java.text.numberformat; import android.app.alertdialog; import android.app.progressdialog; import android.content.context; import android.os.bundle; import android.os.handler; import android.os.message; import android.text.spannable; import android.text.spannablestring; import android.text.style.stylespan; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.widget.progressbar; import android.widget.textview; public class commonprogressdialog extends alertdialog { private progressbar mprogress; private textview mprogressnumber; private textview mprogresspercent; private textview mprogressmessage; private handler mviewupdatehandler; private int mmax; private charsequence mmessage; private boolean mhasstarted; private int mprogressval; private string tag= "commonprogressdialog" ; private string mprogressnumberformat; private numberformat mprogresspercentformat; public commonprogressdialog(context context) { super (context); // todo auto-generated constructor stub initformats(); } @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super .oncreate(savedinstancestate); setcontentview(r.layout.common_progress_dialog); mprogress=(progressbar) findviewbyid(r.id.progress); mprogressnumber=(textview) findviewbyid(r.id.progress_number); mprogresspercent=(textview) findviewbyid(r.id.progress_percent); mprogressmessage=(textview) findviewbyid(r.id.progress_message); // layoutinflater inflater = layoutinflater.from(getcontext()); mviewupdatehandler = new handler() { @override public void handlemessage(message msg) { // todo auto-generated method stub super .handlemessage(msg); int progress = mprogress.getprogress(); int max = mprogress.getmax(); double dprogress = ( double )progress/( double )( 1024 * 1024 ); double dmax = ( double )max/( double )( 1024 * 1024 ); if (mprogressnumberformat != null ) { string format = mprogressnumberformat; mprogressnumber.settext(string.format(format, dprogress, dmax)); } else { mprogressnumber.settext( "" ); } if (mprogresspercentformat != null ) { double percent = ( double ) progress / ( double ) max; spannablestring tmp = new spannablestring(mprogresspercentformat.format(percent)); tmp.setspan( new stylespan(android.graphics.typeface.bold), 0 , tmp.length(), spannable.span_exclusive_exclusive); mprogresspercent.settext(tmp); } else { mprogresspercent.settext( "" ); } } }; // view view = inflater.inflate(r.layout.common_progress_dialog, null); // mprogress = (progressbar) view.findviewbyid(r.id.progress); // mprogressnumber = (textview) view.findviewbyid(r.id.progress_number); // mprogresspercent = (textview) view.findviewbyid(r.id.progress_percent); // setview(view); //mprogress.setmax(100); onprogresschanged(); if (mmessage != null ) { setmessage(mmessage); } if (mmax > 0 ) { setmax(mmax); } if (mprogressval > 0 ) { setprogress(mprogressval); } } private void initformats() { mprogressnumberformat = "%1.2fm/%2.2fm" ; mprogresspercentformat = numberformat.getpercentinstance(); mprogresspercentformat.setmaximumfractiondigits( 0 ); } private void onprogresschanged() { mviewupdatehandler.sendemptymessage( 0 ); } public void setprogressstyle( int style) { //mprogressstyle = style; } public int getmax() { if (mprogress != null ) { return mprogress.getmax(); } return mmax; } public void setmax( int max) { if (mprogress != null ) { mprogress.setmax(max); onprogresschanged(); } else { mmax = max; } } public void setindeterminate( boolean indeterminate) { if (mprogress != null ) { mprogress.setindeterminate(indeterminate); } // else { // mindeterminate = indeterminate; // } } public void setprogress( int value) { if (mhasstarted) { mprogress.setprogress(value); onprogresschanged(); } else { mprogressval = value; } } @override public void setmessage(charsequence message) { // todo auto-generated method stub //super.setmessage(message); if (mprogressmessage!= null ){ mprogressmessage.settext(message); } else { mmessage = message; } } @override protected void onstart() { // todo auto-generated method stub super .onstart(); mhasstarted = true ; } @override protected void onstop() { // todo auto-generated method stub super .onstop(); mhasstarted = false ; } } |
测试程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private void showdialog(){ mdialog = new commonprogressdialog( this ); mdialog.setmessage( "正在下载" ); mdialog.setprogressstyle(progressdialog.style_horizontal); mdialog.setoncancellistener( new oncancellistener() { @override public void oncancel(dialoginterface dialog) { // todo auto-generated method stub //cancel(true); } }); mdialog.show(); mdialog.setmax( 100 * 1024 * 1024 ); mdialog.setprogress( 65 * 1024 * 1024 ); } |