Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能示例
本文實(shí)例講述了Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能。分享給大家供大家參考,具體如下:
進(jìn)度條ProgressBar的使用主要有兩種方向;
1.使用官方默認(rèn)樣式
2.使用自定義樣式
先看效果:

詳細(xì)代碼實(shí)現(xiàn)文末給出
關(guān)于系統(tǒng)自帶樣式:
在 style="@android:style 中有許多系統(tǒng)自帶樣式,大家可以更具自身喜好選擇。
如果不選擇 style 系統(tǒng)會(huì)默認(rèn)使用上圖中紅色的樣式。
關(guān)于自定義樣式:
這里我們最好看看源碼 很容易理解
主要分為三個(gè)部分:當(dāng)前進(jìn)度、緩沖進(jìn)度、以及背景 三個(gè)屬性
這里我們通過(guò)在drawable里新建my_bar.xml來(lái)實(shí)現(xiàn)
這里有個(gè)注意點(diǎn) 很多人寫(xiě)了xml后發(fā)現(xiàn) 直接就顯示滿進(jìn)度 而不是緩慢增長(zhǎng)
由于是替換系統(tǒng)自帶樣式,所以id必須與系統(tǒng)保持一致:(如:android:id="@android:id/background")
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--定義軌道背景-->
<item android:id="@android:id/background"
android:drawable="@drawable/no"/>
<!--定義軌道上已完成部分的樣式-->
<item android:id="@android:id/progress"
android:drawable="@drawable/ok"/>
</layer-list>
這里對(duì)比下系統(tǒng)源碼就很好理解了:

這里的模擬方法采用的是線程結(jié)合Handler
由于線程不能直接改變控件屬性 所以需要用Handler來(lái)接受線程發(fā)出的Message
具體方法如下:
public class MainActivity extends Activity {
//記錄ProgressBar的完成進(jìn)度
private int sum1=0,sum2 = 0 ;
ProgressBar bar1,bar2;
//創(chuàng)建一個(gè)負(fù)責(zé)更新進(jìn)度的Handler
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息是本程序發(fā)送的
if (msg.what == 0x111){
bar1.setProgress(sum1);
bar2.setProgress(sum2);
}
}
};
//模擬耗時(shí)
Thread thread = new Thread(){
@Override
public void run() {
while (sum2 < 100){
//bar1獲取完成工作的百分比
if (sum1 > 100){
sum1 = 100;
if (sum2<100){
sum2 += (int) (Math.random()*25);
}else {
sum2 = 100;
thread.stop();
}
sum1=0;
}else {
sum1 = sum1 + (int) (Math.random()*25);
}
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
//更新ProgressBar
mHandler.sendEmptyMessage(0x111);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar1 = (ProgressBar) findViewById(R.id.bar);
bar2 = (ProgressBar) findViewById(R.id.bar2);
thread.start();
}
}
最后在給出布局文件:
<?xml version="1.0" encoding="utf-8" ?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetStart="0dp"
android:background="#9FB6CD">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/toolbar_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--定義一個(gè)大環(huán)型進(jìn)度條-->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Large"/>
<!--定義一個(gè)中等大小環(huán)形進(jìn)度條-->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--定義一個(gè)小進(jìn)度條-->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="任務(wù)完成的進(jìn)度"/>
<!--定義一個(gè)大水平進(jìn)度條-->
<ProgressBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"/>
<!--頂一個(gè)水平進(jìn)度條,并改變軌道外觀-->
<ProgressBar
android:id="@+id/bar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progressDrawable="@drawable/my_bar"
style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android判斷json格式將錯(cuò)誤信息提交給服務(wù)器
- android和服務(wù)器的URLEncodedUtils亂碼編碼問(wèn)題的解決方案
- Android開(kāi)發(fā)實(shí)現(xiàn)的文本折疊點(diǎn)擊展開(kāi)功能示例
- Android開(kāi)發(fā)之SeekBar基本使用及各種美觀樣式示例
- Android開(kāi)發(fā)之自定義星星評(píng)分控件RatingBar用法示例
- Android開(kāi)發(fā)之計(jì)算器GridLayout布局實(shí)現(xiàn)方法示例
- Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能實(shí)例分析
- Android開(kāi)發(fā)之圖片壓縮實(shí)現(xiàn)方法分析
- Android開(kāi)發(fā)實(shí)現(xiàn)自動(dòng)切換文字TextSwitcher功能示例
- android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法
相關(guān)文章
Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法
這篇文章主要介紹了Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法,結(jié)合實(shí)例形式分析了ListView滾動(dòng)刷新與加載的相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Kotlin 創(chuàng)建接口或者抽象類(lèi)的匿名對(duì)象實(shí)例
這篇文章主要介紹了Kotlin 創(chuàng)建接口或者抽象類(lèi)的匿名對(duì)象實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解
這篇文章主要介紹了Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
利用Kotlin開(kāi)發(fā)你的第一個(gè)Android應(yīng)用
Kotlin最近真的是大熱啊,所以下面這篇文章主要給大家介紹了關(guān)于利用Kotlin開(kāi)發(fā)你的第一個(gè)Android應(yīng)用的相關(guān)資料,文中將實(shí)現(xiàn)的步驟介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-10-10
Jetpack Compose圖片組件使用實(shí)例詳細(xì)講解
在Compose中,圖片組件主要有兩種,分別是顯示圖標(biāo)的Icon組件和顯示圖片的Image組件,當(dāng)我們顯示一系列的小圖標(biāo)的時(shí)候,我們可以使用Icon組件,當(dāng)顯示圖片時(shí),我們就用專(zhuān)用的Image組件2023-04-04
android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個(gè)計(jì)時(shí)器,這個(gè)計(jì)時(shí)器稱為Chronometer,我們可以成它為Android的一個(gè)組件,同時(shí)它也具備自己獨(dú)有的方法2013-01-01

