Android開發使用ProgressBar實現進度條功能示例

這篇文章主要介紹了Android開發使用ProgressBar實現進度條功能,結合實例形式分析了Android進度條ProgressBar的具體樣式、佈局與功能實現技巧,需要的朋友可以參考下

本文實例講述了Android開發使用ProgressBar實現進度條功能。分享給大家供大家參考,具體如下:

進度條ProgressBar的使用主要有兩種方向;

1.使用官方默認樣式

2.使用自定義樣式

先看效果:

詳細代碼實現文末給出

關於系統自帶樣式:

style="@android:style 中有許多系統自帶樣式,大家可以更具自身喜好選擇。

如果不選擇 style 系統會默認使用上圖中紅色的樣式。

關於自定義樣式:

這裏我們最好看看源碼 很容易理解

主要分爲三個部分:當前進度、緩衝進度、以及背景 三個屬性

這裏我們通過在drawable裏新建my_bar.xml來實現

這裏有個注意點 很多人寫了xml後發現 直接就顯示滿進度 而不是緩慢增長

由於是替換系統自帶樣式,所以id必須與系統保持一致:(如: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>

這裏對比下系統源碼就很好理解了:

這裏的模擬方法採用的是線程結合Handler

由於線程不能直接改變控件屬性 所以需要用Handler來接受線程發出的Message

具體方法如下:

public class MainActivity extends Activity {
  //記錄ProgressBar的完成進度
  private int sum1=0,sum2 = 0 ;
  ProgressBar bar1,bar2;
  //創建一個負責更新進度的Handler
  Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      //表明消息是本程序發送的
      if (msg.what == 0x111){
        bar1.setProgress(sum1);
        bar2.setProgress(sum2);
      }
    }
  };
  //模擬耗時
  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">
    <!--定義一個大環型進度條-->
    <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="match_parent"
    android:layout_height="wrap_content"
    android:text="任務完成的進度"/>
  <!--定義一個大水平進度條-->
  <ProgressBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
  <!--頂一個水平進度條,並改變軌道外觀-->
  <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>

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧彙總

希望本文所述對大家Android程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章