ProgressBar进度条,分为旋转进度条和水平进度条,进度条的样式根据需要自定义,这篇文章主要介绍了Android ProgressBar进度条使用方法,感兴趣的小伙伴们可以参考一下
正文
Android ProgressBar进度条使用详解
progressbar,分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案例大多都是通过button点击增加、减少进度值,使用方法incrementprogressby(int),最简单的做法是在xml布局文件中放置progressbar空间,然后再mainactivity中触发事件后执行incrementprogressby(int),代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="cn.teachcourse.www.mainactivity"android:orientation="vertical"android:gravity="center"android:id="@+id/onclick_ll" ><!--点击linearlayout控件,改变progressbar进度--><progressbarandroid:id="@+id/progressbar_horizontal"style="?android:attr/progressbarstylehorizontal"android:layout_width="match_parent"android:layout_height="15dp"android:layout_margintop="28dp"/></linearlayout> |
在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
|
public class mainactivity extends actionbaractivity implements view.onclicklistener{private progressbar pb_large;private progressbar pb_normal;private progressbar pb_small;private progressbar pb_horizontal;private int readed;private int progressvalue;private linearlayout onclick_ll;@overrideprotected void oncreate(bundle savedinstancestate) {super.oncreate(savedinstancestate);setcontentview(r.layout.activity_main);initview();drawable drawable = getresources().getdrawable(r.drawable.progress_bar_states);pb_horizontal.setprogressdrawable(drawable);//设置水平滚动条的样式pb_horizontal.setmax(100);//设置进度条的最大值onclick_ll.setonclicklistener(this);//linearlayout添加监视器}private void initview() {pb_horizontal = (progressbar) findviewbyid(r.id.progressbar_horizontal);onclick_ll=(linearlayout)findviewbyid(r.id.onclick_ll);}@overridepublic void onclick(view v) {pb_horizontal.incrementprogressby(5);if(pb_horizontal.getprogress()==pb_horizontal.getmax()){toast.maketext(this, "进度条已经达到最大值,进度条消失", toast.length_long).show();pb_horizontal.setprogress(0); //当达到最大之后,进度值归0}}} |
我们这里展示的是progressbar读取文件字节流后,展示的读取进度条,上面的简单演示就不说了。
效果图:progressbar读取文件字节流演示

这里展示文件读取字节流过程中,progressbar进度变化情况,当读取完成,进度条刚好满,这在我们实际项目开发中,经常需要使用到,如何成为我写这篇文章的原因。
布局文件progress_horizontal_read_data.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
|
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="cn.teachcourse.www.mainactivity"android:orientation="vertical"android:gravity="center" ><textviewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/read_data_tv"android:text="@string/read_info"android:scrollbars="vertical"android:layout_marginleft="10dp"android:layout_marginright="10dp"android:linespacingextra="10dp"/><progressbarandroid:id="@+id/progressbar_horizontal_read_data"style="?android:attr/progressbarstylehorizontal"android:layout_width="match_parent"android:layout_height="15dp"android:layout_margintop="28dp"/></linearlayout> |
java代码progressbaractivity.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
|
public class progressbaractivity extends actionbaractivity {private progressbar pb_horizontal;private int readed;private int progressvalue;private textview mreadprogress_tv;private string mdata;private stringbuffer sb;//每次读取到的字节数据存储int length;//标记文件大小private thread thread;//读取文件线程@overrideprotected void oncreate(bundle savedinstancestate) {super.oncreate(savedinstancestate);setcontentview(r.layout.progress_horizontal_read_data);initview();drawable drawable = getresources().getdrawable(r.drawable.progress_bar_states);string path = environment.getexternalstoragedirectory()+ "/progressmonitor.txt";// progressmonitor.txt是我存到sdcard中的一个文件,可以自定义文件名大小位置final file file = new file(path);if(file.exists()){length=(int) file.length();pb_horizontal.setmax(length);//设置进度条最大值pb_horizontal.setprogressdrawable(drawable);//自定义进度条样式}//启动一个线程读取文件内容thread=new thread() {@overridepublic void run() {readfromfile(file.getabsolutepath());}};thread.start();}handler handler = new handler() {@overridepublic void handlemessage(message msg) {switch (msg.what) {case 110:progressvalue += msg.arg1;pb_horizontal.setprogress(progressvalue);mreadprogress_tv.settext(sb.tostring());if(progressvalue==length){toast.maketext(progressbaractivity.this, "文件读取完成", toast.length_long).show();}log.d("progressvalue-------------->", progressvalue + "");break;}}};public void readfromfile(string path) {fileinputstream fis;try {fis = new fileinputstream(path);datainputstream dis = new datainputstream(fis);sb=new stringbuffer();byte b[] = new byte[10];// 每次读取1字节while ((readed = dis.read(b)) != -1) {message msg = new message();msg.arg1 = readed;msg.what = 110;mdata=new string(b,0,readed);sb.append(mdata);handler.sendmessage(msg);try {thread.sleep(1000);} catch (exception e) {// todo auto-generated catch blocke.printstacktrace();}}dis.close();fis.close();} catch (exception e) {e.printstacktrace();}}private void initview() {pb_horizontal = (progressbar) findviewbyid(r.id.progressbar_horizontal_read_data);mreadprogress_tv=(textview)findviewbyid(r.id.read_data_tv);}} |
这里使用handler消息处理,每次读取到数据后更新进度条,将读取的字节数据放置在arg1中,在handler的handlemessage方法中设置进度值,同时刷新textview显示的内容,这里难就难在如何获取每次读取的字节数,然后刷新进度条,在字节流的学习中,我们可以自定义每次读取字节大小,file类中可以获取到文件的总大小,最终我们得到上面图片中的效果。在这个例子里面,我们可以将读取手机卡的文件,换成下载文件,传输文件同样适应。
以上就是本文的全部内容,希望大家对android软件编程有所帮助。

发表评论