android 控件之ProgressBar實現雙進度條(通常用於流媒體的緩衝區進度)

下面詳細介紹ProgressBar




一、說明


  在某些操作的進度中的可視指示器,爲用戶呈現操作的進度,還它有一個次要的進度條,用來顯示中間進度,如在流媒體播放的緩衝區的進度。一個進度條也可不確定其進度。在不確定模式下,進度條顯示循環動畫。這種模式常用於應用程序使用任務的長度是未知的。


二、XML重要屬性


    android:progressBarStyle:默認進度條樣式


    android:progressBarStyleHorizontal:水平樣式


 


三、重要方法


    getMax():返回這個進度條的範圍的上限


    getProgress():返回進度


    getSecondaryProgress():返回次要進度


    incrementProgressBy(int diff):指定增加的進度


    isIndeterminate():指示進度條是否在不確定模式下


    setIndeterminate(boolean indeterminate):設置不確定模式下


    setVisibility(int v):設置該進度條是否可視


四、重要事件


    onSizeChanged(int w, int h, int oldw, int oldh):當進度值改變時引發此事件


五、實例


1.佈局文件:

     

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="默認進度條" />       

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定義進度條" />       

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button android:id="@+id/decrease_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二減少" />

        <Button android:id="@+id/increase_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二增加" />

    </LinearLayout>

</LinearLayout>

Activity中的使用:

 

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.activity_main);
		setProgressBarVisibility(true);

		final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
		setProgress(progressHorizontal.getProgress() * 100);
		setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);

		Button button = (Button) findViewById(R.id.increase);
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				progressHorizontal.incrementProgressBy(1);
				// Title progress is in range 0..10000
				setProgress(100 * progressHorizontal.getProgress());
			}
		});

		button = (Button) findViewById(R.id.decrease);
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				progressHorizontal.incrementProgressBy(-1);
				// Title progress is in range 0..10000
				setProgress(100 * progressHorizontal.getProgress());
			}
		});

		button = (Button) findViewById(R.id.increase_secondary);
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				progressHorizontal.incrementSecondaryProgressBy(1);
				// Title progress is in range 0..10000
				setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
			}
		});

		button = (Button) findViewById(R.id.decrease_secondary);
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				progressHorizontal.incrementSecondaryProgressBy(-1);
				// Title progress is in range 0..10000
				setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
			}
		});

	}

}

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