Android Progress 使用示例

自定义进度条背景 my_progress_bar.xml ,在 drawable 目录中创建它:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        android:drawable="@drawable/no" />

    <item android:id="@android:id/progress"
        android:drawable="@drawable/yes" />
</layer-list>

上面代码中使用两张图片,大家可以自行添加。

主布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorGray"
    >

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

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Large"
            android:layout_marginStart="5dp"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Large.Inverse"
            android:layout_marginStart="5dp"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Inverse"
            android:layout_marginStart="5dp"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Small"
            android:layout_marginStart="5dp"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Small.Inverse"
            android:layout_marginStart="5dp"
            />

    </LinearLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/currentProgress"
        android:layout_marginTop="40dp"
        />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_marginTop="20dp"
        />

    <ProgressBar
        android:id="@+id/myProgressBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:max="100"
        android:progressDrawable="@drawable/my_progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_marginTop="40dp"
        />

</LinearLayout>

主程序文件代码:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    final private static String TAG = "Toby_Test";

    final private int msgWhat = 20170318;
    final int[] data = new int[100];

    private int hasData = 0;
    private int status = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar bar1 = (ProgressBar) findViewById(R.id.progressBar);
        final ProgressBar bar2 = (ProgressBar) findViewById(R.id.myProgressBar);

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                if (msg.what == msgWhat) {
                    bar1.setProgress(status);
                    bar2.setProgress(status);
                }

                super.handleMessage(msg);
            }
        };

        new Thread() {
            @Override
            public void run() {
                while (status < 100) {
                    status = doWork();
                    handler.sendEmptyMessage(msgWhat);
                }
            }
        }.start();

    }

    private int doWork() {
        data[hasData++] = (int) (Math.random() * 100);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }

}

运行效果:

《Android Progress 使用示例》 运行效果

参考文献:《疯狂Android讲义(第2版)》

    原文作者:赵者也
    原文地址: https://www.jianshu.com/p/ac1a362e1097
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