本文实例为大家分享了android实现节点进度条效果展示的具体代码,供大家参考,具体内容如
代码:
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
|
package utils.android.view.lxz; import java.util.ArrayList; import java.util.List; import com.community.custom.android.R; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; /** * 2015-1-24 * 小林爱 * */ public class NodeProgressBar extends View implements Runnable { private float dn=1f; DisplayMetrics metrics; { metrics= new DisplayMetrics(); ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); dn= 1 * 3 /metrics.density; } /**读取空进度条图片*/ private BitmapDrawable db_empty; /**View宽度*/ private int viewWidth; /**View高度*/ private int viewHeight; /** * 白色空心圆图片集合 * */ private List<BitmapDrawable> list_whitecircle ; /** * 蓝色实心圆图片集合 * */ private List<BitmapDrawable> list_bluecircle ; /**蓝色进度条*/ private BitmapDrawable db_blue; /**进度比值 控制蓝色进度条*/ private double ratio = 0 ; /**节点名称*/ private String[] textArr= new String[]{ "等待服务" , "服务中" , "等待评价" , "成功" }; /**节点数*/ private int count=textArr.length;; /**当前节点进度*/ private int index = 0 ; /**X轴对称 偏移值*/ private int offX=( int ) ( 50 /dn); /**Y轴 偏移值*/ private int offY=( int ) ( 70 /dn); /**文字与节点 偏移值*/ private int offTextY=( int ) ( 65 /dn); /**白色空心圆偏移值*/ private int offWhiteCirle_y=( int ) (- 3 /dn); /**白色空心进度条偏移值*/ private int offWhiteRect_y=( int ) (- 2 /dn); /**白色空心进度条偏移值*/ private BitmapDrawable db_blue_half_circle; /**创建一只新画笔*/ private Paint paint = new Paint(); /**白色空心圆半径*/ private int r_white = ( int ) ( 76 /dn); /**蓝色空心圆半径*/ private int r_blue = ( int ) ( 56 /dn); /**白色进度条高度*/ private int whiteProgressHeight = ( int ) ( 30 /dn); /**蓝色进度条高度*/ private int blueProgressHeight = ( int ) ( 16 /dn); /**文本颜色j*/ private String textColor= "#46A3FF" ; /**文本未激活颜色*/ private String textColorNotEnabled= "#7E7E81" ; // /**View 背景颜色*/ // private String bgColor="#e0000000"; /**文本框大小*/ private int textSize=( int ) ( 32 /dn); /**白色空心进度条宽度*/ private int maxProgressWidth; /**半圆蓝色进度条宽度*/ private int half_blueWidth = ( int ) ( 12 /dn); public String[] getTextArr() { return textArr; } public void setTextArr(String[] textArr) { this .textArr = textArr; count=textArr.length;; init(); this .post( new Runnable() { @Override public void run() { //读取View宽度 viewWidth = NodeProgressBar. this .getWidth(); //读取View高度 viewHeight = NodeProgressBar. this .getHeight(); // 进度条宽度计算 maxProgressWidth = viewWidth - r_white-offX* 2 ; //绘制 invalidate(); } }); } @SuppressWarnings ( "deprecation" ) public NodeProgressBar(Context context) { super (context); init(); } public NodeProgressBar(Context context, AttributeSet attrs) { super (context, attrs); init(); } /**控制蓝色进度条*/ public void setProgressOnly( int i) { ratio = i / 100d; invalidate(); } /**以节点数来空值进度条 至少大于1*/ public void setProgressByNode( final double d) { this .post( new Runnable() { @Override public void run() { // TODO Auto-generated method stub int progress; if (d== 1 ) { progress= 1 ; } else { progress= ( int ) ( (100d/((count- 1 )* 1 .0d))*(d- 1 )); } setProgressAndIndex(progress); } }); } /**控制蓝色进度条并且对节点染色*/ public void setProgressAndIndex( int i){ if (i== 0 ){ index= 0 ; ratio= 0 ; invalidate(); return ; } //获得相对进度条长度 int adbProgress = maxProgressWidth-(count- 1 )*r_white; //得到每一个节点所需进度值 int k= 100 /(count- 1 ); //计算当前进度需要染色的节点个数 index= 1 +i/k; if (index!=count) { //获得节点磁力比率 double wh= 1 .0d*(r_white/ 2 )/( double )maxProgressWidth; //计算蓝色进度条和染色节点宽度 ratio=i% 100 == 0 ?ratio= 1 :wh+wh* 2 *(index- 1 )+ 1 .0d*(( double )adbProgress/( double )maxProgressWidth)*(i/100d); } else { //设置进度条为满 ratio= 1 ; } invalidate(); } /** * 初始化图片资源,和基础数值 * */ @SuppressWarnings ( "deprecation" ) private void init() { metrics= new DisplayMetrics(); ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); Log.d( "lxz" , "" +metrics.density); dn= 1 * 3 /metrics.density; list_whitecircle= new ArrayList<BitmapDrawable>(); list_bluecircle= new ArrayList<BitmapDrawable>(); //根据节点个数 初始化空心圆和实心圆 for ( int i = 0 ; i < count; i++) { BitmapDrawable drawable1 = new BitmapDrawable( BitmapFactory.decodeResource(getResources(), R.drawable.progress_white_circle)); list_whitecircle.add(drawable1); BitmapDrawable drawable2 = new BitmapDrawable( BitmapFactory.decodeResource(getResources(), R.drawable.progress_blue_circle)); list_bluecircle.add(drawable2); } //初始化蓝色小半圆 db_blue_half_circle = new BitmapDrawable(BitmapFactory.decodeResource( getResources(), R.drawable.progress_blue_half_circle)); //初始化空心进度条 db_empty = new BitmapDrawable(BitmapFactory.decodeResource( getResources(), R.drawable.progress_whtie_groove)); //初始化 db_blue = new BitmapDrawable(BitmapFactory.decodeResource( getResources(), R.drawable.progress_blue_groove)); //UI线程初始化数值 this .post( this ); } @Override public void run() { //读取View宽度 viewWidth = NodeProgressBar. this .getWidth(); //读取View高度 viewHeight = NodeProgressBar. this .getHeight(); // 进度条宽度计算 maxProgressWidth = viewWidth - r_white-offX* 2 ; //绘制 invalidate(); } @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub super .draw(canvas); //获得X轴偏转值 int offAbs_x=( int )((viewWidth-maxProgressWidth)/ 2 .0d); //获得X轴偏转值 // canvas.drawColor(Color.parseColor(bgColor)); canvas.drawColor(Color.TRANSPARENT); //绘制空心进度条 drawRect(canvas, db_empty, viewWidth / 2 , r_white / 2 +offY+offWhiteRect_y, maxProgressWidth,whiteProgressHeight); paint.setTextSize(textSize); paint.setFakeBoldText( true ); //绘制白色空心园 for ( int i = 0 , j = list_whitecircle.size(); i < j; i++) { BitmapDrawable db_whitecircle = list_whitecircle.get(i); int x=maxProgressWidth / (count - 1 )* i+offAbs_x; int y=r_white/ 2 + offWhiteCirle_y+offY; drawCircle(canvas, db_whitecircle,x, y, r_white); String str=textArr[i]; if (i<index) { paint.setColor(Color.parseColor(textColor)); } else { paint.setColor(Color.parseColor(textColorNotEnabled)); } float textWidht = paint.measureText(str); canvas.drawText(str, x-textWidht/ 2 , y+offTextY, paint); } //绘制蓝色进度条 drawRect(canvas, db_blue, ( int ) ((maxProgressWidth * ratio) / 2 )+offAbs_x , r_white / 2 +offY, ( int ) (maxProgressWidth * ratio),blueProgressHeight); //绘制蓝色小半圆 if (ratio > 0 ) { drawRect(canvas, db_blue_half_circle,( int ) ((maxProgressWidth * ratio) / 2 ) + ( int ) (maxProgressWidth * ratio) / 2 + half_blueWidth / 2 +offAbs_x, r_white / 2 +offY, half_blueWidth, blueProgressHeight); } //绘制蓝色圆 for ( int i = 0 , j = index; i < j; i++) { BitmapDrawable db_bluecircle = list_bluecircle.get(i); drawCircle(canvas, db_bluecircle, maxProgressWidth / (count - 1 ) * i+offAbs_x, r_white/ 2 +offY, r_blue); } } /**传统矩形坐标方法*/ public void drawRect(Canvas canvas, Drawable d, int x, int y, int width, int height) { d.setBounds(x - width / 2 , y - height / 2 , x + width / 2 , y + height / 2 ); d.draw(canvas); } /**传统圆点坐标计算方法*/ public void drawCircle(Canvas canvas, Drawable d, int x, int y, int r) { d.setBounds(x - r / 2 , y - r / 2 , x + r / 2 , y + r / 2 ); d.draw(canvas); } /**绘制文字 传统坐标计算方法*/ public void drawText(Canvas canvas,Paint paint, String str, int x, int y, int w, int h) { canvas.drawText(str, x-w/ 2 , y-h/ 2 , x+w, y+h, paint); } public double getRatio() { return ratio; } public void setRatio( double ratio) { this .ratio = ratio; } public int getCount() { return count; } public void setCount( int count) { this .count = count; } public int getIndex() { return index; } public void setIndex( int index) { this .index = index; } } |
源码下载:节点进度条
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/b275518834/article/details/43148731