java.lang.Object
↳ android.view.View
↳ android.widget.ProgressBar
直接子類
AbsSeekBar
間接子類
RatingBar, SeekBar
|
在某些操做的進度中的可視指示器,爲用戶呈現操做的進度,還它有一個次要的進度條,用來顯示中間進度,如在流媒體播放的緩衝區的進度。一個進度條也可不肯定其進度。在不肯定模式下,進度條顯示循環動畫。這種模式經常使用於應用程序使用任務的長度是未知的。進度條也就是一個表示運轉的過程,例如發送短信,鏈接網絡等等,表示一個過程正在執行中html
一、android.widget. ProgressBar,繼承自android.view.View 。在android.widget包中。對應對話框ProgressDialog。ProgressBar有兩種展現方式,錶盤形式(普通、小、大)和條形填充形式。在layout定義時,須要經過設施style屬性類設置展現方式。java
ProgressBar的樣式有四種:android
android:progressBarStyle:默認進度條樣式,不肯定模式
網絡
android:progressBarStyleHorizontal:水平進度條樣式
android:progressBarStyleLarge :大號進度條樣式,也是不肯定進度模式
android:progressBarStyleSmall :小號進度條樣式,也是不肯定進度模式
app
2、XML重要屬性ide
android:max-- 這事進度條長度最大值佈局
android:progress--設定度條當前進度值post
android:secondaryProgress--第二進度條進度值動畫
android:progressBarStyle:默認進度條樣式spa
android:progressBarStyleHorizontal:水平樣式
style
=
"?android:attr/progressBarStyleLarge" --- 屬性風格類型--大圓圈,以下圖
style=」?android:attr/progressBarStyleSmall」
--- 屬性風格類型--小圓圈,以下圖:
style="?android:attr/progressBarStyleHorizontal" --水平進度條 --
以下圖:
幾秒鐘以後自動滾到到以下:
也能夠用下面的形式代替上面的形式的:
1
2
3
|
3、重要方法
getMax():返回這個進度條的範圍的上限
getProgress():返回當前進度值
getSecondaryProgress():返回次要當前進度值
incrementProgressBy(int diff):指定增長的進度--即步長
isIndeterminate():指示進度條是否在不肯定模式下
setIndeterminate(boolean indeterminate):設置不肯定模式下
setVisibility(int v):設置該進度條是否可視
4、重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當進度值改變時引起此事件
接下來看案例:
1.定義一個佈局文件progressbar.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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
ScrollView
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"match_parent"
>
<
LinearLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TextView
android:id
=
"@+id/startText"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"垂直的----標題上面也有一個進度條哦"
android:textColor
=
"#CD0000"
android:background
=
"#BC8F8F"
/>
<!-- style=」?android:attr/progressBarStyleLarge」大圓圈 -->
<
ProgressBar
android:id
=
"@+id/progtessBer_btn_id1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
style
=
"?android:attr/progressBarStyleLarge"
/>
<!-- style=」?android:attr/progressBarStyleSmall」小圓圈 -->
<
ProgressBar
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
style
=
"?android:attr/progressBarStyleSmall"
android:layout_gravity
=
"center_horizontal"
/>
<
TextView
android:id
=
"@+id/startText1"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"水平的"
android:textColor
=
"#aaaaaa"
/>
<!-- style="?android:attr/progressBarStyleHorizontal" 水平進度條 -->
<
ProgressBar
android:id
=
"@+id/progtessBer_btn_id2"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
style
=
"?android:attr/progressBarStyleHorizontal"
/>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/progress_text"
/>
</
LinearLayout
>
</
ScrollView
>
|
2.以後定義java文件:ProgressBarDemo.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
|
package
com.dream.app.start.first.prograssbar;
import
com.dream.app.start.MenuDemo;
import
com.dream.app.start.R;
import
com.dream.app.start.R.id;
import
com.dream.app.start.R.layout;
import
com.dream.app.start.utils.PublicClass;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.os.Handler;
import
android.text.method.ScrollingMovementMethod;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.Window;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ArrayAdapter;
import
android.widget.Button;
import
android.widget.ListView;
import
android.widget.ProgressBar;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
ProgressBarDemo
extends
PublicClass {
private
ProgressBar progressbar,progressbar_1;
Button btn1,btn2;
private
int
prostatus=
0
;
//建立一個handler對象
private
Handler handler=
new
Handler();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
//在標題條裏放置進度條。請求窗口特點風格,這裏設置成不明確的進度風格
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
//設置窗口進度條特性風格
// requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progressbar);
//設置標題欄中的不明確的進度條是否能夠顯示,當你須要表示處理中的時候設置爲True,處理完畢後設置爲false
setProgressBarIndeterminateVisibility(
true
);
//設置進度條進度值,要乘以100的
// setProgress(60*100);
// setSecondaryProgress(80*100);
btn2=(Button)findViewById(R.id.button_cancel);
// btn2.setOnClickListener(onClick);
progressbar=(ProgressBar)findViewById(R.id.progtessBer_btn_id2);
progressbar_1=(ProgressBar)findViewById(R.id.progtessBer_btn_id1);
//設置進度條的最大值
progressbar.setMax(
100000
);
progressbar_1.setMax(
100000
);
//新開啓一個進程
new
Thread(
new
Runnable() {
@Override
public
void
run() {
// 循環1000次,不斷地更新prostatus狀態值
while
(prostatus++<
100000
) {
//將一個Runnable對象添加到消息隊列中去
//而且當執行該對象的時候,執行run
handler.post(
new
Runnable() {
@Override
public
void
run() {
//從新設置進度條當前的值
progressbar.setProgress(prostatus);
progressbar_1.setProgress(prostatus);
}
});
}
}
}).start();
}
//toast方法
private
void
toastshow(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
|
運行效果以下:
二:用圖片實現滾動效果:
1.添加圖片到drawable下
2.自定義圖片資源文件iamge_progress.xml
1
2
3
4
5
6
7
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
animated-rotate
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:drawable
=
"@drawable/image_progress"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
/>
|
3.定義佈局文件,progress.xml
1
2
3
4
5
6
7
8
9
10
11
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:gravity
=
"center"
>
<
ProgressBar
android:indeterminateDrawable
=
"@drawable/drawable_progress"
android:layout_height
=
"100dp"
android:layout_width
=
"100dp"
/>
</
LinearLayout
>
|
運行效果以下:
三》自定義漸變色進度條:
定義drawable資源文件color_progressbar.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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
layer-list
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:id
=
"@android:id/background"
>
<
shape
>
<
corners
android:radius
=
"5dip"
/>
<
gradient
android:startColor
=
"#ff9d9e9d"
android:centerColor
=
"#ff5a5d5a"
android:centerY
=
"0.75"
android:endColor
=
"#ff747674"
android:angle
=
"270"
/>
</
shape
>
</
item
>
<
item
android:id
=
"@android:id/secondaryProgress"
>
<
clip
>
<
shape
>
<
corners
android:radius
=
"5dip"
/>
<
gradient
android:startColor
=
"#80ffd300"
android:centerColor
=
"#80ffb600"
android:centerY
=
"0.75"
android:endColor
=
"#a0ffcb00"
android:angle
=
"270"
/>
</
shape
>
</
clip
>
</
item
>
<
item
android:id
=
"@android:id/progress"
>
<
clip
>
<
shape
>
<
corners
android:radius
=
"5dip"
/>
<
gradient
android:startColor
=
"#FF3030"
android:endColor
=
"#AEEEEE"
android:angle
=
"270"
/>
</
shape
>
</
clip
>
</
item
>
</
layer-list
>
|
2.定義對應的不佈局文件:progressbar.xml在此文件中引用咱們定義的drawable資源配置文件
1
2
3
4
5
6
7
8
9
10
11
12
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:gravity
=
"center"
>
<
ProgressBar
android:id
=
"@+id/color_progressBar"
android:indeterminateDrawable
=
"@drawable/color_progress"
android:layout_height
=
"wrap_content"
android:layout_width
=
"match_parent"
/>
</
LinearLayout
>
|
或者在代碼中給進度條設置自定義資源文件:
效果以下:
四:自定義progressbar顏色:
1.定義一個圖片資源文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
rotate
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fromDegrees
=
"0"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:toDegrees
=
"360"
>
<
shape
android:innerRadiusRatio
=
"3"
android:shape
=
"ring"
android:thicknessRatio
=
"8"
android:useLevel
=
"false"
>
<
gradient
android:centerColor
=
"#FFFFFF"
android:centerY
=
"0.50"
android:endColor
=
"#FFFF00"
android:startColor
=
"#000000"
android:type
=
"sweep"
android:useLevel
=
"false"
/>
</
shape
>
</
rotate
>
|
2.定義佈局文件:
1
2
3
4
5
|
<
ProgressBar
android:id
=
"@+id/color_progressBar2"
android:indeterminateDrawable
=
"@drawable/color_progress2"
android:layout_height
=
"wrap_content"
android:layout_width
=
"wrap_content"
/>
|
3.效果: