android之进度条

xml引用

<ProgressBar
        android:id="@+id/pb_progressbar"
        style="@style/StyleProgressBarMini"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_margin="30dp"
        android:background="@drawable/shape_progressbar_bg"
        android:max="100"
        android:progress="0" />
ProgressBar

样式styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="StyleProgressBarMini" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:maxHeight">50dip</item>
        <item name="android:minHeight">10dip</item>
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:progressDrawable">@drawable/shape_progressbar_mini</item>
    </style>
</resources>
styles.xml

边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <!--边框填充的颜色 --> 
    <solid android:color="#cecece" />
    <!-- 设置进度条的四个角为弧形 --> 
    <!-- android:radius 弧形的半径  -->
    <corners android:radius="90dp" />
    <!-- padding:边界的间隔 -->
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>
shape_progressbar_bg.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="5dip" />
                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="@color/white"
                    android:startColor="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="0dip" />
                <gradient
                android:angle="270"
                android:centerY="0.75"
                android:endColor="#df0024"
                android:startColor="#df0024" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="@color/blue"
                    android:startColor="@color/blue" />
            </shape>
        </clip>
    </item>
</layer-list>
shape_progressbar_mini.xml

后台设置

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
    private ProgressBar pb_progressbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb_progressbar=(ProgressBar)findViewById(R.id.pb_progressbar);
        handler.postDelayed(runnable, DELYED); //启动定时器
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    //定時器
    private int DELYED= 500;
    private int timeOut=0;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                handler.postDelayed(this, DELYED);
                timeOut++;
                pb_progressbar.setProgress(timeOut);
                
            } catch (Exception e) {
                Log.e("->runnable定时器", e.toString());
            }
        }
    };
}
Java代码
原文地址:https://www.cnblogs.com/huangzhen22/p/5025904.html