Android學習筆記:進度條ProgressBar的使用以及與AsyncTask的配合使用

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   os   使用   sp   java   

ProgressBar時android用於顯示進度的組件。當執行一個比較耗時的操作(如io操作、網路操作等),為了避免介面沒有變化讓使用者體驗降低,提供一個進度條可以讓使用者知道程式還在運行。

一、ProgressBar有如下幾種常見樣式

1、預設進度條的樣式為圓圈(為中等大小的圓圈)

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

注意,當樣式為圓圈時,其進度不停的在旋轉,只是告訴用互程式在執行,無法精確地告訴使用者具體進度。

而且預設顯示就是旋轉的,還無法讓它停下來,只有通過程式讓其隱藏(比如任務處理完成時)。 

如果想要顯示為圓形的,又要控制其停頓和旋轉,需要用自訂的方式,這個不在這裡介紹。

隱藏進度條,很簡單:xxxx.setVisibility(View.GONE);

2、顯示為大的尺寸的圓圈

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

3、顯示為小的尺寸的圓圈

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

可以直接設定 layout_width 和 layout_height為具體的值,來控制圓圈的半徑。

4、顯示為直條形的

<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

對於直條形的樣式,不僅可以讓它向圓形一樣,預設就是進度不停變化;而且還可以精確控制其進度,下面會重點介紹。

二 、直條形進度條

設定為一直在旋轉的樣式

<ProgressBar
android:id="@+id/progress1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
/>

設定indeterminate屬性為true,就可以顯示時進度條一直在變化,該屬性預設是false的。

如果想讓它停止變化,可以通過代碼控制:

ProgressBar pro1 = (ProgressBar) findViewById(R.id.progress1);

pro1.setIndeterminate(false);

如果想讓它停止時,進度顯示完成,可以如下代碼:

pro1.setProgress(100);  

注意進度條的預設最大大小是100,可以通過setMax來設定自己需要的值。

三、精確控制進度(和AsyncTask結合)

代碼如下

public void startProgress(View view) {final ProgressBar pro1 = (ProgressBar) findViewById(R.id.progress1);new AsyncTask<Integer, Integer, Integer>() {@Overrideprotected Integer doInBackground(Integer... params) {for (int i = 1; i <= 100; i++)doSomethingTask(i);return 100;}private void doSomethingTask(int num) {try {Thread.sleep(50);publishProgress(num);} catch (InterruptedException e) {e.printStackTrace();}}@Overrideprotected void onPostExecute(Integer result) {TextView resultView = (TextView) findViewById(R.id.resultText);resultView.setVisibility(View.VISIBLE);resultView.setText("the result is " + result);super.onPostExecute(result);}@Overrideprotected void onProgressUpdate(Integer... values) {pro1.setProgress(values[0]);super.onProgressUpdate(values);}}.execute(1);}

在AsyncTask的doInBackground方法中執行背景工作(實際上是在另外線程中執行,如io操作、網路操作。在這個方法中不能操作介面組件),如果想讓進度條顯示中間的進度,就可以在背景工作的執行過程中調用publishProgress方法發送進度。而執行publishProgress方法,會觸發onProgressUpdate方法(該方法在主線程中進行,可以在該方法中更新介面組件,這裡更新的是進度條的進度)。當doInBackground方法執行完畢後,會觸發onPostExecute方法(該方法在主線程中進行,可以在該方法中更新介面組件),可以在該方法中顯示任務完成後的介面,上面例子是在本文view顯示下資訊。

通過上面的代碼和例子,基本上就可以滿足大部分情境。最後提一點的是,ProgressBar的進度只能通過代碼控制,無法讓使用者拖動。如果想要讓使用者能夠拖動進度(比如在視頻或音頻播放介面),可以使用SeekBar組件。

 

Android學習筆記:進度條ProgressBar的使用以及與AsyncTask的配合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.