[控件]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默认圆

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值