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

這里的andorid:progressDrawable用于指定進(jìn)度條的軌道的繪制形式,該屬性可以指定為一個(gè)LayerDrawble對(duì)象的引用(該對(duì)象可以在XML文件中使用<layer-list>元素來(lái)進(jìn)行配置)。
android:indeterminateBehavior
定義當(dāng)進(jìn)度達(dá)到最大時(shí),不確定模式的表現(xiàn);該值必須為repeat或者cycle,repeat表示進(jìn)度從0重新開(kāi)始;cycle表示進(jìn)度保持當(dāng)前值,并且回到0
android:indeterminateDuration=“500”,每轉(zhuǎn)一圈的時(shí)間,ms
2.2 API屬性
當(dāng)然ProgressBar也提供如下方法來(lái)操作進(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ù)時(shí),進(jìn)度條增加,當(dāng)參數(shù)為負(fù)數(shù)時(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>
<!-- 下載過(guò)程中進(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>上述代碼就是一個(gè) LayerDrawble圖片,它是層次化的Drawable合集, 根元素為<layer-list> ,每一個(gè)item就是一個(gè)shape圖片,最后一個(gè)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>就是一個(gè)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 這個(gè)圖片:

是一個(gè) rotate 動(dòng)畫(huà)效果。
1. 我們來(lái)修改一下系統(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"/>沒(méi)加這句話是默認(rèn)灰色圓形進(jìn)度條, 加了之后就變成了紅色圓形進(jìn)度條。
2. 自定義轉(zhuǎn)圈動(dòng)畫(huà)
<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. 實(shí)例演示
我們來(lái)寫(xiě)一個(gè)水平進(jìn)度條,模擬下載任務(wù)進(jìn)度過(guò)程:
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="開(kāi)始下載"/>演示效果圖:

到此這篇關(guān)于Android ProgressBar組件使用教程的文章就介紹到這了,更多相關(guān)Android ProgressBar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義View實(shí)現(xiàn)公交成軌跡圖
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)公交成軌跡圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Android實(shí)現(xiàn)類似微信的文本輸入框 效果
本文給大家介紹一下微信的文本輸入框是如何實(shí)現(xiàn)的,其實(shí)那只是個(gè)普通的文本框設(shè)了一個(gè)特殊的背景而已。具體微信怎么實(shí)現(xiàn)的,大家可以反編譯下,這里介紹下如何實(shí)現(xiàn)這個(gè)背景2017-05-05
Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來(lái)實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09
解析Android 8.1平臺(tái)SystemUI 導(dǎo)航欄加載流程
這篇文章主要介紹了Android 8.1平臺(tái)SystemUI 導(dǎo)航欄加載流程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼
下面小編就為大家分享一篇Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

