Android ProgressBar組件使用教程
1. 前言
進(jìn)度條是UI界面中一種非常實用的組件,通常用于向用戶顯示某個耗時操作完成的百分比,進(jìn)度條可以動態(tài)的顯示進(jìn)度,因為避免長時間地執(zhí)行某個耗時操作時,讓用戶感覺程序失去了響應(yīng),從而更好地提高用戶界面的友好性。
進(jìn)度條展示有兩種:水平進(jìn)度條 和 環(huán)形進(jìn)度條 ,下文分別詳細(xì)介紹。首先我們來看下進(jìn)度條的相關(guān)屬性介紹。
2. ProgressBar屬性介紹
2.1 XML屬性

這里的andorid:progressDrawable用于指定進(jìn)度條的軌道的繪制形式,該屬性可以指定為一個LayerDrawble對象的引用(該對象可以在XML文件中使用<layer-list>元素來進(jìn)行配置)。
android:indeterminateBehavior
定義當(dāng)進(jìn)度達(dá)到最大時,不確定模式的表現(xiàn);該值必須為repeat或者cycle,repeat表示進(jìn)度從0重新開始;cycle表示進(jìn)度保持當(dāng)前值,并且回到0
android:indeterminateDuration=“500”,每轉(zhuǎn)一圈的時間,ms
2.2 API屬性
當(dāng)然ProgressBar也提供如下方法來操作進(jìn)度條:
setProgress(int) //設(shè)置第一進(jìn)度
setSecondaryProgress(int) //設(shè)置第二進(jìn)度
getProgress() //獲取第一進(jìn)度
getSecondaryProgress() //獲取第二進(jìn)度
incrementProgressBy(int) //增加或減少第一進(jìn)度, 當(dāng)參數(shù)為正數(shù)時,進(jìn)度條增加,當(dāng)參數(shù)為負(fù)數(shù)時,進(jìn)度條減少
incrementSecondaryProgressBy(int) //增加或減少第二進(jìn)度
getMax() //獲取最大進(jìn)度
3. 水平進(jìn)度條
在xml中引用有兩種方式:
A為 style="?android:attr/progressBarStyleHorizontal"
B為 style="@android:style/Widget.ProgressBar.Horizontal"
查看B的源碼,相關(guān)屬性為:
<style name="Widget.ProgressBar.Horizontal">
<item name="indeterminateOnly">false</item>
<item name="progressDrawable">@drawable/progress_horizontal</item>
<item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
<item name="minHeight">20dip</item>
<item name="maxHeight">20dip</item>
<item name="mirrorForRtl">true</item>
</style>上文中提到的progressDrawable就是水平進(jìn)度條軌跡的顯示Drawable。我們繼續(xù)去看下progress_horizontal 的源碼
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 進(jìn)度條的背景色-->
<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>
<!-- 緩沖進(jìn)度條的背景色-->
<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>
<!-- 下載過程中進(jìn)度條的顏色-->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>上述代碼就是一個 LayerDrawble圖片,它是層次化的Drawable合集, 根元素為<layer-list> ,每一個item就是一個shape圖片,最后一個item顯示在最上層。
4. 圓形進(jìn)度條
系統(tǒng)自帶的圓形進(jìn)度條,都是一直轉(zhuǎn)圈狀態(tài),為不精確顯示進(jìn)度 默認(rèn)android:indeterminate屬性值為true 。

我們進(jìn)去 android:style/Widget.ProgressBar.Large源碼看下
<style name="Widget.ProgressBar.Large">
<item name="indeterminateDrawable">@drawable/progress_large_white</item>
<item name="minWidth">76dip</item>
<item name="maxWidth">76dip</item>
<item name="minHeight">76dip</item>
<item name="maxHeight">76dip</item>
</style>就是一個indeterminateDrawable ,進(jìn)去再看下代碼:
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_white_76"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100" />看看 spinner_white_76 這個圖片:

是一個 rotate 動畫效果。
1. 我們來修改一下系統(tǒng)圓形進(jìn)度條的顏色,修改屬性為:android:indeterminateTint="#ff0000"
源碼注釋:Tint to apply to the indeterminate progress indicator 翻譯:就是給進(jìn)度條著色
代碼:
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:indeterminateTint="#ff0000"/>沒加這句話是默認(rèn)灰色圓形進(jìn)度條, 加了之后就變成了紅色圓形進(jìn)度條。
2. 自定義轉(zhuǎn)圈動畫
<ProgressBar
android:id="@+id/progressbar2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:indeterminateDrawable="@drawable/bg_loading"/>bg_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:drawable="@drawable/loading"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="359.0"
android:repeatMode="reverse"/>
</item>
</layer-list>loading.xml :

