Android 使用ProgressBar實現進度條


ProgressBar簡介
ProgressBar是進度條組件,通常用於向用戶展示某個耗時操作完成的進度,而不讓用戶感覺是程序失去了響應,從而更好地提升用戶界面的友好型。

課程目標
(1)制定ProgressBar顯示風格
(2)ProgressBar的分類
(3)標題上ProgressBar的設置
(4)ProgressBar的關鍵屬性
(5)ProgressBar的關鍵方法
(6)ProgressBar的基礎使用
(7)自定義ProgressBar樣式

制定ProgressBar顯示風格
style="?android:attr/progressBarStyleLarge" 大環形進度條
style="?android:attr/progressBarStyleSmall" 小環形進度條
style="?android:attr/progressBarStyleHorizontal" 水平進度條

ProgressBar的分類
(1)可以精確顯示進度(可以顯示進度或者百分比)
(2)不可以精確顯示進度(一直轉啊轉,類似於一個過場動畫)
可以精確顯示任務的ProgressBar可以用於顯示下載任務;對於不可以精確顯示進度的ProgressBar的話他對應的任務的事件可能是不可控的,也就是說我可能不知道要過多少時間才能好。

標題欄上的ProgressBar
通過一個案例來顯示如何在標題欄上顯示ProgressBar
通過啟用窗口特征,啟用帶進度和不帶進度的進度條
啟用窗口特征——啟用帶進度的進度條:
requestWindowFeature(Window.FEATURE_PROGRESS);
啟用窗口特征——啟用不帶進度的進度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
啟動好了之后還要把這兩個進度條顯示出來
顯示兩種進度條:
setProgressBarVisibility(true)
setProgressBarIndeterminateVisibility(true);
設置有進度的進度條的刻度(刻度的范圍在0~10000)
setProgress(3000);
注意刻度不能設置成10000,因為這個時候progressBar的刻度已經到了100%,這個時候他會消失,因為進度已經完成了。
(注意:
 requestWindowFeature(Window.FEATURE_PROGRESS);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 這兩句話要放在
 setContentView(R.layout.activity_main);
 之前,不然程序運行會出錯。

 
第一顯示進度(相當於在優酷上看電影的時候當前播放的進度)
第二顯示進度(相當於在優酷上看電影的時候緩沖對應的進度)
最大顯示進度(相當於一部電影的時長對應的長度)
通過第一顯示進度除以最大顯示進度可以得到當前進度的百分比。
ProgressBar的關鍵屬性
android:max = "100"  —— 最大顯示進度
android:progress = "50" —— 第一顯示進度
android:secondaryProgress="80" —— 第二顯示進度
android:indeterminate = "true" —— 設置是否精確顯示
 true表示不精確顯示進度;false表示精確顯示進度

ProgressBar的關鍵方法
(1)setProgress(int) 設置第一進度
(2)setSecondaryProgress(int) 設置第二進度
(3)getProgress() 獲取第一進度
(4)getSecondaryProgress() 獲取的二進度
(5)incrementProgressBy(int) 增加或減少第一進度
(6)incrementSecondaryProgressBy(int) 增加或減少第二進度
(7)getMax() 獲取最大進度

ProgressBar設置一下ProgressBar的以下方法就創建好了ProgressBar。
接下來是設置ProgressDialog,它是一個彈出的對話框(類似於之前學的DatePickerDialog和TimePickerDialog)
新建ProgressDialog對象:
progressDialog = new ProgressDialog(MainActivity.this);
/* 設定頁面顯示風格的屬性 */
設置顯示風格:
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
設置標題:
prodialog.setTitle("ProgressDialog的標題");
設置對話框里的文字信息:
prodialog.setMessage("ProgressDialog里的文字信息");
設置圖標:
prodialog.setIcon(R.drawable.ic_launcher)
/* 設定關於ProgressDialog的一些屬性 */
設定最大進度:
prodialog.setMax(100);
設定初始化已經增長到的進度:
prodialog.incrementProgressBy(50);
進度條是明確顯示進度的:
prodialog.setIndeterminate(false);
設定確定按鈕:
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {   
 @Override
 public void onClick(DialogInterface dialog, int which) {
  Toast.makeText(MainActivity.this, "你點擊了ProgressDialog的確定鍵", Toast.LENGTH_SHORT).show();
 }
});
設定是否可以取消:
progressDialog.setCancelable(true);

<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"
    >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        />

    <Button
        android:id="@+id/button_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="增加" />

    <Button
        android:id="@+id/button_reduce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="減少" />

    <Button
        android:id="@+id/button_reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="重置" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="顯示ProgressDialog" />

</LinearLayout>
activity_main.xml
package com.example.progressbar;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {
    
    private ProgressBar progressBar;
    private Button addButton;
    private Button reduceButton;
    private Button resetButton;
    private TextView textView;
    private Button showButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(false);
        setProgress(3000);
        
        progressBar = (ProgressBar) findViewById(R.id.progressBar4);
        progressBar.setSecondaryProgress(10);
        addButton = (Button) findViewById(R.id.button_add);
        reduceButton = (Button) findViewById(R.id.button_reduce);
        resetButton = (Button) findViewById(R.id.button_reset);
        textView = (TextView) findViewById(R.id.textView1);
        showButton = (Button) findViewById(R.id.button_show);
        
        int firstProgressNum = progressBar.getProgress();
        int secondProgressNum = progressBar.getSecondaryProgress();
        int maxProgressNum = progressBar.getMax();
        textView.setText("第一進度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%\n"
                        + "第二進度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%");
        
        addButton.setOnClickListener(this);
        reduceButton.setOnClickListener(this);
        resetButton.setOnClickListener(this);
        showButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button_add:
            if (progressBar.getProgress() + 1 <= progressBar.getMax()) 
                progressBar.incrementProgressBy(1);
            if (progressBar.getSecondaryProgress() + 1 <= progressBar.getMax())
                progressBar.incrementSecondaryProgressBy(1);
            break;
        case R.id.button_reduce:
            if (progressBar.getProgress() - 1 >= 0)
                progressBar.incrementProgressBy(-1);
            if (progressBar.getSecondaryProgress() - 1 >= 0)
                progressBar.incrementSecondaryProgressBy(-1);
            break;
        case R.id.button_reset:
            progressBar.setProgress(0);
            progressBar.setSecondaryProgress(10);
            break;
        case R.id.button_show:
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setTitle("ProgressDialog的標題");
            progressDialog.setMessage("ProgressDialog的文字信息");
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setMax(100);
            progressDialog.setIndeterminate(false);
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "你點擊了ProgressDialog的確定鍵", Toast.LENGTH_SHORT).show();
                }
            });
            progressDialog.setCancelable(true);
            progressDialog.show();
        }
        int firstProgressNum = progressBar.getProgress();
        int secondProgressNum = progressBar.getSecondaryProgress();
        int maxProgressNum = progressBar.getMax();
        textView.setText("第一進度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%\n"
                        + "第二進度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%");
    }
}
MainActivity.java

自定義進度條樣式
其實橫向顯示進度條的真正的樣式是:
@android:style/Widget.ProgressBar.Horizontal
這個樣式里面的最重要的其實是其中的style標簽的item標簽的android:progressDrawable
progressDrawable屬性對應一個文件——這里是@android:drawable/progress_horizontal
可以通過給ProgressBar添加一個android:progressDrawable屬性來覆蓋之前的屬性來達到(比如:修改顏色)的效果。

效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM