ProgressBar可以作為一些操作過程中的視覺指示器,可以將操作進度實時的反饋給用戶,有時也會使用第二進度條用以輔助顯示,例如我們在觀看視頻時,第一進度條可以顯示當前播放進度,而第二進度條則用以顯示緩沖進度,更好地提高用戶體驗。
同時,對于某些不確定的情況下,比如網(wǎng)絡(luò)連接時,可以使用轉(zhuǎn)圈的動畫作為一個進度指示器,提示用戶此時正在加載操作。
對于如何使用ProgressBar,API文檔也給了一個代碼樣例:
public class MyActivity extends Activity {
private static final int PROGRESS = 0x1;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.progressbar_activity);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
}
從示例代碼中可以看出,doWork方法屬于耗時操作,因此這里新開了一個線程(PS:耗時操作不能在主線程UI中運行,否則可能會造成ANR,即應(yīng)用程序無響應(yīng)的情況,這里應(yīng)該注意)。對于將耗時操作的進度反饋到UI線程,也有較多方法(后面課程會詳細講解),這里采用了Handler的post方法,將實時操作進度反饋到UI線程中的ProgressBar中。最后,新開了一個線程,不要忘記調(diào)用它的start方法,啟動線程。
在布局文件中引入一個ProgressBar可以使用一個ProgressBar標簽,還需要一些屬性修飾,下面對常用屬性進行一下介紹:
進度條的樣式,Android提供了幾種原生的進度條樣式,可以通過style屬性在布局文件中進行設(shè)置。
這么解釋不夠直觀,下面通過一個小實例來對上面進度條樣式和屬性進行學(xué)習。
布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--水平進度條-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="60" />
<!-- 小進度條-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--大進度條-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Large.Inverse"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
布局文件中定義了六種樣式的進度條,并對第一個進度條設(shè)置了max(最大值)、progress(當前進度)、secondaryProgress(第二進度)等屬性,下面運行實例觀察一下不同樣式進度條的外觀差異:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/9-1.png" alt="這里寫圖片描述" />
除了上述樣式的進度條之外,還有顯示在標題欄上的進度條, 布局代碼中添加了兩個按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_show"
android:text="顯示標題欄進度條"
android:onClick="show"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_dismiss"
android:text="隱藏標題欄進度條"
android:onClick="dismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Activity代碼如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//確定進度的標題欄進度條
requestWindowFeature(Window.FEATURE_PROGRESS);
//不確定進度的標題欄進度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
}
public void show(View view) {
setProgressBarVisibility(true);
setProgress(800);
setProgressBarIndeterminateVisibility(true);
}
public void dismiss(View view) {
setProgressBarVisibility(false);
setProgressBarIndeterminateVisibility(false);
}
}
這里調(diào)用了Activity類的requestWindowFeature方法傳入Window.FEATURE_PROGRESS參數(shù)顯示有進度的標題欄進度條,傳入Window.FEATURE_INDETERMINATE_PROGRESS參數(shù)則顯示不帶進度的標題欄進度條。注意這個方法要在setContentView方法之前調(diào)用,這里涉及到View繪制過程的知識,這里只需要記住這個順序即可,后面還會從原理上講解為什么要放在setContentView的前面。
調(diào)用setProgressBarVisibility方法傳入布爾變量即可決定標題欄進度條的顯示與否,同理setProgressBarIndeterminateVisibility用以決定標題欄不確定進度條的顯示與否。
運行項目實例如下:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/9-2.png" alt="這里寫圖片描述" />
這里需要說明,使用Android Studio作為開發(fā)工具的同學(xué)要注意:項目默認的MainActivity繼承自AppCompatActivity,需改成Activity,同時,在AndroidManifest文件中設(shè)置對應(yīng)的Activity屬性android:theme="@style/Theme.AppCompat.CompactMenu" (有頂部的標題欄的主題),就可以正常顯示了。
上面介紹了標題欄進度條的顯示和隱藏,下面通過一個小例子了解一下進度條是如何更新進度的,我們模擬了一個下載過程。布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="download"
android:text="下載模擬" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載進度如下:"/>
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/probar_download"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Activity.java代碼如下:
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.probar_download);
progressBar.setMax(100);
progressBar.setProgress(20);
}
public void download(View view){
new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.incrementProgressBy(1);
}
}
}.start();
}
}
這里新開了一個線程用于模擬下載過程,并調(diào)用ProgressBar的increaseProgressBy方法,在原來的基礎(chǔ)上每次增加1。除了increaseProgressBy方法,還有如下表所示的常用方法,讀者可以自行測試它們的用法。
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/9-3.png" alt="這里寫圖片描述" />
運行實例如下: http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/9-4.png" alt="這里寫圖片描述" />