5. 實例演示
我們來寫一個水平進(jìn)度條,模擬下載任務(wù)進(jìn)度過程:
public class MainActivity extends AppCompatActivity {
private ProgressBar horizontalProgress;
private ProgressBar circleProgress;
private Button startBtn;
private TextView mTextView;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if(msg.what >= 100) {
horizontalProgress.setProgress(100);
mTextView.setText("下載完成");
removeCallbacksAndMessages(null);
return;
}
int progress = msg.what;
horizontalProgress.setProgress(progress);
mTextView.setText("下載進(jìn)度:" + progress + "%");
Message message = Message.obtain();
int temp = progress + 4;
message.what = temp;
mHandler.sendMessageDelayed(message, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontalProgress = findViewById(R.id.progressbar1);
mTextView = findViewById(R.id.tv_progress);
circleProgress = findViewById(R.id.progressbar2);
startBtn = findViewById(R.id.start_download);
}
@Override
protected void onResume() {
super.onResume();
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = Message.obtain();
msg.what = 1;
mHandler.sendMessage(msg);
startBtn.setClickable(false);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}布局文件activity_main.xml
<?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="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_progress"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="水平進(jìn)度條"
android:textSize="20sp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/progressbar1"
android:layout_height="15dp"
android:layout_width="match_parent"
android:progress="0"
android:max="100"/>
<TextView
android:id="@+id/tv_progress2"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:text="圓形進(jìn)度條"
android:textSize="20sp" />
<ProgressBar
android:id="@+id/progressbar2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:indeterminateDrawable="@drawable/bg_loading"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/start_download"
android:textSize="20sp"
android:text="開始下載"/>演示效果圖:

到此這篇關(guān)于Android ProgressBar組件使用教程的文章就介紹到這了,更多相關(guān)Android ProgressBar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android ListView實現(xiàn)簡單列表功能
這篇文章主要為大家詳細(xì)介紹了Android ListView實現(xiàn)簡單列表功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android 自定義view和屬性動畫實現(xiàn)充電進(jìn)度條效果
近期項目中需要使用到一種類似手機(jī)電池充電進(jìn)度的動畫效果,以前沒學(xué)屬性動畫的時候,是用圖片+定時器的方式來完成的,下面給大家分享android自定義view和屬性動畫實現(xiàn)充電進(jìn)度條2016-12-12
Android移動開發(fā)recycleView的頁面點擊跳轉(zhuǎn)設(shè)計實現(xiàn)
這篇文章主要介紹了Android移動開發(fā)recycleView的頁面點擊跳轉(zhuǎn)設(shè)計實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
在Android中使用Anntation來代替ENUM的方法
本篇文章主要介紹了在Android中使用Anntation來代替ENUM的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
android 通過MediaRecorder實現(xiàn)簡單的錄音示例
本篇文章中主要介紹了android 通過MediaRecorder實現(xiàn)簡單的錄音示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
android開發(fā)中ListView與Adapter使用要點介紹
項目用到ListView,由于要用到 ImageView ,圖片源不是在資源里面的,沒法使用資源 ID,因此無法直接使用SimpleAdapter,要自己寫一個Adapter。 在使用ListView和Adapter需要注意以下幾點2013-06-06
如何利用Flutter實現(xiàn)酷狗流暢Tabbar效果
這篇文章主要給大家介紹了關(guān)于如何利用Flutter實現(xiàn)酷狗流暢Tabbar效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02
Android使用Notification在狀態(tài)欄上顯示通知
這篇文章主要為大家詳細(xì)介紹了Android使用Notification在狀態(tài)欄上顯示通知,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android軟鍵盤顯示模式及打開和關(guān)閉方式(推薦)
這篇文章主要介紹了Android軟鍵盤顯示模式及打開和關(guān)閉方式(推薦),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Android編程實現(xiàn)獲取當(dāng)前連接wifi名字的方法
這篇文章主要介紹了Android編程實現(xiàn)獲取當(dāng)前連接wifi名字的方法,涉及Android針對WiFi屬性操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11

