[控件]ProgressBar進度條

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class mainActivity extends Activity {
    Button btn;
    ProgressBar pb1, pb2;
    int count = 0;
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            pb1.setProgress(msg.arg1);
            pb2.setProgress(msg.arg1);
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pb1 = (ProgressBar) findViewById(R.id.pb1);
        pb2 = (ProgressBar) findViewById(R.id.pb2);
        btn = (Button) findViewById(R.id.btn);
        pb1.setIndeterminate(false);
        pb2.setIndeterminate(false);

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i = 0; i < 100; i++) {
                            try {
                                count++;
                                Thread.sleep(100);
                                Message msg = handler.obtainMessage();
                                msg.arg1 = count;
                                msg.sendToTarget();
                            }
                            catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">

	<ProgressBar
		android:id="@+id/pb1"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		style="?android:attr/progressBarStyleHorizontal"></ProgressBar>
	<ProgressBar
		android:id="@+id/pb2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		style="?android:attr/progressBarStyleLarge"
		android:max="100"
		android:secondaryProgress="70"></ProgressBar>
	<Button
		android:id="@+id/btn"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:text="開始"></Button>
</LinearLayout>
自定義進度條旋轉所使用的XML文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list
	xmlns:android="http://schemas.android.com/apk/res/android">
	<item>
		<rotate android:drawable="@drawable/logining_ring"
			android:fromDegrees="0.0" android:toDegrees="360.0"
			android:pivotX="50.0%" android:pivotY="50.0%" />
	</item>
</layer-list>
fromDegrees 爲動畫起始時物件的角度
toDegrees 屬性爲動畫結束時物件旋轉的角度 能夠大於360度
當角度爲負數——表示逆時針旋轉
當角度爲正數——表示順時針旋轉
(負數from——to正數:順時針旋轉)
(負數from——to負數:逆時針旋轉)
(正數from——to正數:順時針旋轉)
(正數from——to負數:逆時針旋轉)
pivotX
pivotY
爲動畫相對於物件的X、Y座標的開始位
以上兩個屬性值 從0%-100%中取值
50%爲物件的X或Y方向座標上的中點位置

自定義橫向進度條
<ProgressBar
    android:id="@id/pbWeb"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="3dip"
    android:progressDrawable="@drawable/progressbar" />
progressbar.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dip" />

            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <solid android:color="@color/main_color" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <solid android:color="@color/main_color" />
            </shape>
        </clip>
    </item>

</layer-list>




android:indeterminateOnly 若是設置爲true那麼忽略進度顯示呈現循環動畫
android:indeterminateDrawable顯示自定義旋轉XML
android:max最大值默認爲100
android:secondaryProgress設置第二進度條
style進度條樣式
?android:attr/progressBarStyleHorizontal 進度條
?android:attr/progressBarStyleLarge大圓
?android:attr/progressBarStyleSmall小圓
?android:attr/progressBarStyle默認圓

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。html

相關文章
相關標籤/搜索