標籤:android progressbar
package com.gc.progressbar;/* * 1、ProgressBar組件也是一組重要的組件,ProgressBar本身代表了進度條組件, * 它還派生了兩個常用的組件:SeekBar和RatingBar。 * 2、進度條的功能與用法: * 進度條通常用於向使用者顯示某個耗時操作完成的百分比,進度條可以動態地顯示進度 * 因此避免長時間地執行某個耗時操作時,讓使用者感覺程式失去了響應。 * Android支援如下幾種風格的進度條,通過style屬性可以為ProgressBar指定風格 * @android:style/Widget.ProgressBar.Horizontal-----水平進度條 * @android:style/Widget.ProgressBar.Inverse--------普通大小的環形進度條 * @android:style/Widget.ProgressBar.Large----------大環形進度條 * @android:style/Widget.ProgressBar.Large.Inverse---大環形進度條 * @android:style/Widget.ProgressBar.Small-----------小環形進度條 * @android:style/Widget.ProgressBar.Small.Inverse---小環形進度條 * 3、ProgressBar常用的XML屬性: * android:max-------------------設定該進度條的最大值 * android:progress--------------設定該進度條的已完成進度值 * android:progressDrawable------設定該進度條的軌道對應的Drawable對象 * android:indeterminate---------該屬性設為true,設定進度條不精確顯示進度 * android:indeterminateDrawable-設定繪製不顯示進度的進度條的Drawable對象 * android:indeterminateDuration-設定不精確顯示進度的期間 * android:progressDrawable用於指定進度條的軌道的繪製形式,該屬性可指定為 * 一個LayerDrawable對象(該對象可通過在XML檔案中用<layer-list>元素進行配置 )的引用 * 4、ProgressBar提供了下面兩個方法來操作進度 * setProgress(int) ---設定進度的完成百分比 * incrementProgressBy(int)---設定進度條的進度增加或減少。當參數為正數時進度增加 * ,當參數為負數時進度減少。 */import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.widget.ProgressBar;/** * * @author Android將軍 * */public class MainActivity extends Activity {//該程式類比填充長度為100的數組private int [] data=new int[100];private int hasData=0;//記錄ProgressBar的完成進度private int status=0;private ProgressBar bar,bar2;//建立一個負責更新的進度的HandlerHandler mHandler=new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stub//表明訊息是由該程式發送的if(msg.what==0x111){bar.setProgress(status);bar2.setProgress(status);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bar=(ProgressBar)findViewById(R.id.bar);bar2=(ProgressBar)findViewById(R.id.bar2);//啟動線程來執行任務new Thread(){public void run(){while(status<100){//擷取耗時操作的完成百分比status=doWork();//發送訊息mHandler.sendEmptyMessage(0x111);}}}.start();}//類比一個耗時的操作public int doWork() {// 為數組元素賦值data[hasData++]=(int)(Math.random()*100);try{Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}return hasData;}}
布局檔案:
<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" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- 定義一個大環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large" /> <!-- 定義一個中等大小的環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 定義一個小環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="任務完成的進度" /> <!-- 定義一個水平進度條 --> <ProgressBar android:id="@+id/bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" style="@android:style/Widget.ProgressBar.Horizontal" /> <!-- 定義一個水平進度條,並改變軌道外觀 --> <ProgressBar android:id="@+id/bar2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/my_bar" style="@android:style/Widget.ProgressBar.Horizontal" /></LinearLayout>
轉載請註明出處:http://blog.csdn.net/android_jiangjun/article/details/25555633